diff options
author | Schwarzkopf Balázs <schwarzkopfb@icloud.com> | 2020-09-14 16:22:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-14 16:22:07 +0200 |
commit | f6bfdd66a69fb01077e488b5a67a38ca3d43610e (patch) | |
tree | b2613d944f83bfdc2d17691494d87ca839ed4f30 /std/node/assertion_error_test.ts | |
parent | a6f34d47222ad7cc40519bd95a58ae773d1fe656 (diff) |
feat(std/node): Add AssertionError class (#7210)
Diffstat (limited to 'std/node/assertion_error_test.ts')
-rw-r--r-- | std/node/assertion_error_test.ts | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/std/node/assertion_error_test.ts b/std/node/assertion_error_test.ts new file mode 100644 index 000000000..b0457fe5f --- /dev/null +++ b/std/node/assertion_error_test.ts @@ -0,0 +1,175 @@ +import { stripColor } from "../fmt/colors.ts"; +import { + assert, + assertEquals, + assertNotStrictEquals, + assertStrictEquals, +} from "../testing/asserts.ts"; +import { + AssertionError, + copyError, + inspectValue, + createErrDiff, +} from "./assertion_error.ts"; + +Deno.test({ + name: "copyError()", + fn() { + class TestError extends Error {} + const err = new TestError("this is a test"); + const copy = copyError(err); + + assert(copy instanceof Error, "Copy should inherit from Error."); + assert(copy instanceof TestError, "Copy should inherit from TestError."); + assertEquals(copy, err, "Copy should be equal to the original error."); + assertNotStrictEquals( + copy, + err, + "Copy should not be strictly equal to the original error.", + ); + }, +}); + +Deno.test({ + name: "inspectValue()", + fn() { + const obj = { a: 1, b: [2] }; + Object.defineProperty(obj, "c", { value: 3, enumerable: false }); + assertStrictEquals( + stripColor(inspectValue(obj)), + "{ a: 1, b: [ 2 ] }", + ); + }, +}); + +Deno.test({ + name: "createErrDiff()", + fn() { + assertStrictEquals( + stripColor( + createErrDiff({ a: 1, b: 2 }, { a: 2, b: 2 }, "strictEqual"), + ), + stripColor( + 'Expected "actual" to be reference-equal to "expected":' + "\n" + + "+ actual - expected" + "\n" + + "\n" + + "+ { a: 1, b: 2 }" + "\n" + + "- { a: 2, b: 2 }", + ), + ); + }, +}); + +Deno.test({ + name: "construct AssertionError() with given message", + fn() { + const err = new AssertionError( + { + message: "answer", + actual: "42", + expected: "42", + operator: "notStrictEqual", + }, + ); + assertStrictEquals(err.name, "AssertionError"); + assertStrictEquals(err.message, "answer"); + assertStrictEquals(err.generatedMessage, false); + assertStrictEquals(err.code, "ERR_ASSERTION"); + assertStrictEquals(err.actual, "42"); + assertStrictEquals(err.expected, "42"); + assertStrictEquals(err.operator, "notStrictEqual"); + }, +}); + +Deno.test({ + name: "construct AssertionError() with generated message", + fn() { + const err = new AssertionError( + { actual: 1, expected: 2, operator: "equal" }, + ); + assertStrictEquals(err.name, "AssertionError"); + assertStrictEquals(stripColor(err.message), "1 equal 2"); + assertStrictEquals(err.generatedMessage, true); + assertStrictEquals(err.code, "ERR_ASSERTION"); + assertStrictEquals(err.actual, 1); + assertStrictEquals(err.expected, 2); + assertStrictEquals(err.operator, "equal"); + }, +}); + +Deno.test({ + name: "construct AssertionError() with stackStartFn", + fn: function stackStartFn() { + const expected = /node/; + const err = new AssertionError({ + actual: "deno", + expected, + operator: "match", + stackStartFn, + }); + assertStrictEquals(err.name, "AssertionError"); + assertStrictEquals(stripColor(err.message), "deno match /node/"); + assertStrictEquals(err.generatedMessage, true); + assertStrictEquals(err.code, "ERR_ASSERTION"); + assertStrictEquals(err.actual, "deno"); + assertStrictEquals(err.expected, expected); + assertStrictEquals(err.operator, "match"); + assert(err.stack, "error should have a stack"); + assert( + !err.stack?.includes("stackStartFn"), + "stackStartFn() should not present in stack trace", + ); + }, +}); + +Deno.test({ + name: "error details", + fn() { + const stack0 = new Error(); + const stack1 = new Error(); + const err = new AssertionError({ + message: "Function(s) were not called the expected number of times", + details: [ + { + message: + "Expected the calls function to be executed 2 time(s) but was executed 3 time(s).", + actual: 3, + expected: 2, + operator: "calls", + stack: stack0, + }, + { + message: + "Expected the fn function to be executed 1 time(s) but was executed 0 time(s).", + actual: 0, + expected: 1, + operator: "fn", + stack: stack1, + }, + ], + }); + + assertStrictEquals( + err.message, + "Function(s) were not called the expected number of times", + ); + + assertStrictEquals( + err["message 0"], + "Expected the calls function to be executed 2 time(s) but was executed 3 time(s).", + ); + assertStrictEquals(err["actual 0"], 3); + assertStrictEquals(err["expected 0"], 2); + assertStrictEquals(err["operator 0"], "calls"); + assertStrictEquals(err["stack trace 0"], stack0); + + assertStrictEquals( + err["message 1"], + "Expected the fn function to be executed 1 time(s) but was executed 0 time(s).", + ); + assertStrictEquals(err["actual 1"], 0); + assertStrictEquals(err["expected 1"], 1); + assertStrictEquals(err["operator 1"], "fn"); + assertStrictEquals(err["stack trace 1"], stack1); + }, +}); |