Closed avishaik closed 1 year ago
Could you please include some actually runnable sample code? Your example is not runnable as is, and I am not convinced that it works fine in Sinon 12, just that it runs (fails stubbing silently).
You are right, my mistake, in version 12.0.1 I don't get an error, but the stub does not really work, the execSync
function runs the original implementation and not the stub.
Please try the following code: index.ts
import { execSync } from "child_process";
export function execCmd(cmd: string): string {
try {
const cmdRes = execSync(cmd).toString().trim();
console.log(cmdRes);
return cmdRes;
} catch (error) {
//error
}
}
index.spec.ts
import { expect } from "chai";
import sinon from "sinon";
import * as childProcess from "child_process";
import { execCmd } from "../src/index";
it('return string of execSync', function() {
sinon.stub(childProcess, "execSync").returns("test");
const cmd = execCmd("dir");
expect(cmd).to.equal("test");
});
I would expect cmd
to equal "test", instead I get the "dir" command result.
You are destructuring the execSync
function from child_process
. So you hold the function reference directly. Sinon the replaces the function on the module, but the original function doesn't change.
There is no way sinon can make this work. You will have to hold a reference to the module and call child_process.execSync(...)
to call the stub.
See my comment here: https://stackoverflow.com/a/52591287/200987
Describe the bug Hello team,
There is an issue with sinon version 14.0.1 when stubbing for example
execSync
fromchild_process
. For example this code:For some reason when running the test, the stub always fails with the following error:
I downgraded to 12.0.1 and it's working fine.
To Reproduce Steps to reproduce the behavior:
Please use my test example
Expected behavior Expected the stub to work
Context (please complete the following information):