Skip to content

noMisplacedAssertion

诊断类别:lint/suspicious/noMisplacedAssertion

¥Diagnostic Category: lint/suspicious/noMisplacedAssertion

自从:v1.8.0 来源:

¥Since: v1.8.0 Sources:

检查正则表达式是否是简单参数时,检查 unicode 宽度而不是字节数。

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

在执行测试套件时,放置(和使用)expect 断言函数可能会导致意外行为。

¥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 断言模块 URL

    ¥Deno assertion module URL

查看 options 以了解更多信息。

¥Check the options if you need to change the defaults.

¥Examples

¥Invalid

describe("describe", () => {
expect()
})
code-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.

import assert from "node:assert";
describe("describe", () => {
assert.equal()
})
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.

import {test, expect} from "bun:test";
expect(1, 2)
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.

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/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.

¥Valid

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);
});

¥Related links