以下の内容はhttps://uga-box.hatenablog.com/entry/2024/05/24/000000より取得しました。


【Node.js】標準のtest runner

Node.jsが標準でtest runnerを搭載したのを思い出したので触ってみた

nodejs.org

簡単に試すには、demo.test.mjsというファイルを作って以下を記載すれば良い

import test from 'node:test';

test('synchronous passing test', (t) => {
  // This test passes because it does not throw an exception.
  assert.strictEqual(1, 1);
});

そして、以下のコマンドで実行される

$ node --test demo.test.mjs
✔ synchronous passing test (0.454416ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 42.490667

おなじみのdescribe()it()もある

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

skipもできる

test.skip("Skipping tests", () => {
  assert.notEqual(1, 2)
})

mock 関数もあるので、関数の呼び出し回数、関数に渡された引数の値をテストすることもできた

import assert from 'node:assert';
import { mock, test } from 'node:test';

test('spies on a function', () => {
  const sum = mock.fn((a, b) => {
    return a + b;
  });

  assert.strictEqual(sum.mock.calls.length, 0);
  assert.strictEqual(sum(3, 4), 7);
  assert.strictEqual(sum.mock.calls.length, 1);

  const call = sum.mock.calls[0];
  assert.deepStrictEqual(call.arguments, [3, 4]);
  assert.strictEqual(call.result, 7);
  assert.strictEqual(call.error, undefined);

  // Reset the globally tracked mocks.
  mock.reset();
});

他にも色々あるので使うことになったら見直す




以上の内容はhttps://uga-box.hatenablog.com/entry/2024/05/24/000000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14