Skip to content

Conversation

@grypez
Copy link
Contributor

@grypez grypez commented Apr 30, 2025

A demonstration that the rule vitest/prefer-spy-on outputs code which is not semantically equivalent to the input.

Comment on lines +5 to +9
const mocks = vi.hoisted(() => {
const Foo = vi.fn();
Foo.prototype.baz = vi.fn().mockResolvedValue('Hello fizz');
return { Foo };
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sucks that --fix is broken for this, but for future reference, this is semantically equivalent and keeps the linter happy:

Suggested change
const mocks = vi.hoisted(() => {
const Foo = vi.fn();
Foo.prototype.baz = vi.fn().mockResolvedValue('Hello fizz');
return { Foo };
});
const mocks = vi.hoisted(() => {
class Foo {
baz = vi.fn().mockResolvedValue('Hello fizz');
}
return { Foo };
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Don't ask me why the linter is happy with this and not the other formulation.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How peculiar.

@rekmarks rekmarks closed this May 1, 2025
@rekmarks rekmarks deleted the lint-repro branch May 1, 2025 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants