Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expect.hasAssertions() / expect.assertions() - Does not work when using expect() bound to current test #567

Closed
ryanone opened this issue Nov 8, 2024 · 1 comment · Fixed by #595

Comments

@ryanone
Copy link

ryanone commented Nov 8, 2024

Problem: If you have the prefer-expect-assertions rule enabled, and also have test like the following...

it('my test description, ({ expect }) => {
  const a = 1;
  const b = 2;

  expect(sum(a, b)).toBe(a + b);
})

ESLint fails to detect that expect.assertions() or expect.hasAssertions() is not present. I have an example repo here: https://github.com/ryanone/vitest-eslint-bug

But ESLint works properly when using an anonymous function instead of an arrow function...

it('my test description, function ({ expect }) {
  // Now ESLint properly complains
  const a = 1;
  const b = 2;

  expect(sum(a, b)).toBe(a + b);
})
  • Look at src/math.test.ts
  • There are two test cases. The first one is the one with the expect() method bound to the current context, but does not have a call to expect.assertions() or expect.hasAssertions(). Yet, npm run lint passes. The latter test case uses the expect() method imported from vitest, and has a call to expect.hasAssertions(). If removed, npm run lint will fail.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant