node:test თუ bun test — Jest-ის გარეშე ტესტები

IS Irakli S. Участник Регистрация: 18 сент. 2026 г.
Сообщений: 44
#1

პატარა API-ს ვწერ და Jest-ის დაყენება აღარ მინდა: დამოკიდებულებების ხე უზარმაზარია. ორივე runtime-ს საკუთარი ტესტ-რანერი აქვს. კითხვები:

1. როგორ გამოიყურება მინიმალური ტესტი თითოეულში?
2. როგორ შევცვალო fetch ტესტში, რომ რეალურ API-ს არ მიმართოს?
3. CI-სთვის რეპორტი (JUnit) როგორ გამოვიტანო?

Გ გიგა Участник Регистрация: 1 сент. 2026 г.
Сообщений: 46
#2

node:test ასე

// price.test.js
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { withVat } from './price.js';

test('adds 18% vat', () => {
  assert.equal(withVat(100), 118);
});

გაშვება node --test თვითონ პოულობს *.test.js ფაილებს

bun-ში

import { test, expect } from 'bun:test';
import { withVat } from './price.js';

test('adds 18% vat', () => {
  expect(withVat(100)).toBe(118);
});

გაშვება bun test

GM Giorgi M. Участник Регистрация: 11 июн. 2026 г.
Сообщений: 117
#3

fetch-ის შეცვლა node:test-ში mock.method-ით, ტესტის ბოლოს თავისით აღდგება:

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { getRate } from './rates.js';

test('reads the rate from the API answer', async (t) => {
  t.mock.method(globalThis, 'fetch', async () =>
    Response.json({ currency: 'USD', rate: 2.7 }),
  );
  assert.equal(await getRate('USD'), 2.7);
  assert.equal(globalThis.fetch.mock.callCount(), 1);
});

t.mock ტესტის კონტექსტზეა მიბმული, ამიტომ სხვა ტესტებზე არ მოქმედებს.

L levan Участник Регистрация: 24 авг. 2026 г.
Сообщений: 74
#4

JUnit რეპორტი CI-სთვის

node --test --test-reporter=spec --test-reporter-destination=stdout \
  --test-reporter=junit --test-reporter-destination=report.xml

ორი რეპორტერი ერთად კონსოლში ჩვეულებრივი გამოსავალი ფაილში junit

Ს სოფო Участник Регистрация: 19 авг. 2026 г.
Сообщений: 21
#5

ტაიმერებიანი კოდი როგორ დავტესტო? setTimeout-ით 30 წამს ელოდება და ტესტი ამდენ ხანს ხომ არ უნდა გაიწელოს

IS Irakli S. Участник Регистрация: 18 сент. 2026 г.
Сообщений: 44
#6

>>5
node:test-ს ყალბი ტაიმერები აქვს: დროს ხელით „გადაახვევ“ და ტესტი მყისიერად სრულდება.

import { test } from 'node:test';
import assert from 'node:assert/strict';

test('fires after 30 seconds', (t) => {
  t.mock.timers.enable({ apis: ['setTimeout'] });
  let fired = false;
  setTimeout(() => { fired = true; }, 30_000);

  t.mock.timers.tick(29_999);
  assert.equal(fired, false);
  t.mock.timers.tick(1);
  assert.equal(fired, true);
});

ჩემი დასკვნა თემიდან: თუ კოდი მხოლოდ Bun-ზე მუშაობს, bun test მოსახერხებელია; თუ Node-ზე ან ორივეზე, node:test და node:assert საკმარისია და დამატებითი პაკეტი არ სჭირდება.

Быстрый ответ

Писать могут только вошедшие участники. Войти