fancy-test icon indicating copy to clipboard operation
fancy-test copied to clipboard

Types for stub don't allow stubbing non-function values

Open mterrel opened this issue 5 years ago • 0 comments

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);
});

mterrel avatar Oct 20 '20 17:10 mterrel