eslint-plugin-jest offers a couple of rules to use specialized assertions instead of the generic toEqual().
Following code review comments I made, I suggest to enable the following rules:
-
jest/prefer-comparison-matcher
instead of expect(x > 5).toBe(true);
, use expect(x).toBeGreaterThan(5);
-
jest/prefer-equality-matcher
instead of expect(x === 5).toBe(true);
, use expect(x).toBe(5);
-
jest/prefer-to-be
All equality matchers work the same on primitives, but toBe
is shorter and forms a grammatically natural sentence.
-
jest/prefer-to-contain
instead of expect(a.includes(b)).toBe(true);
, use expect(a).toContain(b);
-
jest/prefer-to-have-length
instead of expect(files.length).toBe(1);
, use expect(files).toHaveLength(1);
as it gives a better failure message
-
jest/prefer-strict-equal
toStrictEqual() is stricter with undefined
and does not allow comparing class instance to object literal. This one would be enabled as a warning only, because there are >1000 usages that cannot be fixed automatically.