Skip to content

Commit

Permalink
Add some tests for test function
Browse files Browse the repository at this point in the history
  • Loading branch information
sondr3 committed Jan 6, 2022
1 parent 65fb615 commit e7bec25
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
58 changes: 58 additions & 0 deletions lib/test.test.ts
@@ -0,0 +1,58 @@
import { strict as assert } from "node:assert";

import { test } from "./index.js";

// yo, i heard you like tests, so i put tests in your tests

test("object only", () => {
const it = test({
name: "has name",
fn: () => {
/* */
},
});

assert.equal(it.name, "has name");
});

test("object without name throws", () => {
assert.throws(() =>
// @ts-ignore
test({
fn: () => {
/* */
},
}),
);
});

test("object with two fn throws", () => {
assert.throws(() =>
// @ts-ignore
test(
{
name: "oh no",
fn: () => {
// no-op
},
},
() => {
// no-op
},
),
);
});

test("anonymous function only", () => {
const it = test(() => {
/* */
});
assert.equal(it.name, "unnamed");
});

test("named function only", () => {
const it = test(function thisHasAName() {
/* */
});
assert.equal(it.name, "thisHasAName");
});
10 changes: 6 additions & 4 deletions lib/test_fn.ts
Expand Up @@ -4,7 +4,9 @@ import { color } from "./utils.js";
export interface TestDefinition {
name: string;
fn: TestFn;
// Ignore this test, skipping it and run the test suite as if it doesn't exist.
ignore?: boolean;
// Ignores all tests that do not have the `only` flag and fails the test suite.
only?: boolean;
}

Expand All @@ -14,7 +16,7 @@ type TestOptions = Pick<TestDefinition, "ignore" | "only">;

export class Test {
readonly name!: string;
readonly fn: TestFn;
readonly fn!: TestFn;
readonly ignore: boolean = false;
readonly only: boolean = false;
success = false;
Expand All @@ -38,11 +40,10 @@ export class Test {
if (!fnNameOrOpts.name) {
throw new Error("Test must have a name");
}
if (!fn || typeof fn !== "function") {
throw new Error("Test is missing function");
if (fn || typeof fn === "function") {
throw new Error("Test has two test functions");
}

this.fn = fn;
Object.assign(this, fnNameOrOpts);
} else {
throw new Error("Misformed test definition");
Expand Down Expand Up @@ -94,6 +95,7 @@ export class Test {
}

export function test(fn: TestFn): Test;
export function test(t: TestDefinition): Test;
export function test(opts: TestNoFn, fn: TestFn): Test;
export function test(name: string, fn: TestFn): Test;
export function test(name: string, fn: TestFn, options?: TestOptions): Test;
Expand Down

0 comments on commit e7bec25

Please sign in to comment.