-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
toEqual/toStrictEqual
does not check custom Error properties
#5244
Comments
This happened as both vitest/packages/expect/src/jest-utils.ts Line 98 in c692f76
Perhaps Vitest need to do more checks when both |
I think jest still has the same code. There's one similar issue and even PR too, but they got closed as they didn't receive attention:
If there were no back-compat or jest-compat concern, then personally it makes sense to do the same treatment as NodeJS's assertion https://nodejs.org/api/assert.html#comparison-details_1, namely these two:
But in practice, changing this condition might have some larger implication. I haven't tried but it might be possible to implement stricter |
@fenghan34 even they are extending from Error, I would expect from the |
@steffanek Thanks for raising an issue btw. The current behavior doesn't seem intuitive for me as well, but the code we're seeing here is originally from Jest https://github.com/jestjs/jest/blob/e6d60cb1ab04a2d691dcbf025ed53d7a07327889/packages/expect-utils/src/jasmineUtils.ts#L86-L88 and the comment says it's based on underscore, so it could be like more than 10 years old. What I just wanted to add is that, there could be some reason people have been adopting this behavior, so I think it's worth spending time surveying some history and more comparison. Like you've already shown, In terms of documentation, there's at least a warning about this for Also here is a repro to show the current behavior without https://stackblitz.com/edit/vitest-dev-vitest-jzxduv?file=test%2Fsimple.test.ts Show codeimport { expect, it, assert } from 'vitest';
import nodeAssert from 'node:assert';
class MyError extends Error {
constructor(message: string, public custom: string) {
super(message);
}
}
class YourError extends Error {
constructor(message: string, public custom: string) {
super(message);
}
}
it('custom', () => {
const e1 = new MyError('hi', 'a');
const e2 = new MyError('hi', 'b');
expect(e1).toEqual(e2);
expect(e1).toStrictEqual(e2);
assert.deepEqual(e1, e2);
nodeAssert.notDeepStrictEqual(e1, e2);
});
it('message', () => {
const e1 = new MyError('hi', 'a');
const e2 = new YourError('hello', 'a');
expect(e1).not.toEqual(e2);
expect(e1).not.toStrictEqual(e2);
assert.notDeepEqual(e1, e2);
nodeAssert.notDeepStrictEqual(e1, e2);
});
it('class', () => {
const e1 = new MyError('hello', 'a');
const e2 = new YourError('hello', 'b');
expect(e1).toEqual(e2);
expect(e1).not.toStrictEqual(e2);
assert.deepEqual(e1, e2);
nodeAssert.notDeepStrictEqual(e1, e2);
}); Next I'll see if I can do something with |
toEqual/toStrictEqual
does not check custom Error properties
I explored On Vitest side, probably what we can do for now is to just put more warning on documentation #5253 |
@steffanek Yes, It makes sense to me to be honest. And I think @hi-ogawa has provided the appropriate solution at this time. |
Thanks @hi-ogawa and @fenghan34 for your quick response and solution. |
Just a note, Vitest supports extended assert API: https://vitest.dev/api/assert.html |
@sheremet-va |
@steffanek Using
Regarding performance, I would guess |
Makes sense @hi-ogawa thanks for your contribution. Let's see if this behavior is really desired, to enable it by default. |
Hi @hi-ogawa it seems that I do not need anymore to use the custom matcher that you've initially provided. import { it, expect } from "vitest";
import { Schema } from "@effect/schema";
import { Exit } from "effect";
class SchemaTaggedError extends Schema.TaggedError<SchemaTaggedError>()(
"SchemaTaggedError",
{
msg: Schema.String,
other: Schema.String,
}
) {}
class SchemaTaggedClass extends Schema.TaggedClass<SchemaTaggedClass>()(
"SchemaTaggedClass",
{
msg: Schema.String,
other: Schema.String,
}
) {}
it("Error type", () => {
expect(new SchemaTaggedError({ msg: "a", other: "other" })).toStrictEqual(
new SchemaTaggedError({ msg: "a", other: "other" })
);
});
it("Exit with SchemaTaggedClass", () => {
expect(
Exit.fail(new SchemaTaggedClass({ msg: "a", other: "other" }))
).toStrictEqual(
Exit.fail(new SchemaTaggedClass({ msg: "a", other: "other" }))
);
});
//This should not pass..
it("Exit with SchemaTaggedError", () => {
expect(
Exit.fail(new SchemaTaggedError({ msg: "a", other: "other" }))
).toStrictEqual(
Exit.fail(new SchemaTaggedError({ msg: "b", other: "other" }))
);
}); |
Describe the bug
Hi, I'm using the effect library to construct class instances, and I encounter the following issues, where the 2 assertions below pass (which is wrong):
I've initially raised an issue on the effect, but it result that by comparing with the native assert it works as expected:
Reproduction
reproduction on StackBlitz with the code above
System Info
Used Package Manager
pnpm
Validations
The text was updated successfully, but these errors were encountered: