跳转到内容

noMisplacedAssertion (since v1.8.0)

Diagnostic Category: lint/nursery/noMisplacedAssertion

Sources:

Checks that the assertion function, for example expect, is placed inside an it() function call.

Placing (and using) the expect assertion function can result in unexpected behaviors when executing your testing suite.

The rule will check for the following assertion calls:

  • expect
  • assert
  • assertEquals

However, the rule will ignore the following assertion calls:

  • expect.any
  • expect.anything
  • expect.closeTo
  • expect.arrayContaining
  • expect.objectContaining
  • expect.stringContaining
  • expect.stringMatching
  • expect.extend
  • expect.addEqualityTesters
  • expect.addSnapshotSerializer

If the assertion function is imported, the rule will check if they are imported from:

  • "chai"
  • "node:assert"
  • "node:assert/strict"
  • "bun:test"
  • "vitest"
  • Deno assertion module URL

Check the options if you need to change the defaults.

describe("describe", () => {
expect()
})
code-block.js:2:5 lint/nursery/noMisplacedAssertion ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The assertion isn't inside a it(), test() or Deno.test() function call.
  
    1 │ describe("describe", () => {
  > 2 │     expect()
       ^^^^^^
    3 │ })
    4 │ 
  
   This will result in unexpected behaviours from your test suite.
  
   Move the assertion inside a it(), test() or Deno.test() function call.
  
import assert from "node:assert";
describe("describe", () => {
assert.equal()
})
code-block.js:3:5 lint/nursery/noMisplacedAssertion ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The assertion isn't inside a it(), test() or Deno.test() function call.
  
    1 │ import assert from "node:assert";
    2 │ describe("describe", () => {
  > 3 │     assert.equal()
       ^^^^^^
    4 │ })
    5 │ 
  
   This will result in unexpected behaviours from your test suite.
  
   Move the assertion inside a it(), test() or Deno.test() function call.
  
import {test, expect} from "bun:test";
expect(1, 2)
code-block.js:2:1 lint/nursery/noMisplacedAssertion ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The assertion isn't inside a it(), test() or Deno.test() function call.
  
    1 │ import {test, expect} from "bun:test";
  > 2 │ expect(1, 2)
   ^^^^^^
    3 │ 
  
   This will result in unexpected behaviours from your test suite.
  
   Move the assertion inside a it(), test() or Deno.test() function call.
  
import {assertEquals} from "https://deno.land/std@0.220.0/assert/mod.ts";
assertEquals(url.href, "https://deno.land/foo.js");
Deno.test("url test", () => {
const url = new URL("./foo.js", "https://deno.land/");
});
code-block.js:3:1 lint/nursery/noMisplacedAssertion ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The assertion isn't inside a it(), test() or Deno.test() function call.
  
    1 │ import {assertEquals} from "https://deno.land/std@0.220.0/assert/mod.ts";
    2 │ 
  > 3 │ assertEquals(url.href, "https://deno.land/foo.js");
   ^^^^^^^^^^^^
    4 │ Deno.test("url test", () => {
    5 │     const url = new URL("./foo.js", "https://deno.land/");
  
   This will result in unexpected behaviours from your test suite.
  
   Move the assertion inside a it(), test() or Deno.test() function call.
  
import assert from "node:assert";
describe("describe", () => {
it("it", () => {
assert.equal()
})
})
describe("describe", () => {
it("it", () => {
expect()
})
})
test.each([1, 2, 3])('test', (a, b, expected) => {
expect(a + b).toBe(expected)
})
import { waitFor } from '@testing-library/react';
await waitFor(() => {
expect(111).toBe(222);
});