Skip to main content
Module

std/testing/asserts_test.ts

Deno standard library
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { assert, assertAlmostEquals, assertArrayIncludes, assertEquals, assertExists, assertFalse, assertInstanceOf, AssertionError, assertIsError, assertMatch, assertNotEquals, assertNotMatch, assertNotStrictEquals, assertObjectMatch, assertRejects, assertStrictEquals, assertStringIncludes, assertThrows, equal, fail, unimplemented, unreachable,} from "./asserts.ts";import { bold, gray, green, red, stripColor, yellow } from "../fmt/colors.ts";
Deno.test("EqualDifferentZero", () => { assert(equal(0, -0)); assert(equal(0, +0)); assert(equal(+0, -0)); assert(equal([0], [-0])); assert(equal(["hello", 12.21, 0], ["hello", 12.21, -0])); assert(equal(["hello", 12.21, 0], ["hello", 12.21, +0])); assert(equal(["hello", 12.21, -0], ["hello", 12.21, +0])); assert(equal({ msg: "hello", case: 0 }, { msg: "hello", case: -0 })); assert(equal({ msg: "hello", array: [0] }, { msg: "hello", array: [-0] }));});
Deno.test("Equal", function (): void { assert(equal("world", "world")); assert(!equal("hello", "world")); assertFalse(equal("hello", "world")); assert(equal(5, 5)); assert(!equal(5, 6)); assertFalse(equal(5, 6)); assert(equal(NaN, NaN)); assert(equal({ hello: "world" }, { hello: "world" })); assert(!equal({ world: "hello" }, { hello: "world" })); assertFalse(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" } }, ), ); assertFalse( equal( { hello: "world", hi: { there: "everyone" } }, { hello: "world", hi: { there: "everyone else" } }, ), ); assert(equal({ [Symbol.for("foo")]: "bar" }, { [Symbol.for("foo")]: "bar" })); assert(!equal({ [Symbol("foo")]: "bar" }, { [Symbol("foo")]: "bar" })); assertFalse(equal({ [Symbol("foo")]: "bar" }, { [Symbol("foo")]: "bar" }));
assert(equal(/deno/, /deno/)); assert(!equal(/deno/, /node/)); assertFalse(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))); assertFalse(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), ), ); assertFalse( 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))); assertFalse(equal(new Date("Invalid"), new Date(2019, 0, 3))); assert(!equal(new Date("Invalid"), new Date(2019, 0, 3, 4, 20, 1, 10))); assertFalse(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]))); assertFalse(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]))); assertFalse(equal(new Set([1, 2]), new Set([3, 2, 1]))); assert(!equal(new Set([1, 2, 3]), new Set([4, 5, 6]))); assertFalse(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"]]))); assertFalse(equal(new Map([["foo", "bar"]]), new Map([["bar", "baz"]]))); assertFalse(equal(new Map([["foo", "bar"]]), new Map([["bar", "baz"]]))); assert( !equal( new Map([["foo", "bar"]]), new Map([ ["foo", "bar"], ["bar", "baz"], ]), ), ); assertFalse( 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]]))); assertFalse(equal(new Map([[{ x: 1 }, true]]), new Map([[{ x: 1 }, false]]))); assert(!equal(new Map([[{ x: 1 }, true]]), new Map([[{ x: 2 }, true]]))); assertFalse(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])); assertFalse(equal([1, 2, 3, 4], [1, 2, 3])); assert(!equal([1, 2, 3, 4], [1, 2, 3])); assertFalse(equal([1, 2, 3, 4], [1, 2, 3])); assert(!equal([1, 2, 3, 4], [1, 4, 2, 3])); assertFalse(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]))); assertFalse( 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"), ), ); assertFalse( equal( new URL("https://example.test"), new URL("https://example.test/with-path"), ), ); assert( !equal({ a: undefined, b: undefined }, { a: undefined, c: undefined }), ); assertFalse( equal({ a: undefined, b: undefined }, { a: undefined, c: undefined }), ); assertFalse(equal({ a: undefined, b: undefined }, { a: undefined })); assertThrows(() => equal(new WeakMap(), new WeakMap())); assertThrows(() => equal(new WeakSet(), new WeakSet())); assert(!equal(new WeakMap(), new WeakSet())); assertFalse(equal(new WeakMap(), new WeakSet())); assert( equal(new WeakRef({ hello: "world" }), new WeakRef({ hello: "world" })), ); assert( !equal(new WeakRef({ world: "hello" }), new WeakRef({ hello: "world" })), ); assertFalse( equal(new WeakRef({ world: "hello" }), new WeakRef({ hello: "world" })), ); assert(!equal({ hello: "world" }, new WeakRef({ hello: "world" }))); assertFalse(equal({ hello: "world" }, new WeakRef({ hello: "world" }))); assert( !equal( new WeakRef({ hello: "world" }), // deno-lint-ignore ban-types new (class<T extends object> extends WeakRef<T> {})({ hello: "world" }), ), ); assertFalse( equal( new WeakRef({ hello: "world" }), // deno-lint-ignore ban-types new (class<T extends object> extends WeakRef<T> {})({ hello: "world" }), ), ); assert( !equal( new WeakRef({ hello: "world" }), // deno-lint-ignore ban-types new (class<T extends object> extends WeakRef<T> { foo = "bar"; })({ hello: "world" }), ), ); assertFalse( equal( new WeakRef({ hello: "world" }), // deno-lint-ignore ban-types new (class<T extends object> extends WeakRef<T> { foo = "bar"; })({ hello: "world" }), ), );
assert( !equal( new (class A { private hello = "world"; })(), new (class B { private hello = "world"; })(), ), );
assertFalse( equal( new (class A { private hello = "world"; })(), new (class B { private hello = "world"; })(), ), );});
Deno.test("EqualCircular", () => { const objA: { prop?: unknown } = {}; objA.prop = objA; const objB: { prop?: unknown } = {}; objB.prop = objB; assert(equal(objA, objB));
const mapA = new Map(); mapA.set("prop", mapA); const mapB = new Map(); mapB.set("prop", mapB); assert(equal(mapA, mapB));});
Deno.test("NotEquals", 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("AssertExists", function (): void { assertExists("Denosaurus"); assertExists(false); assertExists(0); assertExists(""); assertExists(-0); assertExists(0); assertExists(NaN);
const value = new URLSearchParams({ value: "test" }).get("value"); assertExists(value); assertEquals(value.length, 4);
let didThrow; try { assertExists(undefined); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true); didThrow = false; try { assertExists(null); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true);});
Deno.test("AssertStringContains", function (): void { assertStringIncludes("Denosaurus", "saur"); assertStringIncludes("Denosaurus", "Deno"); assertStringIncludes("Denosaurus", "rus"); let didThrow; try { assertStringIncludes("Denosaurus", "Raptor"); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true);});
Deno.test("ArrayContains", function (): void { const fixture = ["deno", "iz", "luv"]; const fixtureObject = [{ deno: "luv" }, { deno: "Js" }]; assertArrayIncludes(fixture, ["deno"]); assertArrayIncludes(fixtureObject, [{ deno: "luv" }]); assertArrayIncludes( Uint8Array.from([1, 2, 3, 4]), Uint8Array.from([1, 2, 3]), ); assertThrows( (): void => assertArrayIncludes(fixtureObject, [{ deno: "node" }]), AssertionError, `actual: "[ { deno: "luv", }, { deno: "Js", },]" expected to include: "[ { deno: "node", },]"missing: [ { deno: "node", },]`, );});
Deno.test("AssertStringContainsThrow", function (): void { let didThrow = false; try { assertStringIncludes("Denosaurus from Jurassic", "Raptor"); } catch (e) { assert(e instanceof AssertionError); assert( e.message === `actual: "Denosaurus from Jurassic" expected to contain: "Raptor"`, ); didThrow = true; } assert(didThrow);});
Deno.test("AssertStringMatching", function (): void { assertMatch("foobar@deno.com", RegExp(/[a-zA-Z]+@[a-zA-Z]+.com/));});
Deno.test("AssertStringMatchingThrows", function (): void { let didThrow = false; try { assertMatch("Denosaurus from Jurassic", RegExp(/Raptor/)); } catch (e) { assert(e instanceof AssertionError); assert( e.message === `actual: "Denosaurus from Jurassic" expected to match: "/Raptor/"`, ); didThrow = true; } assert(didThrow);});
Deno.test("AssertStringNotMatching", function (): void { assertNotMatch("foobar.deno.com", RegExp(/[a-zA-Z]+@[a-zA-Z]+.com/));});
Deno.test("AssertStringNotMatchingThrows", function (): void { let didThrow = false; try { assertNotMatch("Denosaurus from Jurassic", RegExp(/from/)); } catch (e) { assert(e instanceof AssertionError); assert( e.message === `actual: "Denosaurus from Jurassic" expected to not match: "/from/"`, ); didThrow = true; } assert(didThrow);});
Deno.test("AssertObjectMatching", function (): void { const sym = Symbol("foo"); const a = { foo: true, bar: false }; const b = { ...a, baz: a }; const c = { ...b, qux: b }; const d = { corge: c, grault: c }; const e = { foo: true } as { [key: string]: unknown }; e.bar = e; const f = { [sym]: true, bar: false }; interface r { foo: boolean; bar: boolean; } const g: r = { foo: true, bar: false }; const h = { foo: [1, 2, 3], bar: true }; const i = { foo: [a, e], bar: true }; const j = { foo: [[1, 2, 3]], bar: true }; const k = { foo: [[1, [2, [3]]]], bar: true }; const l = { foo: [[1, [2, [a, e, j, k]]]], bar: true }; const m = { foo: /abc+/i, bar: [/abc/g, /abc/m] }; const n = { foo: new Set(["foo", "bar"]), bar: new Map([ ["foo", 1], ["bar", 2], ]), baz: new Map([ ["a", a], ["b", b], ]), };
// Simple subset assertObjectMatch(a, { foo: true, }); // Subset with another subset assertObjectMatch(b, { foo: true, baz: { bar: false }, }); // Subset with multiple subsets assertObjectMatch(c, { foo: true, baz: { bar: false }, qux: { baz: { foo: true }, }, }); // Subset with same object reference as subset assertObjectMatch(d, { corge: { foo: true, qux: { bar: false }, }, grault: { bar: false, qux: { foo: true }, }, }); // Subset with circular reference assertObjectMatch(e, { foo: true, bar: { bar: { bar: { foo: true, }, }, }, }); // Subset with interface assertObjectMatch(g, { bar: false }); // Subset with same symbol assertObjectMatch(f, { [sym]: true, }); // Subset with array inside assertObjectMatch(h, { foo: [] }); assertObjectMatch(h, { foo: [1, 2] }); assertObjectMatch(h, { foo: [1, 2, 3] }); assertObjectMatch(i, { foo: [{ bar: false }] }); assertObjectMatch(i, { foo: [{ bar: false }, { bar: { bar: { bar: { foo: true } } } }], }); // Subset with nested array inside assertObjectMatch(j, { foo: [[1, 2, 3]] }); assertObjectMatch(k, { foo: [[1, [2, [3]]]] }); assertObjectMatch(l, { foo: [[1, [2, [a, e, j, k]]]] }); // Regexp assertObjectMatch(m, { foo: /abc+/i }); assertObjectMatch(m, { bar: [/abc/g, /abc/m] }); //Built-in data structures assertObjectMatch(n, { foo: new Set(["foo"]) }); assertObjectMatch(n, { bar: new Map([["bar", 2]]) }); assertObjectMatch(n, { baz: new Map([["b", b]]) }); assertObjectMatch(n, { baz: new Map([["b", { foo: true }]]) });
// Missing key { let didThrow; try { assertObjectMatch( { foo: true, }, { foo: true, bar: false, }, ); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true); } // Simple subset { let didThrow; try { assertObjectMatch(a, { foo: false, }); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true); } // Subset with another subset { let didThrow; try { assertObjectMatch(b, { foo: true, baz: { bar: true }, }); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true); } // Subset with multiple subsets { let didThrow; try { assertObjectMatch(c, { foo: true, baz: { bar: false }, qux: { baz: { foo: false }, }, }); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true); } // Subset with same object reference as subset { let didThrow; try { assertObjectMatch(d, { corge: { foo: true, qux: { bar: true }, }, grault: { bar: false, qux: { foo: false }, }, }); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true); } // Subset with circular reference { let didThrow; try { assertObjectMatch(e, { foo: true, bar: { bar: { bar: { foo: false, }, }, }, }); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true); } // Subset with symbol key but with string key subset { let didThrow; try { assertObjectMatch(f, { foo: true, }); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true); } // Subset with array inside but doesn't match key subset { let didThrow; try { assertObjectMatch(i, { foo: [1, 2, 3, 4], }); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true); } { let didThrow; try { assertObjectMatch(i, { foo: [{ bar: true }, { foo: false }], }); didThrow = false; } catch (e) { assert(e instanceof AssertionError); didThrow = true; } assertEquals(didThrow, true); } // actual/expected value as instance of class { class A { a: number; constructor(a: number) { this.a = a; } } assertObjectMatch({ test: new A(1) }, { test: { a: 1 } }); assertObjectMatch({ test: { a: 1 } }, { test: { a: 1 } }); assertObjectMatch({ test: { a: 1 } }, { test: new A(1) }); assertObjectMatch({ test: new A(1) }, { test: new A(1) }); } { // actual/expected contains same instance of Map/TypedArray/etc const body = new Uint8Array([0, 1, 2]); assertObjectMatch({ body, foo: "foo" }, { body }); } { // match subsets of arrays assertObjectMatch( { positions: [[1, 2, 3, 4]] }, { positions: [[1, 2, 3]], }, ); } //Regexp assertThrows(() => assertObjectMatch(m, { foo: /abc+/ }), AssertionError); assertThrows(() => assertObjectMatch(m, { foo: /abc*/i }), AssertionError); assertThrows( () => assertObjectMatch(m, { bar: [/abc/m, /abc/g] }), AssertionError, ); //Built-in data structures assertThrows( () => assertObjectMatch(n, { foo: new Set(["baz"]) }), AssertionError, ); assertThrows( () => assertObjectMatch(n, { bar: new Map([["bar", 3]]) }), AssertionError, ); assertThrows( () => assertObjectMatch(n, { baz: new Map([["a", { baz: true }]]) }), AssertionError, );});
Deno.test("AssertsUnimplemented", function (): void { let didThrow = false; try { unimplemented(); } catch (e) { assert(e instanceof AssertionError); assert(e.message === "unimplemented"); didThrow = true; } assert(didThrow);});
Deno.test("AssertsUnreachable", function (): void { let didThrow = false; try { unreachable(); } catch (e) { assert(e instanceof AssertionError); assert(e.message === "unreachable"); didThrow = true; } assert(didThrow);});
Deno.test("AssertFail", function (): void { assertThrows(fail, AssertionError, "Failed assertion."); assertThrows( (): void => { fail("foo"); }, AssertionError, "Failed assertion: foo", );});
Deno.test("AssertFailWithWrongErrorClass", 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("AssertThrowsWithReturnType", () => { assertThrows(() => { throw new Error(); });});
Deno.test("AssertRejectsFailIfGivenFunctionIsSynchronousAndThrow", async () => { await assertRejects(() => assertRejects(() => { throw new Error(); }) );});
Deno.test("AssertRejectsFailWithRightMessageWhenFunctionIsSynchronousAndThrow", async () => { await assertRejects( () => assertRejects(() => { throw { wrong: "true" }; }), AssertionError, "Function throws when expected to reject.", );});
Deno.test("AssertRejectsWithReturnType", async () => { await assertRejects(() => { return Promise.reject(new Error()); });});
Deno.test("AssertThrowsWithErrorCallback", () => { assertThrows( () => { throw new AggregateError([new Error("foo"), new Error("bar")], "baz"); }, (error: Error) => { assert(error instanceof AggregateError); assertEquals(error.message, "baz"); assertEquals(error.errors.length, 2); assertStringIncludes(error.errors[0].stack, "Error: foo"); assertStringIncludes(error.errors[1].stack, "Error: bar"); }, );});
Deno.test("AssertRejectsWithErrorCallback", async () => { await assertRejects( () => { return Promise.reject( new AggregateError([new Error("foo"), new Error("bar")], "baz"), ); }, (error: Error) => { assert(error instanceof AggregateError); assertEquals(error.message, "baz"); assertEquals(error.errors.length, 2); assertStringIncludes(error.errors[0].stack, "Error: foo"); assertStringIncludes(error.errors[1].stack, "Error: bar"); }, );});
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 types test", fn(): void { const x = { number: 2 };
const y = x as Record<never, never>; const z = x as unknown;
// y.number; // ~~~~~~ // Property 'number' does not exist on type 'Record<never, never>'.deno-ts(2339)
assertStrictEquals(y, x); y.number; // ok
// z.number; // ~ // Object is of type 'unknown'.deno-ts(2571)
assertStrictEquals(z, x); z.number; // ok },});
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("assert almost equals number", () => { //Default precision assertAlmostEquals(-0, +0); assertAlmostEquals(Math.PI, Math.PI); assertAlmostEquals(0.1 + 0.2, 0.3); assertThrows(() => assertAlmostEquals(1, 2)); assertThrows(() => assertAlmostEquals(1, 1.1));
//Higher precision assertAlmostEquals(0.1 + 0.2, 0.3, 1e-16); assertThrows( () => assertAlmostEquals(0.1 + 0.2, 0.3, 1e-17), AssertionError, `"${ ( 0.1 + 0.2 ).toExponential() }" expected to be close to "${(0.3).toExponential()}"`, );
//Special cases assertAlmostEquals(Infinity, Infinity); assertThrows( () => assertAlmostEquals(0, Infinity), AssertionError, '"0" expected to be close to "Infinity"', ); assertThrows( () => assertAlmostEquals(-Infinity, +Infinity), AssertionError, '"-Infinity" expected to be close to "Infinity"', ); assertThrows( () => assertAlmostEquals(Infinity, NaN), AssertionError, '"Infinity" expected to be close to "NaN"', ); assertThrows( () => assertAlmostEquals(NaN, NaN), AssertionError, '"NaN" expected to be close to "NaN"', );});
Deno.test({ name: "assertInstanceOf", fn(): void { class TestClass1 {} class TestClass2 {}
// Regular types assertInstanceOf(new Date(), Date); assertInstanceOf(new Number(), Number); assertInstanceOf(Promise.resolve(), Promise); assertInstanceOf(new TestClass1(), TestClass1);
// Throwing cases assertThrows( () => assertInstanceOf(new Date(), RegExp), AssertionError, `Expected object to be an instance of "RegExp" but was "Date".`, ); assertThrows( () => assertInstanceOf(5, Date), AssertionError, `Expected object to be an instance of "Date" but was "number".`, ); assertThrows( () => assertInstanceOf(new TestClass1(), TestClass2), AssertionError, `Expected object to be an instance of "TestClass2" but was "TestClass1".`, );
// Custom message assertThrows( () => assertInstanceOf(new Date(), RegExp, "Custom message"), AssertionError, "Custom message", );
// Edge cases assertThrows( () => assertInstanceOf(5, Number), AssertionError, `Expected object to be an instance of "Number" but was "number".`, );
let TestClassWithSameName: new () => unknown; { class TestClass1 {} TestClassWithSameName = TestClass1; } assertThrows( () => assertInstanceOf(new TestClassWithSameName(), TestClass1), AssertionError, `Expected object to be an instance of "TestClass1".`, );
assertThrows( () => assertInstanceOf(TestClass1, TestClass1), AssertionError, `Expected object to be an instance of "TestClass1" but was not an instanced object.`, ); assertThrows( () => assertInstanceOf(() => {}, TestClass1), AssertionError, `Expected object to be an instance of "TestClass1" but was not an instanced object.`, ); assertThrows( () => assertInstanceOf(null, TestClass1), AssertionError, `Expected object to be an instance of "TestClass1" but was "null".`, ); assertThrows( () => assertInstanceOf(undefined, TestClass1), AssertionError, `Expected object to be an instance of "TestClass1" but was "undefined".`, ); assertThrows( () => assertInstanceOf({}, TestClass1), AssertionError, `Expected object to be an instance of "TestClass1" but was "Object".`, ); assertThrows( () => assertInstanceOf(Object.create(null), TestClass1), AssertionError, `Expected object to be an instance of "TestClass1" but was "Object".`, );
// Test TypeScript types functionality, wrapped in a function that never runs // deno-lint-ignore no-unused-vars function typeScriptTests() { class ClassWithProperty { property = "prop1"; } const testInstance = new ClassWithProperty() as unknown;
// @ts-expect-error: `testInstance` is `unknown` so setting its property before `assertInstanceOf` should give a type error. testInstance.property = "prop2";
assertInstanceOf(testInstance, ClassWithProperty);
// Now `testInstance` should be of type `ClassWithProperty` testInstance.property = "prop3";
let x = 5 as unknown;
// @ts-expect-error: `x` is `unknown` so adding to it shouldn't work x += 5; assertInstanceOf(x, Number);
// @ts-expect-error: `x` is now `Number` rather than `number`, so this should still give a type error. x += 5; } },});
Deno.test({ name: "assert* functions with specified type parameter", fn(): void { assertEquals<string>("hello", "hello"); assertNotEquals<number>(1, 2); assertArrayIncludes<boolean>([true, false], [true]); const value = { x: 1 }; assertStrictEquals<typeof value>(value, value); // deno-lint-ignore 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", async () => { await assertRejects( () => { return assertRejects( () => { return Promise.reject("Panic!"); }, String, "Panic!", ); }, AssertionError, "A non-Error object was rejected.", );
await assertRejects( () => { return assertRejects(() => { return Promise.reject(null); }); }, AssertionError, "A non-Error object was rejected.", );
await assertRejects( () => { return assertRejects(() => { return Promise.reject(undefined); }); }, AssertionError, "A non-Error object was rejected.", );
await assertRejects( () => { return assertRejects(() => { return Promise.reject(undefined); }); }, AssertionError, "A non-Error object was rejected.", );});
Deno.test( "assertEquals compares objects structurally if one object's constructor is undefined and the other is Object", () => { const a = Object.create(null); a.prop = "test"; const b = { prop: "test", };
assertEquals(a, b); assertEquals(b, a); },);
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, }`, );});
Deno.test("Assert Throws Parent Error", () => { assertThrows( () => { throw new AssertionError("Fail!"); }, Error, "Fail!", );});
Deno.test("Assert Throws Async Parent Error", async () => { await assertRejects( () => { return Promise.reject(new AssertionError("Fail!")); }, Error, "Fail!", );});
Deno.test( "Assert Throws Async promise rejected with custom Error", async () => { class CustomError extends Error {} class AnotherCustomError extends Error {} await assertRejects( () => assertRejects( () => Promise.reject(new AnotherCustomError("failed")), CustomError, "fail", ), AssertionError, 'Expected error to be instance of "CustomError", but was "AnotherCustomError".', ); },);
Deno.test("Assert Is Error Non-Error Fail", () => { assertThrows( () => assertIsError("Panic!", undefined, "Panic!"), AssertionError, `Expected "error" to be an Error object.`, );
assertThrows( () => assertIsError(null), AssertionError, `Expected "error" to be an Error object.`, );
assertThrows( () => assertIsError(undefined), AssertionError, `Expected "error" to be an Error object.`, );});
Deno.test("Assert Is Error Parent Error", () => { assertIsError(new AssertionError("Fail!"), Error, "Fail!");});
Deno.test("Assert Is Error with custom Error", () => { class CustomError extends Error {} class AnotherCustomError extends Error {} assertIsError(new CustomError("failed"), CustomError, "fail"); assertThrows( () => assertIsError(new AnotherCustomError("failed"), CustomError, "fail"), AssertionError, 'Expected error to be instance of "CustomError", but was "AnotherCustomError".', );});
Deno.test("Assert False with falsy values", () => { assertFalse(false); assertFalse(0); assertFalse(""); assertFalse(null); assertFalse(undefined);});
Deno.test("Assert False with truthy values", () => { assertThrows(() => assertFalse(true)); assertThrows(() => assertFalse(1)); assertThrows(() => assertFalse("a")); assertThrows(() => assertFalse({})); assertThrows(() => assertFalse([]));});