Skip to content
This repository has been archived by the owner on Apr 18, 2022. It is now read-only.

Commit

Permalink
Make void context suites extendable
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleJune committed Sep 14, 2020
1 parent 149352d commit 4c894e5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/[email protected]/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)
Expand All @@ -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
Expand All @@ -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/[email protected]/testing/asserts.ts";
import { User, getUser, resetUsers } from "./example/user.ts";

Expand Down
14 changes: 7 additions & 7 deletions example/user_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserSuiteContext> = new TestSuite({
const userSuite: TestSuite<UserContext> = new TestSuite({
name: "user",
beforeEach(context: UserSuiteContext) {
beforeEach(context: UserContext) {
context.user = new User("Kyle June");
},
afterEach() {
Expand All @@ -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<GetUserSuiteContext> = new TestSuite({
const getUserSuite: TestSuite<GetUserContext> = new TestSuite<GetUserContext>({
name: "getUser",
suite: userSuite,
});
Expand All @@ -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);
Expand Down
21 changes: 13 additions & 8 deletions test_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface TestDefinition<T> {
* The test suite that the test belongs to.
* Any option that is not specified will be inherited from the test suite.
*/
suite?: TestSuite<T>;
suite?: TestSuite<T> | TestSuite<Partial<T>> | TestSuite<void>;
/** Ignore test if set to true. */
ignore?: boolean;
/**
Expand Down Expand Up @@ -42,7 +42,7 @@ export interface TestSuiteDefinition<T> {
* 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<T> | TestSuite<Partial<T>>;
suite?: TestSuite<T> | TestSuite<Partial<T>> | TestSuite<void>;
/** Ignore all tests in suite if set to true. */
ignore?: boolean;
/**
Expand Down Expand Up @@ -140,7 +140,7 @@ export class TestSuite<T> {
/** 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<T>;
private context?: Partial<T>;
/**
* The parent test suite that the test suite belongs to.
* Any option that is not specified will be inherited from the parent test suite.
Expand Down Expand Up @@ -188,9 +188,9 @@ export class TestSuite<T> {
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<T>;
} else if (options.suite) {
this.suite = options.suite;
this.suite = options.suite as TestSuite<T>;
}
if (this.suite && this.suite.locked) {
throw new Error("cannot add to test suite starting another test suite");
Expand Down Expand Up @@ -258,7 +258,7 @@ export class TestSuite<T> {
| ((context: T) => Promise<void>),
): void;
static test<T>(
suite: TestSuite<T>,
suite: TestSuite<T> | TestSuite<Partial<T>> | TestSuite<void>,
name: string,
fn:
| (() => void)
Expand All @@ -268,7 +268,12 @@ export class TestSuite<T> {
): void;
static test<T>(options: TestDefinition<T>): void;
static test<T>(
a: string | TestDefinition<T> | TestSuite<T>,
a:
| string
| TestDefinition<T>
| TestSuite<T>
| TestSuite<Partial<T>>
| TestSuite<void>,
b?:
| string
| (() => void)
Expand All @@ -292,7 +297,7 @@ export class TestSuite<T> {
name: b as string,
fn: c as (this: T) => (void | Promise<void>),
};
const suite: TestSuite<T> = options.suite ?? globalSuite!;
const suite: TestSuite<T> = (options.suite ?? globalSuite!) as TestSuite<T>;
if (suite.locked) {
throw new Error("cannot add to test suite starting another test suite");
}
Expand Down

0 comments on commit 4c894e5

Please sign in to comment.