Перейти до вмісту

Довідник API node:test

Реєстрація тестів Node.js, набори, хуки, перевірки та моки

На цій сторінці

Імпортуйте сумісний із Node API реєстрації тестів через node:test:

import test, { describe, it } from "node:test";

test("addition", (t) => {
  t.assert.strictEqual(1 + 1, 2);
});

describe("math", () => {
  it("multiplies", (t) => {
    t.assert.strictEqual(2 * 3, 6);
  });
});

Доступний інтерфейс

ОбластьЧлени
Тести та набориtest / it, describe / suite, а також .skip, .todo і .only
Хукиbefore, after, beforeEach, afterEach
Контекст тестуt.assert, t.test, хуки, t.diagnostic, t.plan, t.skip, t.todo, t.mock, t.signal
Мокиmock.fn, mock.method, моки властивостей/аксесорів, фіктивні таймери, скидання та відновлення
Перевірки модуляtest.assert

Перевірки знімків, mock.module() і програмну точку входу run() не реалізовано.

Запуск тестів

Запускайте файли node:test за допомогою elide test, який також приймає явно вказаний файл:

elide test ./math.test.mjs

elide run реєструє тести, не запускаючи їх, і виводить команду elide test, яку слід використати натомість.

Див. також

Reference

Status: Partial · Not measured · since 1.1.0

Exports

ExportKindStatusNotes
test()functionFullDefine and run a test case
it()functionFullAlias for test()
describe()functionFullGroup tests into a suite
suite()functionFullAlias for describe()
before()functionFullRegister a function to run before all tests in the current suite
after()functionFullRegister a function to run after all tests in the current suite
beforeEach()functionFullRegister a function to run before each test in the current suite
afterEach()functionFullRegister a function to run after each test in the current suite
mockpropertyFullThe mock object for creating spies and mocking timers

Not implemented

expectFailure, getTestContext.

Чи була ця сторінка корисною?