noMisplacedAssertion
Diagnostic Category: lint/suspicious/noMisplacedAssertion
Since: v1.8.0
Sources:
- Inspired from:
jest/no-standalone-expect
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.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.js:2:5 lint/suspicious/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.
code-block.js:3:5 lint/suspicious/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.
code-block.js:2:1 lint/suspicious/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.
code-block.js:3:1 lint/suspicious/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.