-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Mocking changes returned promises #5647
Copy link
Copy link
Closed
Labels
p3-minor-bugAn edge case that only affects very specific usage (priority)An edge case that only affects very specific usage (priority)
Description
Describe the bug
A promise returned by a mocked function or methods, changes a returned promise.
i.e., saving the promise in a variable before returning it and then comparing it to what's returned outside the function with === evaluates to false.
I can't find documentation for this and it doesn't happen with jest, so I suppose it's unintended.
Code from the StackBlitz link just in case:
class Foo {
x?: Promise<void>;
bar() {
return this.x = Promise.resolve();
}
}
Foo.prototype.bar = vi.fn(Foo.prototype.bar);
test('should work', async () => {
const foo = new Foo();
const y = foo.bar();
expect(foo.x === y).toBe(true); // errors
});Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-zkjejn?file=src%2Fbasic.ts
System Info
irrelevantUsed Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
p3-minor-bugAn edge case that only affects very specific usage (priority)An edge case that only affects very specific usage (priority)
Type
Projects
Status
Has plan