src/content/docs/linter/rules/no-misplaced-assertion.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.8.0` - Diagnostic Category: [`lint/suspicious/noMisplacedAssertion`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Inspired from [`jest/no-standalone-expect`](https://github.com/jest-community/eslint-plugin-jest/blob/main/docs/rules/no-standalone-expect.md) - Inspired from [`vitest/no-standalone-expect`](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/no-standalone-expect.md){
"linter": {
"rules": {
"suspicious": {
"noMisplacedAssertion": "error"
}
}
}
}
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:
expectassertassertEqualsHowever, the rule will ignore the following assertion calls:
expect.anyexpect.anythingexpect.closeToexpect.arrayContainingexpect.objectContainingexpect.stringContainingexpect.stringMatchingexpect.extendexpect.addEqualityTestersexpect.addSnapshotSerializerIf the assertion function is imported, the rule will check if they are imported from:
"chai""node:assert""node:assert/strict""bun:test""vitest"Check the options if you need to change the defaults.
describe("describe", () => {
expect()
})
import assert from "node:assert";
describe("describe", () => {
assert.equal()
})
import {test, expect} from "bun:test";
expect(1, 2)
import {assertEquals} from "https://deno.land/[email protected]/assert/mod.ts";
assertEquals(url.href, "https://deno.land/foo.js");
Deno.test("url test", () => {
const url = new URL("./foo.js", "https://deno.land/");
});
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);
});