Skip to content

useExpectToStrictEqual (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useExpectToStrictEqual": "error"
}
}
}
}

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 undefined is 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.

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