std/testing/_format_test.ts
Mark Ladyshau 662f27ce84
feat(testing/asserts): return error from assertRejects and assertThrows (#2226)
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2022-05-25 17:51:42 +09:00

68 lines
1.2 KiB
TypeScript

// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { green, red, stripColor } from "../fmt/colors.ts";
import { assertEquals, assertThrows } from "./asserts.ts";
import { format } from "./_format.ts";
Deno.test("assert diff formatting (strings)", () => {
assertThrows(
() => {
assertEquals([..."abcd"].join("\n"), [..."abxde"].join("\n"));
},
Error,
`
a\\n
b\\n
${green("+ x")}\\n
${green("+ d")}\\n
${green("+ e")}
${red("- c")}\\n
${red("- d")}
`,
);
});
// Check that the diff formatter overrides some default behaviours of
// `Deno.inspect()` which are problematic for diffing.
Deno.test("assert diff formatting", () => {
// Wraps objects into multiple lines even when they are small. Prints trailing
// commas.
assertEquals(
stripColor(format({ a: 1, b: 2 })),
`{
a: 1,
b: 2,
}`,
);
// Same for nested small objects.
assertEquals(
stripColor(format([{ x: { a: 1, b: 2 }, y: ["a", "b"] }])),
`[
{
x: {
a: 1,
b: 2,
},
y: [
"a",
"b",
],
},
]`,
);
// Grouping is disabled.
assertEquals(
stripColor(format(["i", "i", "i", "i", "i", "i", "i"])),
`[
"i",
"i",
"i",
"i",
"i",
"i",
"i",
]`,
);
});