Aller au contenu

Référence de l'API node:test

Enregistrement des tests Node.js, suites, hooks, assertions et mocks

Sur cette page

Importez l’API d’enregistrement des tests compatible Node avec 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);
  });
});

Surface disponible

DomaineMembres
Tests et suitestest / it, describe / suite, ainsi que .skip, .todo et .only
Hooksbefore, after, beforeEach, afterEach
Contexte de testt.assert, t.test, hooks, t.diagnostic, t.plan, t.skip, t.todo, t.mock, t.signal
Mocksmock.fn, mock.method, mocks de propriétés/accesseurs, fausses minuteries, réinitialisation et restauration
Assertions du moduletest.assert

Les assertions de snapshot, mock.module() et le point d’entrée programmatique run() ne sont pas implémentés.

Exécuter les tests

Exécutez les fichiers node:test avec elide test, qui accepte aussi un fichier explicite :

elide test ./math.test.mjs

elide run enregistre les tests sans les exécuter, et affiche la commande elide test à utiliser à la place.

Voir aussi

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.

Cette page vous a-t-elle aidé ?