Migrate from Jest to node:test (#115)

This commit is contained in:
Andras Schmelczer 2025-08-30 10:38:08 +01:00 committed by GitHub
parent d33f80cca6
commit 0ff3bb5967
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 759 additions and 6421 deletions

View file

@ -1,66 +1,64 @@
import { rateLimit } from "./rate-limit";
import { jest } from "@jest/globals";
import { describe, it, beforeEach, afterEach, mock } from "node:test";
import assert from "node:assert";
describe("rateLimit", () => {
beforeEach(() => {
jest.useFakeTimers();
mock.timers.enable({ apis: ["setTimeout"] });
});
afterEach(() => {
jest.useRealTimers();
mock.timers.reset();
});
it("should call the function immediately on first invocation", async () => {
const mockFn = jest
.fn<() => Promise<string>>()
.mockResolvedValue("result");
const mockFn = mock.fn<() => Promise<string>>();
mockFn.mock.mockImplementation(async () => "result");
const rateLimited = rateLimit(mockFn, 100);
const promise = rateLimited();
expect(mockFn).toHaveBeenCalledTimes(1);
assert.strictEqual(mockFn.mock.callCount(), 1);
await promise;
});
it("should call the function again after the interval has passed", async () => {
const mockFn = jest
.fn<(value: number) => Promise<string>>()
.mockResolvedValue("result");
const mockFn = mock.fn<(value: number) => Promise<string>>();
mockFn.mock.mockImplementation(async () => "result");
const rateLimited = rateLimit(mockFn, 100);
const promise1 = rateLimited(1);
await promise1;
jest.advanceTimersByTime(200);
mock.timers.tick(200);
const promise2 = rateLimited(2);
await promise2;
expect(mockFn).toHaveBeenCalledTimes(2);
expect(mockFn).toHaveBeenCalledWith(2);
assert.strictEqual(mockFn.mock.callCount(), 2);
assert.deepStrictEqual(mockFn.mock.calls[1].arguments, [2]);
});
it("should use the most recent arguments if multiple calls are made within interval", async () => {
const mockFn = jest
.fn<(value: string) => Promise<string>>()
.mockImplementation(async (val) => `${val}-result`);
const mockFn = mock.fn<(value: string) => Promise<string>>();
mockFn.mock.mockImplementation(async (val: string) => `${val}-result`);
const rateLimited = rateLimit(mockFn, 100);
const promise1 = rateLimited("first");
jest.advanceTimersByTime(10);
mock.timers.tick(10);
const promise2 = rateLimited("second");
jest.advanceTimersByTime(10);
mock.timers.tick(10);
const promise3 = rateLimited("third");
jest.advanceTimersByTime(1000);
mock.timers.tick(1000);
expect(await promise1).toEqual("first-result");
expect(await promise2).toEqual("third-result");
expect(await promise3).toBeUndefined();
assert.strictEqual(await promise1, "first-result");
assert.strictEqual(await promise2, "third-result");
assert.strictEqual(await promise3, undefined);
expect(mockFn).toHaveBeenCalledTimes(2);
expect(mockFn).toHaveBeenNthCalledWith(1, "first");
expect(mockFn).toHaveBeenNthCalledWith(2, "third");
assert.strictEqual(mockFn.mock.callCount(), 2);
assert.deepStrictEqual(mockFn.mock.calls[0].arguments, ["first"]);
assert.deepStrictEqual(mockFn.mock.calls[1].arguments, ["third"]);
});
});