Skip to main content
Module

std/testing/asserts_test.ts

Deno standard library
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.import { _format, assert, assertNotEquals, assertStringContains, assertArrayContains, assertMatch, assertNotMatch, assertEquals, assertStrictEquals, assertNotStrictEquals, assertThrows, assertThrowsAsync, AssertionError, equal, fail, unimplemented, unreachable,} from "./asserts.ts";import { red, green, gray, bold, yellow, stripColor } from "../fmt/colors.ts";
Deno.test("testingEqual", function (): void { assert(equal("world", "world")); assert(!equal("hello", "world")); assert(equal(5, 5)); assert(!equal(5, 6)); assert(equal(NaN, NaN)); assert(equal({ hello: "world" }, { hello: "world" })); assert(!equal({ world: "hello" }, { hello: "world" })); assert( equal( { hello: "world", hi: { there: "everyone" } }, { hello: "world", hi: { there: "everyone" } }, ), ); assert( !equal( { hello: "world", hi: { there: "everyone" } }, { hello: "world", hi: { there: "everyone else" } }, ), ); assert(equal(/deno/, /deno/)); assert(!equal(/deno/, /node/)); assert(equal(new Date(2019, 0, 3), new Date(2019, 0, 3))); assert(!equal(new Date(2019, 0, 3), new Date(2019, 1, 3))); assert( !equal( new Date(2019, 0, 3, 4, 20, 1, 10), new Date(2019, 0, 3, 4, 20, 1, 20), ), ); assert(equal(new Date("Invalid"), new Date("Invalid"))); assert(!equal(new Date("Invalid"), new Date(2019, 0, 3))); assert(!equal(new Date("Invalid"), new Date(2019, 0, 3, 4, 20, 1, 10))); assert(equal(new Set([1]), new Set([1]))); assert(!equal(new Set([1]), new Set([2]))); assert(equal(new Set([1, 2, 3]), new Set([3, 2, 1]))); assert(equal(new Set([1, new Set([2, 3])]), new Set([new Set([3, 2]), 1]))); assert(!equal(new Set([1, 2]), new Set([3, 2, 1]))); assert(!equal(new Set([1, 2, 3]), new Set([4, 5, 6]))); assert(equal(new Set("denosaurus"), new Set("denosaurussss"))); assert(equal(new Map(), new Map())); assert( equal( new Map([ ["foo", "bar"], ["baz", "baz"], ]), new Map([ ["foo", "bar"], ["baz", "baz"], ]), ), ); assert( equal( new Map([["foo", new Map([["bar", "baz"]])]]), new Map([["foo", new Map([["bar", "baz"]])]]), ), ); assert( equal( new Map([["foo", { bar: "baz" }]]), new Map([["foo", { bar: "baz" }]]), ), ); assert( equal( new Map([ ["foo", "bar"], ["baz", "qux"], ]), new Map([ ["baz", "qux"], ["foo", "bar"], ]), ), ); assert(equal(new Map([["foo", ["bar"]]]), new Map([["foo", ["bar"]]]))); assert(!equal(new Map([["foo", "bar"]]), new Map([["bar", "baz"]]))); assert( !equal( new Map([["foo", "bar"]]), new Map([ ["foo", "bar"], ["bar", "baz"], ]), ), ); assert( !equal( new Map([["foo", new Map([["bar", "baz"]])]]), new Map([["foo", new Map([["bar", "qux"]])]]), ), ); assert(equal(new Map([[{ x: 1 }, true]]), new Map([[{ x: 1 }, true]]))); assert(!equal(new Map([[{ x: 1 }, true]]), new Map([[{ x: 1 }, false]]))); assert(!equal(new Map([[{ x: 1 }, true]]), new Map([[{ x: 2 }, true]]))); assert(equal([1, 2, 3], [1, 2, 3])); assert(equal([1, [2, 3]], [1, [2, 3]])); assert(!equal([1, 2, 3, 4], [1, 2, 3])); assert(!equal([1, 2, 3, 4], [1, 2, 3])); assert(!equal([1, 2, 3, 4], [1, 4, 2, 3])); assert(equal(new Uint8Array([1, 2, 3, 4]), new Uint8Array([1, 2, 3, 4]))); assert(!equal(new Uint8Array([1, 2, 3, 4]), new Uint8Array([2, 1, 4, 3]))); assert( equal(new URL("https://example.test"), new URL("https://example.test")), ); assert( !equal( new URL("https://example.test"), new URL("https://example.test/with-path"), ), );});
Deno.test("testingNotEquals", function (): void { const a = { foo: "bar" }; const b = { bar: "foo" }; assertNotEquals(a, b); assertNotEquals("Denosaurus", "Tyrannosaurus"); assertNotEquals( new Date(2019, 0, 3, 4, 20, 1, 10), new Date(2019, 0, 3, 4, 20, 1, 20), ); assertNotEquals( new Date("invalid"), new Date(2019, 0, 3, 4, 20, 1, 20), ); let didThrow; try { assertNotEquals("Raptor", "Raptor"); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true);});
Deno.test("testingAssertStringContains", function (): void { assertStringContains("Denosaurus", "saur"); assertStringContains("Denosaurus", "Deno"); assertStringContains("Denosaurus", "rus"); let didThrow; try { assertStringContains("Denosaurus", "Raptor"); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true);});
Deno.test("testingArrayContains", function (): void { const fixture = ["deno", "iz", "luv"]; const fixtureObject = [{ deno: "luv" }, { deno: "Js" }]; assertArrayContains(fixture, ["deno"]); assertArrayContains(fixtureObject, [{ deno: "luv" }]); assertArrayContains( Uint8Array.from([1, 2, 3, 4]), Uint8Array.from([1, 2, 3]), ); assertThrows( (): void => assertArrayContains(fixtureObject, [{ deno: "node" }]), AssertionError, `actual: "[ { deno: "luv", }, { deno: "Js", },]" expected to contain: "[ { deno: "node", },]"missing: [ { deno: "node", },]`, );});
Deno.test("testingAssertStringContainsThrow", function (): void { let didThrow = false; try { assertStringContains("Denosaurus from Jurassic", "Raptor"); } catch (e) { assert( e.message === `actual: "Denosaurus from Jurassic" expected to contain: "Raptor"`, ); assert(e instanceof AssertionError); didThrow = true; } assert(didThrow);});
Deno.test("testingAssertStringMatching", function (): void { assertMatch("foobar@deno.com", RegExp(/[a-zA-Z]+@[a-zA-Z]+.com/));});
Deno.test("testingAssertStringMatchingThrows", function (): void { let didThrow = false; try { assertMatch("Denosaurus from Jurassic", RegExp(/Raptor/)); } catch (e) { assert( e.message === `actual: "Denosaurus from Jurassic" expected to match: "/Raptor/"`, ); assert(e instanceof AssertionError); didThrow = true; } assert(didThrow);});
Deno.test("testingAssertStringNotMatching", function (): void { assertNotMatch("foobar.deno.com", RegExp(/[a-zA-Z]+@[a-zA-Z]+.com/));});
Deno.test("testingAssertStringNotMatchingThrows", function (): void { let didThrow = false; try { assertNotMatch("Denosaurus from Jurassic", RegExp(/from/)); } catch (e) { assert( e.message === `actual: "Denosaurus from Jurassic" expected to not match: "/from/"`, ); assert(e instanceof AssertionError); didThrow = true; } assert(didThrow);});
Deno.test("testingAssertsUnimplemented", function (): void { let didThrow = false; try { unimplemented(); } catch (e) { assert(e.message === "unimplemented"); assert(e instanceof AssertionError); didThrow = true; } assert(didThrow);});
Deno.test("testingAssertsUnreachable", function (): void { let didThrow = false; try { unreachable(); } catch (e) { assert(e.message === "unreachable"); assert(e instanceof AssertionError); didThrow = true; } assert(didThrow);});
Deno.test("testingAssertFail", function (): void { assertThrows(fail, AssertionError, "Failed assertion."); assertThrows( (): void => { fail("foo"); }, AssertionError, "Failed assertion: foo", );});
Deno.test("testingAssertFailWithWrongErrorClass", function (): void { assertThrows( (): void => { //This next assertThrows will throw an AssertionError due to the wrong //expected error class assertThrows( (): void => { fail("foo"); }, TypeError, "Failed assertion: foo", ); }, AssertionError, `Expected error to be instance of "TypeError", but was "AssertionError"`, );});
Deno.test("testingAssertThrowsWithReturnType", () => { assertThrows(() => { throw new Error(); });});
Deno.test("testingAssertThrowsAsyncWithReturnType", () => { assertThrowsAsync(() => { throw new Error(); });});
const createHeader = (): string[] => [ "", "", ` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${ green( bold("Expected"), ) }`, "", "",];
const added: (s: string) => string = (s: string): string => green(bold(stripColor(s)));const removed: (s: string) => string = (s: string): string => red(bold(stripColor(s)));
Deno.test({ name: "pass case", fn(): void { assertEquals({ a: 10 }, { a: 10 }); assertEquals(true, true); assertEquals(10, 10); assertEquals("abc", "abc"); assertEquals({ a: 10, b: { c: "1" } }, { a: 10, b: { c: "1" } }); assertEquals(new Date("invalid"), new Date("invalid")); },});
Deno.test({ name: "failed with number", fn(): void { assertThrows( (): void => assertEquals(1, 2), AssertionError, [ "Values are not equal:", ...createHeader(), removed(`- ${yellow("1")}`), added(`+ ${yellow("2")}`), "", ].join("\n"), ); },});
Deno.test({ name: "failed with number vs string", fn(): void { assertThrows( (): void => assertEquals(1, "1"), AssertionError, [ "Values are not equal:", ...createHeader(), removed(`- ${yellow("1")}`), added(`+ "1"`), ].join("\n"), ); },});
Deno.test({ name: "failed with array", fn(): void { assertThrows( (): void => assertEquals([1, "2", 3], ["1", "2", 3]), AssertionError, ` [- 1,+ "1", "2", 3, ]`, ); },});
Deno.test({ name: "failed with object", fn(): void { assertThrows( (): void => assertEquals({ a: 1, b: "2", c: 3 }, { a: 1, b: 2, c: [3] }), AssertionError, ` { a: 1,+ b: 2,+ c: [+ 3,+ ],- b: "2",- c: 3, }`, ); },});
Deno.test({ name: "failed with date", fn(): void { assertThrows( (): void => assertEquals( new Date(2019, 0, 3, 4, 20, 1, 10), new Date(2019, 0, 3, 4, 20, 1, 20), ), AssertionError, [ "Values are not equal:", ...createHeader(), removed(`- ${new Date(2019, 0, 3, 4, 20, 1, 10).toISOString()}`), added(`+ ${new Date(2019, 0, 3, 4, 20, 1, 20).toISOString()}`), "", ].join("\n"), ); assertThrows( (): void => assertEquals( new Date("invalid"), new Date(2019, 0, 3, 4, 20, 1, 20), ), AssertionError, [ "Values are not equal:", ...createHeader(), removed(`- ${new Date("invalid")}`), added(`+ ${new Date(2019, 0, 3, 4, 20, 1, 20).toISOString()}`), "", ].join("\n"), ); },});
Deno.test({ name: "strict pass case", fn(): void { assertStrictEquals(true, true); assertStrictEquals(10, 10); assertStrictEquals("abc", "abc");
const xs = [1, false, "foo"]; const ys = xs; assertStrictEquals(xs, ys);
const x = { a: 1 }; const y = x; assertStrictEquals(x, y); },});
Deno.test({ name: "strict failed with structure diff", fn(): void { assertThrows( (): void => assertStrictEquals({ a: 1, b: 2 }, { a: 1, c: [3] }), AssertionError, ` { a: 1,+ c: [+ 3,+ ],- b: 2, }`, ); },});
Deno.test({ name: "strict failed with reference diff", fn(): void { assertThrows( (): void => assertStrictEquals({ a: 1, b: 2 }, { a: 1, b: 2 }), AssertionError, `Values have the same structure but are not reference-equal:
{ a: 1, b: 2, }`, ); },});
Deno.test({ name: "strictly unequal pass case", fn(): void { assertNotStrictEquals(true, false); assertNotStrictEquals(10, 11); assertNotStrictEquals("abc", "xyz"); assertNotStrictEquals(1, "1");
const xs = [1, false, "foo"]; const ys = [1, true, "bar"]; assertNotStrictEquals(xs, ys);
const x = { a: 1 }; const y = { a: 2 }; assertNotStrictEquals(x, y); },});
Deno.test({ name: "strictly unequal fail case", fn(): void { assertThrows(() => assertNotStrictEquals(1, 1), AssertionError); },});
Deno.test({ name: "assert* functions with specified type parameter", fn(): void { assertEquals<string>("hello", "hello"); assertNotEquals<number>(1, 2); assertArrayContains<boolean>([true, false], [true]); const value = { x: 1 }; assertStrictEquals<typeof value>(value, value); // eslint-disable-next-line @typescript-eslint/ban-types assertNotStrictEquals<object>(value, { x: 1 }); },});
Deno.test("Assert Throws Non-Error Fail", () => { assertThrows( () => { assertThrows( () => { throw "Panic!"; }, String, "Panic!", ); }, AssertionError, "A non-Error object was thrown.", );
assertThrows( () => { assertThrows(() => { throw null; }); }, AssertionError, "A non-Error object was thrown.", );
assertThrows( () => { assertThrows(() => { throw undefined; }); }, AssertionError, "A non-Error object was thrown.", );});
Deno.test("Assert Throws Async Non-Error Fail", () => { assertThrowsAsync( () => { return assertThrowsAsync( () => { return Promise.reject("Panic!"); }, String, "Panic!", ); }, AssertionError, "A non-Error object was thrown or rejected.", );
assertThrowsAsync( () => { return assertThrowsAsync(() => { return Promise.reject(null); }); }, AssertionError, "A non-Error object was thrown or rejected.", );
assertThrowsAsync( () => { return assertThrowsAsync(() => { return Promise.reject(undefined); }); }, AssertionError, "A non-Error object was thrown or rejected.", );
assertThrowsAsync( () => { return assertThrowsAsync(() => { throw undefined; }); }, AssertionError, "A non-Error object was thrown or rejected.", );});
Deno.test("assertEquals diff for differently ordered objects", () => { assertThrows( () => { assertEquals( { aaaaaaaaaaaaaaaaaaaaaaaa: 0, bbbbbbbbbbbbbbbbbbbbbbbb: 0, ccccccccccccccccccccccc: 0, }, { ccccccccccccccccccccccc: 1, aaaaaaaaaaaaaaaaaaaaaaaa: 0, bbbbbbbbbbbbbbbbbbbbbbbb: 0, }, ); }, AssertionError, ` { aaaaaaaaaaaaaaaaaaaaaaaa: 0, bbbbbbbbbbbbbbbbbbbbbbbb: 0,- ccccccccccccccccccccccc: 0,+ ccccccccccccccccccccccc: 1, }`, );});
// 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",]`, );});
Deno.test("Assert Throws Parent Error", () => { assertThrows( () => { throw new AssertionError("Fail!"); }, Error, "Fail!", );});
Deno.test("Assert Throws Async Parent Error", () => { assertThrowsAsync( () => { throw new AssertionError("Fail!"); }, Error, "Fail!", );});