Skip to main content
Module

x/expect/expect_test.ts

helpers for writing jest like expect tests in deno
Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
import { assertEquals, AssertionError, assertThrows,} from "https://deno.land/std@0.97.0/testing/asserts.ts";
import { addMatchers, expect } from "./expect.ts";import * as mock from "./mock.ts";
async function assertPass(fn: Function) { try { assertEquals(await fn(), undefined); } catch (err) { throw new AssertionError( `expected ${fn.toString()} to pass but it failed with ${err}`, ); }}
async function assertAllPass(...fns: Function[]) { for (let fn of fns) { await assertPass(fn); }}
async function assertFail(fn: Function) { let thrown = true; try { let resolution = await fn(); thrown = false; throw new AssertionError( `expected ${fn.toString()} to throw but it resolved with ${resolution}`, ); } catch (err) { // expected if (!thrown) throw err; }}
async function assertAllFail(...fns: Function[]) { for (let fn of fns) { await assertFail(fn); }}
Deno.test({ name: "exportsFunction", fn: () => { assertEquals(typeof expect, "function"); },});
Deno.test({ name: "throwsWhenNoMatcherFound", fn: () => { assertThrows( //@ts-ignore () => expect(true).toBeFancy(), TypeError, "matcher not found: toBeFancy", ); },});
Deno.test({ name: "allowsExtendingMatchers", fn: () => { addMatchers({ toBeFancy(value: any) { if (value === "fancy") { return { pass: true }; } else { return { pass: false, message: "was not fancy" }; } }, });
// @ts-ignore assertPass(() => expect("fancy").toBeFancy()); },});
Deno.test({ name: "toBe", fn: async () => { const obj = {}; assertEquals(typeof expect(obj).toBe, "function"); await assertAllPass( () => expect(obj).toBe(obj), () => expect(obj).not.toBe({}), () => expect(Promise.resolve(1)).resolves.toBe(1), () => expect(Promise.reject(1)).rejects.toBe(1), );
await assertFail(() => expect(obj).toBe({})); await assertFail(() => expect(obj).not.toBe(obj)); },});
Deno.test({ name: "toEqual", fn: async () => { const obj = {};
await assertAllPass( () => expect(1).toEqual(1), () => expect(obj).toEqual({}), () => expect(obj).toEqual(obj), () => expect({ a: 1 }).toEqual({ a: 1 }), () => expect([1]).toEqual([1]), () => expect(Promise.resolve(1)).resolves.toEqual(1), () => expect(Promise.reject(1)).rejects.toEqual(1), );
await assertAllFail( () => expect(1).toEqual(2), () => expect(1).toEqual(true), () => expect({}).toEqual(true), () => expect(1).not.toEqual(1), () => expect(true).not.toEqual(true), ); },});
Deno.test({ name: "resolves", fn: async () => { const resolves = expect(Promise.resolve(true)).resolves; for (let method of ["toEqual", "toBe", "toBeTruthy", "toBeFalsy"]) { assertEquals( typeof (resolves as any)[method], "function", `missing ${method}`, ); } },});
Deno.test({ name: "rejects", fn: async () => { const rejects = expect(Promise.reject(true)).rejects; for ( let method of ["toEqual", "toBe", "toBeTruthy", "toBeFalsy"] ) { assertEquals(typeof (rejects as any)[method], "function"); } },});
Deno.test({ name: "toBeDefined", fn: async () => { await assertAllPass( () => expect(true).toBeDefined(), () => expect({}).toBeDefined(), () => expect([]).toBeDefined(), () => expect(undefined).not.toBeDefined(), () => expect(Promise.resolve({})).resolves.toBeDefined(), () => expect(Promise.reject({})).rejects.toBeDefined(), );
await assertAllFail( () => expect(undefined).toBeDefined(), () => expect(true).not.toBeDefined(), ); },});
Deno.test({ name: "toBeUndefined", fn: async () => { await assertAllPass( () => expect(undefined).toBeUndefined(), () => expect(null).not.toBeUndefined(), () => expect(Promise.resolve(undefined)).resolves.toBeUndefined(), () => expect(Promise.reject(undefined)).rejects.toBeUndefined(), );
await assertAllFail( () => expect(null).toBeUndefined(), () => expect(undefined).not.toBeUndefined(), () => expect(false).toBeUndefined(), ); },});
Deno.test({ name: "toBeTruthy", fn: async () => { await assertAllPass( () => expect(true).toBeTruthy(), () => expect(false).not.toBeTruthy(), () => expect(Promise.resolve(true)).resolves.toBeTruthy(), () => expect(Promise.reject(true)).rejects.toBeTruthy(), );
await assertAllFail( () => expect(false).toBeTruthy(), () => expect(true).not.toBeTruthy(), ); },});
Deno.test({ name: "toBeFalsy", fn: async () => { await assertAllPass( () => expect(false).toBeFalsy(), () => expect(true).not.toBeFalsy(), () => expect(Promise.resolve(false)).resolves.toBeFalsy(), () => expect(Promise.reject(false)).rejects.toBeFalsy(), ); await assertAllFail( () => expect(true).toBeFalsy(), () => expect(false).not.toBeFalsy(), ); },});
Deno.test({ name: "toBeGreaterThan", fn: async () => { await assertAllPass( () => expect(2).toBeGreaterThan(1), () => expect(1).not.toBeGreaterThan(2), () => expect(Promise.resolve(2)).resolves.toBeGreaterThan(1), () => expect(Promise.reject(2)).rejects.toBeGreaterThan(1), );
await assertAllFail( () => expect(1).toBeGreaterThan(1), () => expect(1).toBeGreaterThan(2), () => expect(2).not.toBeGreaterThan(1), ); },});
Deno.test({ name: "toBeLessThan", fn: async () => { await assertAllPass( () => expect(1).toBeLessThan(2), () => expect(2).not.toBeLessThan(1), () => expect(Promise.resolve(1)).resolves.toBeLessThan(2), () => expect(Promise.reject(1)).rejects.toBeLessThan(2), );
await assertAllFail( () => expect(1).toBeLessThan(1), () => expect(2).toBeLessThan(1), () => expect(1).not.toBeLessThan(2), ); },});
Deno.test({ name: "toBeGreaterThanOrEqual", fn: async () => { await assertAllPass( () => expect(2).toBeGreaterThanOrEqual(1), () => expect(1).toBeGreaterThanOrEqual(1), () => expect(1).not.toBeGreaterThanOrEqual(2), () => expect(Promise.resolve(2)).resolves.toBeGreaterThanOrEqual(2), () => expect(Promise.reject(2)).rejects.toBeGreaterThanOrEqual(2), );
await assertAllFail( () => expect(1).toBeGreaterThanOrEqual(2), () => expect(2).not.toBeGreaterThanOrEqual(1), ); },});
Deno.test({ name: "toBeLessThanOrEqual", fn: async () => { await assertAllPass( () => expect(1).toBeLessThanOrEqual(2), () => expect(1).toBeLessThanOrEqual(1), () => expect(2).not.toBeLessThanOrEqual(1), () => expect(Promise.resolve(1)).resolves.toBeLessThanOrEqual(2), () => expect(Promise.reject(1)).rejects.toBeLessThanOrEqual(2), ); await assertAllFail( () => expect(2).toBeLessThanOrEqual(1), () => expect(1).not.toBeLessThanOrEqual(1), () => expect(1).not.toBeLessThanOrEqual(2), ); },});
Deno.test({ name: "toBeNull", fn: async () => { await assertAllPass( () => expect(null).toBeNull(), () => expect(undefined).not.toBeNull(), () => expect(false).not.toBeNull(), () => expect(Promise.resolve(null)).resolves.toBeNull(), () => expect(Promise.reject(null)).rejects.toBeNull(), );
await assertAllFail( () => expect({}).toBeNull(), () => expect(null).not.toBeNull(), () => expect(undefined).toBeNull(), ); },});
Deno.test({ name: "toBeInstanceOf", fn: async () => { class A {} class B {}
await assertAllPass( () => expect(new A()).toBeInstanceOf(A), () => expect(new A()).not.toBeInstanceOf(B), );
await assertAllFail( () => expect({}).toBeInstanceOf(A), () => expect(null).toBeInstanceOf(A), ); },});
Deno.test({ name: "toBeNaN", fn: async () => { await assertAllPass( () => expect(NaN).toBeNaN(), () => expect(10).not.toBeNaN(), () => expect(Promise.resolve(NaN)).resolves.toBeNaN(), );
await assertAllFail(() => expect(10).toBeNaN(), () => expect(10).toBeNaN()); },});
Deno.test({ name: "toBeMatch", fn: async () => { await assertAllPass( () => expect("hello").toMatch(/^hell/), () => expect("hello").toMatch("hello"), () => expect("hello").toMatch("hell"), );
await assertAllFail(() => expect("yo").toMatch(/^hell/)); },});
Deno.test({ name: "toHaveProperty", fn: async () => { await assertAllPass( () => expect({ a: "10" }).toHaveProperty("a"), () => expect({ a: "10" }).toHaveProperty("a", "10"), );
await assertAllFail( () => expect({ a: 1 }).toHaveProperty("b"), () => expect({ a: 1 }).toHaveProperty("a", 2), ); },});
Deno.test({ name: "toHaveLength", fn: async () => { await assertAllPass( () => expect([1, 2]).toHaveLength(2), () => expect({ length: 10 }).toHaveLength(10), );
await assertAllFail(() => expect([]).toHaveLength(10)); },});
Deno.test({ name: "toContain", fn: async () => { await assertAllPass( () => expect([1, 2, 3]).toContain(2), () => expect([]).not.toContain(2), );
await assertAllFail( () => expect([1, 2, 3]).toContain(4), () => expect([]).toContain(4), ); },});
Deno.test({ name: "toThrow", fn: async () => { await assertAllPass( () => expect(() => { throw new Error("TEST"); }).toThrow("TEST"), () => expect(Promise.reject(new Error("TEST"))).rejects.toThrow("TEST"), );
await assertAllFail(() => expect(() => true).toThrow()); },});
Deno.test({ name: "toHaveBeenCalled", fn: async () => { await assertAllPass( () => { const m = mock.fn(); m(10); m(20); expect(m).toHaveBeenCalled(); }, () => { const m = mock.fn(); expect(m).not.toHaveBeenCalled(); }, );
await assertAllFail(() => { const m = mock.fn(); expect(m).toHaveBeenCalled(); }); },});
Deno.test({ name: "toHaveBeenCalledTimes", fn: async () => { await assertAllPass( () => { const m = mock.fn(); expect(m).toHaveBeenCalledTimes(0); }, () => { const m = mock.fn(); m(); m(); expect(m).toHaveBeenCalledTimes(2); }, );
await assertAllFail( () => { const m = mock.fn(); expect(m).toHaveBeenCalledTimes(1); }, () => { const m = mock.fn(); m(); m(); expect(m).toHaveBeenCalledTimes(3); }, ); },});
Deno.test({ name: "toHaveBeenCalledWith", fn: async () => { await assertAllPass( () => { const m = mock.fn(); m(1, 2, 3); expect(m).toHaveBeenCalledWith(1, 2, 3); }, () => { const m = mock.fn(); m(1, 2, 3); m(2, 3, 4); expect(m).toHaveBeenCalledWith(1, 2, 3); expect(m).toHaveBeenCalledWith(2, 3, 4); }, );
await assertAllFail( () => { const m = mock.fn(); expect(m).toHaveBeenCalledWith(1); }, () => { const m = mock.fn(); m(2); expect(m).toHaveBeenCalledWith(1); }, ); },});
Deno.test({ name: "toHaveBeenLastCalledWith", fn: async () => { await assertAllPass( () => { const m = mock.fn(); m(1, 2, 3); expect(m).toHaveBeenLastCalledWith(1, 2, 3); }, () => { const m = mock.fn(); m(1, 2, 3); m(2, 3, 4); expect(m).not.toHaveBeenLastCalledWith(1, 2, 3); expect(m).toHaveBeenLastCalledWith(2, 3, 4); }, );
await assertAllFail( () => { const m = mock.fn(); expect(m).toHaveBeenLastCalledWith(1); }, () => { const m = mock.fn(); m(2); expect(m).toHaveBeenLastCalledWith(1); }, ); },});
Deno.test({ name: "toHaveBeenNthCalledWith", fn: async () => { await assertAllPass( () => { const m = mock.fn(); m(1, 2, 3); expect(m).toHaveBeenNthCalledWith(1, 1, 2, 3); }, () => { const m = mock.fn(); m(1, 2, 3); m(2, 3, 4); expect(m).not.toHaveBeenNthCalledWith(2, 1, 2, 3); expect(m).toHaveBeenNthCalledWith(2, 2, 3, 4); }, );
await assertAllFail( () => { const m = mock.fn(); expect(m).toHaveBeenNthCalledWith(1, 1, 2); }, () => { const m = mock.fn(); m(2); expect(m).toHaveBeenNthCalledWith(1, 1); }, ); },});
Deno.test({ name: "toHaveReturnedWith", fn: async () => { await assertAllPass( () => { const m = mock.fn(); m(); expect(m).toHaveReturnedWith(undefined); }, () => { const m = mock.fn(() => true); m(); expect(m).not.toHaveReturnedWith(false); expect(m).toHaveReturnedWith(true); }, () => { const m = mock.fn(() => { throw new Error("TEST"); }); try { m(); } catch (err) {} expect(m).not.toHaveReturnedWith(10); }, );
await assertAllFail( () => { const m = mock.fn(); expect(m).toHaveReturnedWith(1); }, () => { const m = mock.fn(); m(2); expect(m).toHaveReturnedWith(1); }, ); },});
Deno.test({ name: "toHaveReturnedTimes", fn: async () => { await assertAllPass( () => { const m = mock.fn(); m(); expect(m).toHaveReturnedTimes(1); }, () => { const m = mock.fn(() => true); expect(m).toHaveReturnedTimes(0); }, () => { const m = mock.fn(() => { throw new Error("TEST"); }); try { m(); } catch (err) {} expect(m).toHaveReturnedTimes(0); }, );
await assertAllFail( () => { const m = mock.fn(); expect(m).toHaveReturnedTimes(1); }, () => { const m = mock.fn(); m(2); expect(m).not.toHaveReturnedTimes(1); }, ); },});
Deno.test({ name: "toHaveReturned", fn: async () => { await assertAllPass( () => { const m = mock.fn(); m(); expect(m).toHaveReturned(); }, () => { const m = mock.fn(); expect(m).not.toHaveReturned(); }, () => { const m = mock.fn(() => { throw new Error("TEST"); }); try { m(); } catch (err) {} expect(m).not.toHaveReturned(); }, );
await assertAllFail( () => { const m = mock.fn(); expect(m).toHaveReturned(); }, () => { const m = mock.fn(); m(); expect(m).not.toHaveReturned(); }, () => { const m = mock.fn(() => { throw new Error("TEST"); }); m(); expect(m).not.toHaveReturned(); }, ); },});
Deno.test({ name: "toHaveLastReturnedWith", fn: async () => { await assertAllPass( () => { const m = mock.fn((x: number) => x); m(1); m(2); expect(m).toHaveLastReturnedWith(2); }, () => { const m = mock.fn((x: number) => x); m(1); m(2); expect(m).toHaveLastReturnedWith(2); }, );
await assertAllFail( () => { const m = mock.fn((x: number) => x); expect(m).toHaveLastReturnedWith(1); }, () => { const m = mock.fn((x: number) => x); m(2); expect(m).toHaveLastReturnedWith(1); }, ); },});
Deno.test({ name: "toHaveNthReturnedWith", fn: async () => { await assertAllPass( () => { const m = mock.fn((x: number) => x); m(1, 2, 3); expect(m).toHaveNthReturnedWith(1, 1); }, () => { const m = mock.fn((x: number) => x); m(1, 2, 3); m(2, 3, 4); expect(m).not.toHaveNthReturnedWith(2, 1); expect(m).toHaveNthReturnedWith(2, 2); }, );
await assertAllFail( () => { const m = mock.fn(); expect(m).toHaveNthReturnedWith(1, 1); }, () => { const m = mock.fn(); m(2); expect(m).toHaveNthReturnedWith(1, 1); }, ); },});