mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
fix(expect): updated error message for toContain (#4750)
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
parent
9c32f3a57b
commit
7d4b54da68
@ -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}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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}`,
|
||||
);
|
||||
});
|
||||
|
@ -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"',
|
||||
);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user