From 4c894e5a9690e18852e2c1e526daf680cdeeb643 Mon Sep 17 00:00:00 2001 From: Kyle June Date: Sun, 13 Sep 2020 21:10:01 -0500 Subject: [PATCH] Make void context suites extendable --- README.md | 10 +++++----- example/user_test.ts | 14 +++++++------- test_suite.ts | 21 +++++++++++++-------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index d2735d8..104506f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Test Suite -[![version](https://img.shields.io/badge/release-v0.4.0-success)](https://github.com/udibo/test_suite/tree/v0.4.0) +[![version](https://img.shields.io/badge/release-v0.5.0-success)](https://github.com/udibo/test_suite/tree/v0.5.0) [![deno doc](https://img.shields.io/badge/deno-doc-success?logo=deno)](https://doc.deno.land/https/deno.land/x/test_suite@v0.4.1/mod.ts) [![deno version](https://img.shields.io/badge/deno-v1.4.0-success?logo=deno)](https://github.com/denoland/deno/tree/v1.4.0) [![CI](https://github.com/udibo/mock/workflows/CI/badge.svg)](https://github.com/udibo/test_suite/actions?query=workflow%3ACI) @@ -23,16 +23,16 @@ but can also be imported directly from GitHub using raw content URLs. ```ts // Import from Deno's third party module registry -import { TestSuite, test } from "https://deno.land/x/test_suite@v0.4.0/mod.ts"; +import { TestSuite, test } from "https://deno.land/x/test_suite@v0.5.0/mod.ts"; // Import from GitHub -import { TestSuite, } "https://raw.githubusercontent.com/udibo/test_suite/v0.4.0/mod.ts"; +import { TestSuite, } "https://raw.githubusercontent.com/udibo/test_suite/v0.5.0/mod.ts"; ``` ## Usage Below are some examples of how to use TestSuite and test in tests. -See [deno docs](https://doc.deno.land/https/deno.land/x/test_suite@v0.4.0/mod.ts) for more information. +See [deno docs](https://doc.deno.land/https/deno.land/x/test_suite@v0.5.0/mod.ts) for more information. ### TestSuite @@ -47,7 +47,7 @@ The beforeEach and afterEach hook options are similar to beforeAll and afterAll The example test below can be found in the example directory. ```ts -import { TestSuite, test } from "https://deno.land/x/test_suite@v0.4.0/mod.ts"; +import { TestSuite, test } from "https://deno.land/x/test_suite@v0.5.0/mod.ts"; import { assertEquals } from "https://deno.land/std@0.69.0/testing/asserts.ts"; import { User, getUser, resetUsers } from "./example/user.ts"; diff --git a/example/user_test.ts b/example/user_test.ts index fa2f7a1..62627a9 100644 --- a/example/user_test.ts +++ b/example/user_test.ts @@ -2,12 +2,12 @@ import { TestSuite, test } from "../mod.ts"; import { assertEquals } from "../deps/std/testing/asserts.ts"; import { User, getUser, resetUsers } from "./user.ts"; -interface UserSuiteContext { +interface UserContext { user: User; } -const userSuite: TestSuite = new TestSuite({ +const userSuite: TestSuite = new TestSuite({ name: "user", - beforeEach(context: UserSuiteContext) { + beforeEach(context: UserContext) { context.user = new User("Kyle June"); }, afterEach() { @@ -20,11 +20,11 @@ test(userSuite, "create", () => { assertEquals(user.name, "John Doe"); }); -interface GetUserSuiteContext extends UserSuiteContext { +interface GetUserContext extends UserContext { value?: number; } -const getUserSuite: TestSuite = new TestSuite({ +const getUserSuite: TestSuite = new TestSuite({ name: "getUser", suite: userSuite, }); @@ -33,11 +33,11 @@ test(getUserSuite, "user does not exist", () => { assertEquals(getUser("John Doe"), undefined); }); -test(getUserSuite, "user exists", (context: UserSuiteContext) => { +test(getUserSuite, "user exists", (context: UserContext) => { assertEquals(getUser("Kyle June"), context.user); }); -test(userSuite, "resetUsers", (context: UserSuiteContext) => { +test(userSuite, "resetUsers", (context: UserContext) => { assertEquals(getUser("Kyle June"), context.user); resetUsers(); assertEquals(getUser("Kyle June"), undefined); diff --git a/test_suite.ts b/test_suite.ts index 2c0f381..4e7aa1d 100644 --- a/test_suite.ts +++ b/test_suite.ts @@ -13,7 +13,7 @@ export interface TestDefinition { * The test suite that the test belongs to. * Any option that is not specified will be inherited from the test suite. */ - suite?: TestSuite; + suite?: TestSuite | TestSuite> | TestSuite; /** Ignore test if set to true. */ ignore?: boolean; /** @@ -42,7 +42,7 @@ export interface TestSuiteDefinition { * The parent test suite that the test suite belongs to. * Any option that is not specified will be inherited from the parent test suite. */ - suite?: TestSuite | TestSuite>; + suite?: TestSuite | TestSuite> | TestSuite; /** Ignore all tests in suite if set to true. */ ignore?: boolean; /** @@ -140,7 +140,7 @@ export class TestSuite { /** The name of the test suite will be prepended to the names of tests in the suite. */ private name: string; /** The context for tests within the suite. */ - private context: Partial; + private context?: Partial; /** * The parent test suite that the test suite belongs to. * Any option that is not specified will be inherited from the parent test suite. @@ -188,9 +188,9 @@ export class TestSuite { throw new TypeError("name cannot start or end with a space"); } if (globalSuite) { - this.suite = options.suite ?? globalSuite; + this.suite = (options.suite ?? globalSuite) as TestSuite; } else if (options.suite) { - this.suite = options.suite; + this.suite = options.suite as TestSuite; } if (this.suite && this.suite.locked) { throw new Error("cannot add to test suite starting another test suite"); @@ -258,7 +258,7 @@ export class TestSuite { | ((context: T) => Promise), ): void; static test( - suite: TestSuite, + suite: TestSuite | TestSuite> | TestSuite, name: string, fn: | (() => void) @@ -268,7 +268,12 @@ export class TestSuite { ): void; static test(options: TestDefinition): void; static test( - a: string | TestDefinition | TestSuite, + a: + | string + | TestDefinition + | TestSuite + | TestSuite> + | TestSuite, b?: | string | (() => void) @@ -292,7 +297,7 @@ export class TestSuite { name: b as string, fn: c as (this: T) => (void | Promise), }; - const suite: TestSuite = options.suite ?? globalSuite!; + const suite: TestSuite = (options.suite ?? globalSuite!) as TestSuite; if (suite.locked) { throw new Error("cannot add to test suite starting another test suite"); }