0.1.2 • Published 5 months ago

test-runner-register v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

test-runner-register

In-source testing with the Node.js Test Runner

npm latest package Quality

Requirements

Node.js >=18.0.0

Install

npm i -D \
  test-runner-register \
  @swc-node/register \
  @types/node \
  typescript

CJS

path/to/script.ts:

const assert = require("node:assert/strict")
const { test, describe } = require("node:test")

if (
  process.env.NODE_ENV === "test" &&
  process.env.TEST_RUNNER_FILE === __filename
) {
  // You can also use `testRunner` instead of "node:*" modules.
  // const { test, assert, describe } = testRunner

  describe("...", () => {
    test("...", () => {
      assert(true)
    })
  })
}

Run:

node -r test-runner-register \
     -r @swc-node/register \
     path/to/script.ts
▶ ...
  ✔ ... (0.195722ms)
▶ ... (0.838251ms)

ℹ tests 1
ℹ suites 1
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 0.066372

ESM

package.json:

{
  "type": "module"
}

path/to/script.ts:

import assert from "node:assert/strict"
import { test, describe } from "node:test"

if (
  process.env.NODE_ENV === "test" &&
  process.env.TEST_RUNNER_FILE === import.meta.url
) {
  // You can also use `testRunner` instead of "node:*" modules.
  // const { test, assert, describe } = testRunner

  describe("...", () => {
    test("...", () => {
      assert(true)
    })
  })
}

Run (Node.js < 20.6.0):

node -r test-runner-register \
     --loader @swc-node/register/esm \
     ./main.ts

Run (Node.js >= 20.6.0):

node --import test-runner-register/import \
     ./main.ts
▶ ...
  ✔ ... (0.153848ms)
▶ ... (0.78856ms)

ℹ tests 1
ℹ suites 1
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 0.062858

TypeScript

test-runner-register exports testRunner global variable:

const test = require("node:test");
const assert = require("node:assert/strict");

global.testRunner = Object.assign(test, { assert });

env.d.ts:

/// <reference types="test-runner-register/globals" />

tsconfig.json:

{
  "files": [
    "./env.d.ts"
  ]
}

path/to/script.ts:

// import assert from "node:assert/strict"
// import { test, describe } from "node:test"

if (
  process.env.NODE_ENV === "test" &&
  process.env.TEST_RUNNER_FILE === import.meta.url
) {
  // 🎉 You can also use `testRunner` instead of "node:*" modules.
  const { test, assert, describe } = testRunner

  describe("...", () => {
    test("...", () => {
      assert(true)
    })
  })
}

More examples

0.1.0

5 months ago

0.1.2

5 months ago

0.1.1

5 months ago

0.0.10

5 months ago

0.0.11

5 months ago

0.0.12

5 months ago

0.0.13

5 months ago

0.0.9

6 months ago

0.0.8

6 months ago

0.0.7

6 months ago

0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago