Skip to content

bun:test ti permette di cambiare che ore sono nei tuoi test.

Questo funziona con uno qualsiasi dei seguenti:

  • Date.now
  • new Date()
  • new Intl.DateTimeFormat().format()

NOTE

I timer non sono ancora impattati, ma potrebbero esserlo in una futura release di Bun.

setSystemTime

Per cambiare il tempo di sistema, usa setSystemTime:

test.ts
ts
import { setSystemTime, beforeAll, test, expect } from "bun:test";

beforeAll(() => {
  setSystemTime(new Date("2020-01-01T00:00:00.000Z"));
});

test("e il 2020", () => {
  expect(new Date().getFullYear()).toBe(2020);
});

Per supportare i test esistenti che usano useFakeTimers e useRealTimers di Jest, puoi usare useFakeTimers e useRealTimers:

test.ts
ts
test("proprio come in jest", () => {
  jest.useFakeTimers();
  jest.setSystemTime(new Date("2020-01-01T00:00:00.000Z"));
  expect(new Date().getFullYear()).toBe(2020);
  jest.useRealTimers();
  expect(new Date().getFullYear()).toBeGreaterThan(2020);
});

test("diversamente da jest", () => {
  const OriginalDate = Date;
  jest.useFakeTimers();
  if (typeof Bun === "undefined") {
    // In Jest, il costruttore Date cambia
    // Questo puo causare tutti i tipi di bug perche improvvisamente Date !== Date prima del test.
    expect(Date).not.toBe(OriginalDate);
    expect(Date.now).not.toBe(OriginalDate.now);
  } else {
    // In bun:test, il costruttore Date non cambia quando usi useFakeTimers
    expect(Date).toBe(OriginalDate);
    expect(Date.now).toBe(OriginalDate.now);
  }
});

Resettare il tempo di sistema

Per resettare il tempo di sistema, passa nessun argomento a setSystemTime:

test.ts
ts
import { setSystemTime, expect, test } from "bun:test";

test("era il 2020, per un momento.", () => {
  // Impostalo a qualcosa!
  setSystemTime(new Date("2020-01-01T00:00:00.000Z"));
  expect(new Date().getFullYear()).toBe(2020);

  // resettalo!
  setSystemTime();

  expect(new Date().getFullYear()).toBeGreaterThan(2020);
});

Ottenere il tempo finto con jest.now()

Quando stai usando il tempo finto (con setSystemTime o useFakeTimers), puoi usare jest.now() per ottenere il timestamp finto corrente:

test.ts
ts
import { test, expect, jest } from "bun:test";

test("ottieni il tempo finto corrente", () => {
  jest.useFakeTimers();
  jest.setSystemTime(new Date("2020-01-01T00:00:00.000Z"));

  expect(Date.now()).toBe(1577836800000); // Timestamp 1 gen 2020
  expect(jest.now()).toBe(1577836800000); // Stesso valore

  jest.useRealTimers();
});

Questo e utile quando hai bisogno di accedere al tempo finto direttamente senza creare un nuovo oggetto Date.

Impostare il fuso orario

Di default, il fuso orario per tutte le esecuzioni di bun test e impostato a UTC (Etc/UTC) a meno che non venga sovrascritto. Per cambiare il fuso orario, passa la variabile di ambiente $TZ a bun test:

bash
TZ=America/Los_Angeles bun test

O imposta process.env.TZ a runtime:

test.ts
ts
import { test, expect } from "bun:test";

test("Benvenuto in California!", () => {
  process.env.TZ = "America/Los_Angeles";
  expect(new Date().getTimezoneOffset()).toBe(420);
  expect(new Intl.DateTimeFormat().resolvedOptions().timeZone).toBe("America/Los_Angeles");
});

test("Benvenuto a New York!", () => {
  // Diversamente da Jest, puoi impostare il fuso orario piu volte a runtime e funzionera.
  process.env.TZ = "America/New_York";
  expect(new Date().getTimezoneOffset()).toBe(240);
  expect(new Intl.DateTimeFormat().resolvedOptions().timeZone).toBe("America/New_York");
});

Bun a cura di www.bunjs.com.cn