vitest-dev / vitest-dev/vitest
Show warning if `toEqual` compares the object which Object.keys returns empty array for
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 17.1k
- Forks
- 2k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 94
Description
Clear and concise description of the problem
I was using vitest for test for React Router application.
I noticed that Object.keys(Response) returns [], thus it doesn't check anything but whether both objects are response.
This means, this test passes:
test("hoge", () => {
const r1 = new Response("hoge", { status: 200 });
const r2 = new Response(null, { status: 404 });
expect(r1).toEqual(r2);
});
Which is clearly a false-negative.
Suggested solution
It would be difficult to prepare matchers for all objects, but it would be helpful if vitest shows warning if nothing is compared (except plain empty object: {}).
Simple PoC:
import { test, expect } from "vitest";
const isPlainObject = (obj: unknown) => {
return obj.constructor === Object || Object.getPrototypeOf(obj) === null;
};
expect.addEqualityTesters([
(a: unknown, b: unknown) => {
if (
Object.keys(a).length === 0 &&
!isPlainObject(a) &&
Object.keys(b).length === 0 &&
!isPlainObject(b)
) {
console.log("Warning: Comparing two empty non-plain objects, causing false-negative results.");
}
return undefined;
},
]);
test("hoge", () => {
const r1 = new Response("hoge", { status: 200 });
const r2 = new Response(null, { status: 200 });
expect(r1).toEqual(r2);
});
Alternative
No response
Additional context
related: https://github.com/vitest-dev/vitest/issues/9522, https://github.com/vitest-dev/vitest/issues/7991
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the toEqual equality path and the expect.addEqualityTesters example in the issue, then reproduce the Response comparison with the provided test. Determine how empty non-plain objects are identified without warning for plain {} values. Done means the false-negative case produces a warning while ordinary empty plain-object comparisons remain unaffected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- testing-qa
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100