Expect toEqual: undefined properties are required in the expected value
Describe the bug
When using expect().toEqual, undefined properties in the test value cause a failure when those properties don't exist in the expect value.
Steps to Reproduce
import { expect as denoExpect } from "https://deno.land/std/expect/mod.ts";
import { expect as npmExpect } from "npm:expect";
// Passes
Deno.test("npm expect().toEqual ignores undefined values", () => {
npmExpect({
prop1: "string",
prop2: undefined,
}).toEqual({
prop1: "string",
});
});
// Fails
Deno.test("deno expect().toEqual ignores undefined values", () => {
denoExpect({
prop1: "string",
prop2: undefined,
}).toEqual({
prop1: "string",
});
});
Expected behavior
I expect deno expect to behave the same as npm expect and the test above should have passed. If there are undefined properties in the test value, those properties shouldn't need to exist in the expect value.
Environment
- OS: MacOS 13.6.3 ARM
- deno version: 1.38.5
- std version: latest main and stable (0.208.0)
We should probably delete undefined keys before passing them to assertEquals
The presence of undefined values could make the application code behave differently, consider for example: Object.keys({ a: 1, b: undefined }) = [ a, b ]
In npm expect, if one wants to make sure that the expected and received are strictly the same, including undefined values, one can use toStrictEquals.
However in deno toStrictEequals does not have the same meaning and cannot be used in that case.
Deno.test('deno Expect StrictEquals', () => {
denoExpect({}).toStrictEquals({}) // Fails
});
Deno.test('npm Expect StrictEquals', () => {
expect({a:undefined}).toStrictEqual({a:undefined}); // pass
});
node assert library is similar to npm expect.
import assert from "node:assert"
Deno.test("node assert", () => {
assert.deepStrictEqual({},{}); //pass
});
If undefined values are allowed, what would be the equivalent of npm expect strictEquals in deno ? Or should deno strictEquals be changed to match.
I also feel that in most cases the current behavior is better and npm toEquals is a footgun for new developers. So Maybe, instead of changing deno toEquals and potentially toStrictEquals,
undefined properties should be allowed in a new api like toLooseEquals, although this makes deno less aligned with the rest of the js ecosystem, so not sure.
Thanks for the input. It looks like we also need to fix expect().toStrictEqual()
If undefined values are allowed, what would be the equivalent of npm expect strictEquals in deno ?
That's assertEquals in Deno, and that's the main assertion for equality. In std/expect we don't try to fix existing 'expect' behavior. Rather we try to align it with the existing convention to make migration easier.