fancy-test
fancy-test copied to clipboard
Types for stub don't allow stubbing non-function values
Expected behavior
Using stub to temporarily set an object property to a non-function value should be possible, such as in this TypeScript code:
fancy
.stub(process.stdout, "isTTY", false)
.it("Sets isTTY to false", () => {
expect(process.stdout.isTTY).to.equal(false);
});
Actual behavior
This causes a compile error:
Argument of type 'false' is not assignable to parameter of type '() => any'.
Additional info
While it might look tempting to use () => false as the final argument to stub, this does not create the desired behavior because it sets process.stdout.isTTY to be a function.
Note that this issue is similar to #40, but is intended to encompass any case where the value to be stubbed is not a function or a getter.
I'd be happy to submit a PR with a fix for this issue, if you'd like one. The implementation of stub is arguably correct, except for the types.
Workaround
Cast the 3rd parameter to any, like this and the code works correctly:
fancy
.stub(process.stdout, "isTTY", false as any)
.it("Sets isTTY to false", () => {
expect(process.stdout.isTTY).to.equal(false);
});