diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2019-10-10 05:31:23 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-10 05:31:23 -0400 |
commit | e7562eed8c816cd0d97aab6b818d7c8453dbaa2b (patch) | |
tree | c5a9f536e79d2c8d2d02897511a9138acaf35394 /std/testing/test.ts | |
parent | 3882c9d19a641e0c919f1350d87c6d7ee280cf78 (diff) | |
parent | 93f7f00c956c14620ef031626f124b57397ca867 (diff) |
Merge deno_std in main repo (#3091)
The history of deno_std is persevered but rewritten to update links to issues and PRs
Fixes denoland/deno_std#603
Diffstat (limited to 'std/testing/test.ts')
m--------- | std | 0 | ||||
-rw-r--r-- | std/testing/test.ts | 267 |
2 files changed, 267 insertions, 0 deletions
diff --git a/std b/std deleted file mode 160000 -Subproject 43aafbf33285753e7b42230f0eb7969b300f71c diff --git a/std/testing/test.ts b/std/testing/test.ts new file mode 100644 index 000000000..93233f7cc --- /dev/null +++ b/std/testing/test.ts @@ -0,0 +1,267 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import { test, runIfMain } from "./mod.ts"; +import { + assert, + assertEquals, + assertStrictEq, + assertThrows, + assertThrowsAsync +} from "./asserts.ts"; + +test(function testingAssertEqualActualUncoercable(): void { + let didThrow = false; + const a = Object.create(null); + try { + assertEquals(a, "bar"); + } catch (e) { + didThrow = true; + } + assert(didThrow); +}); + +test(function testingAssertEqualExpectedUncoercable(): void { + let didThrow = false; + const a = Object.create(null); + try { + assertStrictEq("bar", a); + } catch (e) { + didThrow = true; + } + assert(didThrow); +}); + +test(function testingAssertStrictEqual(): void { + const a = {}; + const b = a; + assertStrictEq(a, b); +}); + +test(function testingAssertNotStrictEqual(): void { + let didThrow = false; + const a = {}; + const b = {}; + try { + assertStrictEq(a, b); + } catch (e) { + assert(e.message === "actual: [object Object] expected: [object Object]"); + didThrow = true; + } + assert(didThrow); +}); + +test(function testingDoesThrow(): void { + let count = 0; + assertThrows( + (): void => { + count++; + throw new Error(); + } + ); + assert(count === 1); +}); + +test(function testingDoesNotThrow(): void { + let count = 0; + let didThrow = false; + try { + assertThrows( + (): void => { + count++; + console.log("Hello world"); + } + ); + } catch (e) { + assert(e.message === "Expected function to throw."); + didThrow = true; + } + assert(count === 1); + assert(didThrow); +}); + +test(function testingThrowsErrorType(): void { + let count = 0; + assertThrows((): void => { + count++; + throw new TypeError(); + }, TypeError); + assert(count === 1); +}); + +test(function testingThrowsNotErrorType(): void { + let count = 0; + let didThrow = false; + try { + assertThrows((): void => { + count++; + throw new TypeError(); + }, RangeError); + } catch (e) { + assert(e.message === `Expected error to be instance of "RangeError".`); + didThrow = true; + } + assert(count === 1); + assert(didThrow); +}); + +test(function testingThrowsMsgIncludes(): void { + let count = 0; + assertThrows( + (): void => { + count++; + throw new TypeError("Hello world!"); + }, + TypeError, + "world" + ); + assert(count === 1); +}); + +test(function testingThrowsMsgNotIncludes(): void { + let count = 0; + let didThrow = false; + try { + assertThrows( + (): void => { + count++; + throw new TypeError("Hello world!"); + }, + TypeError, + "foobar" + ); + } catch (e) { + assert( + e.message === + `Expected error message to include "foobar", but got "Hello world!".` + ); + didThrow = true; + } + assert(count === 1); + assert(didThrow); +}); + +test(async function testingDoesThrowAsync(): Promise<void> { + let count = 0; + await assertThrowsAsync( + async (): Promise<void> => { + count++; + throw new Error(); + } + ); + assert(count === 1); +}); + +test(async function testingDoesReject(): Promise<void> { + let count = 0; + await assertThrowsAsync( + (): Promise<never> => { + count++; + return Promise.reject(new Error()); + } + ); + assert(count === 1); +}); + +test(async function testingDoesNotThrowAsync(): Promise<void> { + let count = 0; + let didThrow = false; + try { + await assertThrowsAsync( + async (): Promise<void> => { + count++; + console.log("Hello world"); + } + ); + } catch (e) { + assert(e.message === "Expected function to throw."); + didThrow = true; + } + assert(count === 1); + assert(didThrow); +}); + +test(async function testingDoesNotRejectAsync(): Promise<void> { + let count = 0; + let didThrow = false; + try { + await assertThrowsAsync( + (): Promise<void> => { + count++; + console.log("Hello world"); + return Promise.resolve(); + } + ); + } catch (e) { + assert(e.message === "Expected function to throw."); + didThrow = true; + } + assert(count === 1); + assert(didThrow); +}); + +test(async function testingThrowsAsyncErrorType(): Promise<void> { + let count = 0; + await assertThrowsAsync((): Promise<void> => { + count++; + throw new TypeError(); + }, TypeError); + assert(count === 1); +}); + +test(async function testingThrowsAsyncNotErrorType(): Promise<void> { + let count = 0; + let didThrow = false; + try { + await assertThrowsAsync(async (): Promise<void> => { + count++; + throw new TypeError(); + }, RangeError); + } catch (e) { + assert(e.message === `Expected error to be instance of "RangeError".`); + didThrow = true; + } + assert(count === 1); + assert(didThrow); +}); + +test(async function testingThrowsAsyncMsgIncludes(): Promise<void> { + let count = 0; + await assertThrowsAsync( + async (): Promise<void> => { + count++; + throw new TypeError("Hello world!"); + }, + TypeError, + "world" + ); + assert(count === 1); +}); + +test(async function testingThrowsAsyncMsgNotIncludes(): Promise<void> { + let count = 0; + let didThrow = false; + try { + await assertThrowsAsync( + async (): Promise<void> => { + count++; + throw new TypeError("Hello world!"); + }, + TypeError, + "foobar" + ); + } catch (e) { + assert( + e.message === + `Expected error message to include "foobar", but got "Hello world!".` + ); + didThrow = true; + } + assert(count === 1); + assert(didThrow); +}); + +test("test fn overloading", (): void => { + // just verifying that you can use this test definition syntax + assert(true); +}); + +runIfMain(import.meta); |