Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

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

Open
mterrel opened this issue Oct 20, 2020 · 0 comments
Open

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

mterrel opened this issue Oct 20, 2020 · 0 comments

Comments

@mterrel
Copy link

mterrel commented Oct 20, 2020

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);
});
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant