From 151ce0266eb4de2c8fc600c81c192a5f791b6169 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 9 Oct 2019 17:10:09 -0400 Subject: Move everything into std subdir --- std/testing/test.ts | 263 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 263 insertions(+) create mode 100644 std/testing/test.ts (limited to 'std/testing/test.ts') diff --git a/std/testing/test.ts b/std/testing/test.ts new file mode 100644 index 000000000..dd6d772f6 --- /dev/null +++ b/std/testing/test.ts @@ -0,0 +1,263 @@ +// 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 { + let count = 0; + await assertThrowsAsync( + async (): Promise => { + count++; + throw new Error(); + } + ); + assert(count === 1); +}); + +test(async function testingDoesReject(): Promise { + let count = 0; + await assertThrowsAsync( + (): Promise => { + count++; + return Promise.reject(new Error()); + } + ); + assert(count === 1); +}); + +test(async function testingDoesNotThrowAsync(): Promise { + let count = 0; + let didThrow = false; + try { + await assertThrowsAsync( + async (): Promise => { + 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 { + let count = 0; + let didThrow = false; + try { + await assertThrowsAsync( + (): Promise => { + 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 { + let count = 0; + await assertThrowsAsync((): Promise => { + count++; + throw new TypeError(); + }, TypeError); + assert(count === 1); +}); + +test(async function testingThrowsAsyncNotErrorType(): Promise { + let count = 0; + let didThrow = false; + try { + await assertThrowsAsync(async (): Promise => { + 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 { + let count = 0; + await assertThrowsAsync( + async (): Promise => { + count++; + throw new TypeError("Hello world!"); + }, + TypeError, + "world" + ); + assert(count === 1); +}); + +test(async function testingThrowsAsyncMsgNotIncludes(): Promise { + let count = 0; + let didThrow = false; + try { + await assertThrowsAsync( + async (): Promise => { + 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); -- cgit v1.2.3 From 93f7f00c956c14620ef031626f124b57397ca867 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 9 Oct 2019 17:22:22 -0400 Subject: Run deno_std tests in github actions --- std/testing/test.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'std/testing/test.ts') diff --git a/std/testing/test.ts b/std/testing/test.ts index dd6d772f6..93233f7cc 100644 --- a/std/testing/test.ts +++ b/std/testing/test.ts @@ -51,10 +51,12 @@ test(function testingAssertNotStrictEqual(): void { test(function testingDoesThrow(): void { let count = 0; - assertThrows((): void => { - count++; - throw new Error(); - }); + assertThrows( + (): void => { + count++; + throw new Error(); + } + ); assert(count === 1); }); @@ -62,10 +64,12 @@ test(function testingDoesNotThrow(): void { let count = 0; let didThrow = false; try { - assertThrows((): void => { - count++; - console.log("Hello world"); - }); + assertThrows( + (): void => { + count++; + console.log("Hello world"); + } + ); } catch (e) { assert(e.message === "Expected function to throw."); didThrow = true; -- cgit v1.2.3