useExpectToStrictEqual (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/useExpectToStrictEqual - This rule has an unsafe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Same as
jest/prefer-strict-equal - Same as
vitest/prefer-strict-equal - Same as
playwright/prefer-strict-equal
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useExpectToStrictEqual": "error" } } }}Description
Section titled “Description”Enforce using toStrictEqual() instead of toEqual() to compare values in tests.
toEqual() ignores some differences between the two values it compares:
- A property set to
undefinedis treated the same as a property that doesn’t exist. - An empty slot in an array is treated the same as an item set to
undefined. - An object created from a class is treated the same as a plain object with the same properties.
toEqual() can therefore treat two different values as equal. toStrictEqual() checks
all of these differences.
The fix is unsafe because it can change the result of a test.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”expect({ a: "a", b: undefined }).toEqual({ a: "a" });code-block.js:1:34 lint/nursery/useExpectToStrictEqual FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ toEqual() ignores some differences between the values it compares.
> 1 │ expect({ a: "a", b: undefined }).toEqual({ a: "a" });
│ ^^^^^^^
2 │
ℹ It treats a property set to undefined the same as a missing property, and an object created from a class the same as a plain object, so it can treat two different values as equal.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use toStrictEqual() instead.
1 │ - expect({·a:·"a",·b:·undefined·}).toEqual({·a:·"a"·});
1 │ + expect({·a:·"a",·b:·undefined·}).toStrictEqual({·a:·"a"·});
2 2 │
expect(user).not.toEqual(otherUser);code-block.js:1:18 lint/nursery/useExpectToStrictEqual FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ toEqual() ignores some differences between the values it compares.
> 1 │ expect(user).not.toEqual(otherUser);
│ ^^^^^^^
2 │
ℹ It treats a property set to undefined the same as a missing property, and an object created from a class the same as a plain object, so it can treat two different values as equal.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use toStrictEqual() instead.
1 │ - expect(user).not.toEqual(otherUser);
1 │ + expect(user).not.toStrictEqual(otherUser);
2 2 │
await expect(loadUser()).resolves.toEqual({ name: "Ada" });code-block.js:1:35 lint/nursery/useExpectToStrictEqual FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ toEqual() ignores some differences between the values it compares.
> 1 │ await expect(loadUser()).resolves.toEqual({ name: "Ada" });
│ ^^^^^^^
2 │
ℹ It treats a property set to undefined the same as a missing property, and an object created from a class the same as a plain object, so it can treat two different values as equal.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use toStrictEqual() instead.
1 │ - await·expect(loadUser()).resolves.toEqual({·name:·"Ada"·});
1 │ + await·expect(loadUser()).resolves.toStrictEqual({·name:·"Ada"·});
2 2 │
expect({ a: "a", b: undefined }).toStrictEqual({ a: "a", b: undefined });expect(user).not.toStrictEqual(otherUser);await expect(loadUser()).resolves.toStrictEqual({ name: "Ada" });Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.