@sondr3/minitest
Version:
A low-feature, dependency-free and performant test runner inspired by Rust and Deno
87 lines • 2.83 kB
JavaScript
import { TESTS } from "./runner.js";
import { TestRunner } from "./test_runner.js";
const isTestDefinition = (object) => {
return object !== null && typeof object === "object" && "name" in object && "fn" in object;
};
export class Test {
name;
fn;
_ignore = false;
_only = false;
constructor(fnNameOrOpts, fn, opts) {
if (typeof fnNameOrOpts === "function") {
const name = fnNameOrOpts.name === "" ? "unnamed" : fnNameOrOpts.name;
this.name = name;
this.fn = fnNameOrOpts;
this._ignore = opts?.ignore ?? false;
this._only = opts?.only ?? false;
}
else if (typeof fnNameOrOpts === "string") {
if (!fn || typeof fn !== "function") {
throw new Error("Test is missing function");
}
this.name = fnNameOrOpts;
this.fn = fn;
this._ignore = opts?.ignore ?? false;
this._only = opts?.only ?? false;
}
else if (typeof fnNameOrOpts === "object") {
if (!fnNameOrOpts.name) {
throw new Error("Test must have a name");
}
if (isTestDefinition(fnNameOrOpts)) {
if (fn || typeof fn === "function") {
throw new Error("Test has two test functions");
}
this.name = fnNameOrOpts.name;
this.fn = fnNameOrOpts.fn;
this._ignore = fnNameOrOpts.ignore ?? false;
this._only = fnNameOrOpts.only ?? false;
}
else {
if (!fn) {
throw new Error("Test is missing function");
}
this.name = fnNameOrOpts.name;
this.fn = fn;
this._ignore = fnNameOrOpts.ignore ?? false;
this._only = fnNameOrOpts.only ?? false;
}
}
else {
throw new Error("Misformed test definition");
}
TESTS.push(this);
}
/** Mark the test as ignored */
ignore(yes = true) {
this._ignore = yes;
}
/** Only run this test */
only() {
this._only = true;
}
/** @internal */
toTestRunner() {
return new TestRunner(this.name, this.fn, this._ignore, this._only);
}
}
/**
* Register a test which will be run when `mt` is used on the command line and
* the containing module is a test module. `fn` can be async if required.
*
* ## Example:
*
* ```ts
* import { test } from "@sondr3/minitest";
* import { strict as assert } from "node:assert";
*
* test("example test", () => {
* assert(true === true, "Phew");
* });
* ```
*/
export function test(fnNameOrOpts, fn, opts) {
return new Test(fnNameOrOpts, fn, opts);
}
//# sourceMappingURL=test_fn.js.map