fix(expect): updated error message for toContain (#4750)

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
Jacob Avery 2024-05-17 01:33:05 -04:00 committed by GitHub
parent 9c32f3a57b
commit 7d4b54da68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 59 additions and 16 deletions

View File

@ -373,13 +373,20 @@ export function toContain(
// deno-lint-ignore no-explicit-any
const doesContain = (context.value as any)?.includes?.(expected);
const fmtValue = format(context.value);
const fmtExpected = format(expected);
if (context.isNot) {
if (doesContain) {
throw new AssertionError("The value contains the expected item");
throw new AssertionError(
`The value ${fmtValue} contains the expected item ${fmtExpected}`,
);
}
} else {
if (!doesContain) {
throw new AssertionError("The value doesn't contain the expected item");
throw new AssertionError(
`The value ${fmtValue} doesn't contain the expected item ${fmtExpected}`,
);
}
}
}
@ -399,13 +406,29 @@ export function toContainEqual(
}
}
const prettyStringify = (js: unknown) =>
JSON.stringify(js, null, "\t")
.replace(/\"|\n|\t/g, "")
.slice(0, 100);
const fmtValue = prettyStringify(context.value);
const fmtExpected = prettyStringify(expected);
if (context.isNot) {
if (doesContain) {
throw new AssertionError("The value contains the expected item");
throw new AssertionError(
`The value contains the expected item.
Value: ${fmtValue}
Expected: ${fmtExpected}`,
);
}
} else {
if (!doesContain) {
throw new AssertionError("The value doesn't contain the expected item");
throw new AssertionError(
`The value doesn't contain the expected item.
Value: ${fmtValue}
Expected: ${fmtExpected}`,
);
}
}
}

View File

@ -9,11 +9,23 @@ Deno.test("expect().toContainEqual()", () => {
expect(arr).not.toContainEqual({ bar: 42 });
assertThrows(() => {
expect(arr).toContainEqual({ bar: 42 });
}, AssertionError);
assertThrows(
() => {
expect(arr).toContainEqual({ bar: 42 });
},
AssertionError,
`The value doesn't contain the expected item.
Value: [{foo: 42},{bar: 43},{baz: 44}]
Expected: {bar: 42}`,
);
assertThrows(() => {
expect(arr).not.toContainEqual({ bar: 43 });
}, AssertionError);
assertThrows(
() => {
expect(arr).not.toContainEqual({ foo: 42 });
},
AssertionError,
`The value contains the expected item.
Value: [{foo: 42},{bar: 43},{baz: 44}]
Expected: {foo: 42}`,
);
});

View File

@ -15,14 +15,22 @@ Deno.test("expect().toContain()", () => {
assertThrows(() => {
expect(arr).toContain(4);
}, AssertionError);
assertThrows(() => {
expect("foobarbaz").toContain("qux");
}, AssertionError);
assertThrows(
() => {
expect("foobarbaz").toContain("qux");
},
AssertionError,
`The value "foobarbaz" doesn't contain the expected item "qux"`,
);
assertThrows(() => {
expect(arr).not.toContain(2);
}, AssertionError);
assertThrows(() => {
expect("foobarbaz").not.toContain("bar");
}, AssertionError);
assertThrows(
() => {
expect("foobarbaz").not.toContain("bar");
},
AssertionError,
'The value "foobarbaz" contains the expected item "bar"',
);
});