0.1.0 • Published 3 years ago

@mizchi/testio v0.1.0

Weekly downloads
-
License
-
Repository
-
Last release
3 years ago

@mizchi/testio

Simple test runner to run test standalone with console stub.

Test is incremental. Just run tests in related scope.

Support node and deno.

Concepts

  • Lightweight and no dependency.
  • Inline code testing helper. All files are executable as test runner.
  • Drop test codes by terser DCE.
  • Stub console to hydrate logs at fail only.

How to use

yarn add @mizchi/testio esbuild esbuild-register --dev
# esbuild(-register) is optional for typescript

(esbuild-node is not suitable because it removes require.main)

simple

// hello.ts
export function add(a: number, b: number) {
  return a + b;
}

import { test, run, is } from "@mizchi/testio";
test("test1", () => {});
run();

run

$ node -r esbuild-register hello.js
=== PASS: test1

with many files + DCE friendly

Simple code always runs by import and bundler includes it. With many files, other logs are annoying.

Write code for dead code ellimination(DCE) and entry detecting.

export function add(a: number, b: number) {
  return a + b;
}

/* === testio start === */
import { test, run, is, err } from "@mizchi/testio";
const isMain = require.main === module;
if (process.env.NODE_ENV === "test") {
// or main only.
// if (process.env.NODE_ENV === "test" && isMain) {
  test("test1", () => {
    console.log("do not show this message on success");
    is(add(1, 1), 2);
    err(() => is(add(1, -1), 3));
  });
  test("test2", () => {
    is(sub, 1);
  });
  run({ isMain });
}

Run

$ NODE_ENV=test node -r esbuild-register add.ts
=== PASS: test1
=== PASS: test2

See example/*.ts

Drop tests in Build: Vite Example

Define process.env.NODE_ENV=production and require.main === module

vite example.

// vite.config.ts
import { defineConfig } from "vite";
export default defineConfig({
  define: {
    "process.env.NODE_ENV": JSON.stringify("production"),
    "require.main === module": JSON.stringify(false),
  },
});

or DefinePlugin | webpack and others.

easy assertion tool (or use other assertion)

is() and err() are bundled.

  • is(a, b): void: helper match partially.
  • err(fn: () => void | Promise<void>): void: catches throw. fn should throw.
import {is, err, ANY} from "@mizchi/testio";
is(false, ANY);
is(1, 1);
is({ a: 1 }, { a: 1 });
err(() => is(1, 0));
is({ a: 1, b: 2 }, { a: 1 });
is({ a: 1, b: 2 }, {});

err(() => is({ a: 1 }, { a: 2 }));
is([1], []);
is([1], [1]);
is([1, 2, 0], [1, ANY, ANY]);
is([1, 2, 0], [1, ANY, ANY, ANY]);
err(() => is([], [1]));
err(() => is(null, [1, 2]));
err(() => is([1, 2, 1], [1, 3]));
err(() => is({ a: { b: 1 } }, { a: { b: 2 } }));

In complex case, use your favorite assertion library.

Snippets

Node: run with dependency tests

/* === testio start === */
import { run, test } from "@mizchi/testio";
const isMain = require.main === module;
if (process.env.NODE_ENV === "test") {
  test("ok", () => {
    // write here
  });
  run({ isMain });
}

I reccomend this and run all tests by eg. src/index.ts.

Node: run as main only

with cancelAll() before run.

/* === testio start === */
import { run, test, cancelAll } from "@mizchi/testio";
const isMain = require.main === module;
if (process.env.NODE_ENV === "test") {
  cancelAll();
  test("ok", () => {
    // write here
  });
  run({ isMain });
}

It's helpful hack if you want with dependency mode and cancel others.

with Deno

CAUTION I DO NOT reccomend to leave test codes on deno because it causes runtime cost on any case. If you want to use, comment out is needed.

/* === testio start === */
import { run, test } from "http://cdn.skypack.dev/@mizchi/testio";
const isMain = import.meta.main;
if (isMain) {
  test("ok", () => {
    // write here
  });
  run({ isMain });
}

Related

LICENSE

MIT

0.1.0

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago