node:test provides Node’s test runner API (tests, suites, hooks, test context,
and mocks). Run test files with elide test.
Example
js
// math.test.mjs
import assert from "node:assert/strict";
import { describe, it, mock } from "node:test";
describe("math", () => {
it("adds", () => {
assert.equal(1 + 2, 3);
});
it("calls the callback once", () => {
const callback = mock.fn();
[1].forEach(callback);
assert.equal(callback.mock.callCount(), 1);
});
});
utf-8 · js · Lines: 15
elide test ./math.test.mjs prints:
text
math.test.mjs
✓ math > adds [1ms]
✓ math > calls the callback once
2 pass
0 skip
0 fail
Ran 2 tests across one file. (0.01s)
utf-8 · text · Lines: 8
Differences from Node
elide runregisters tests without running them and prints theelide testcommand to use.run(),mock.module(), and thesnapshotserializer setters throwERR_METHOD_NOT_IMPLEMENTED.- Snapshot assertions are not available.
Samples
Reference
Status: Partial · Not measured · since 1.1.0
Exports
| Export | Kind | Status | Notes |
|---|---|---|---|
test() | function | Full | Define and run a test case |
it() | function | Full | Alias for test() |
describe() | function | Full | Group tests into a suite |
suite() | function | Full | Alias for describe() |
before() | function | Full | Register a function to run before all tests in the current suite |
after() | function | Full | Register a function to run after all tests in the current suite |
beforeEach() | function | Full | Register a function to run before each test in the current suite |
afterEach() | function | Full | Register a function to run after each test in the current suite |
mock | property | Full | The mock object for creating spies and mocking timers |
Not implemented
expectFailure, getTestContext.