[tests] migrate from jest to vitest (#3940)

This is a big change, but I think it's a good move, as `vitest` is much
more modern than `jest`.

I'm excited about the UI watch feature (run `npm run test:ui`), for
example - it's really helpful and saves time when debugging tests. I had
to adjust a few tests because they had time related issues, but
basically we are now testing the same things - even a bit better and
less flaky (I hope).

What do you think?
This commit is contained in:
Kristjan ESPERANTO
2025-11-03 19:47:01 +01:00
committed by GitHub
parent b542f33a0a
commit 462abf7027
30 changed files with 2370 additions and 3562 deletions

View File

@@ -1,4 +1,3 @@
const { expect } = require("playwright/test");
const { cors, getUserAgent } = require("#server_functions");
describe("server_functions tests", () => {
@@ -8,12 +7,11 @@ describe("server_functions tests", () => {
let fetchResponseHeadersText;
let corsResponse;
let request;
let fetchMock;
beforeEach(() => {
fetchResponseHeadersGet = jest.fn(() => {});
fetchResponseHeadersText = jest.fn(() => {});
fetchResponseHeadersGet = vi.fn(() => {});
fetchResponseHeadersText = vi.fn(() => {});
fetchResponse = {
headers: {
get: fetchResponseHeadersGet
@@ -21,14 +19,14 @@ describe("server_functions tests", () => {
text: fetchResponseHeadersText
};
fetch = jest.fn();
fetch = vi.fn();
fetch.mockImplementation(() => fetchResponse);
fetchMock = fetch;
corsResponse = {
set: jest.fn(() => {}),
send: jest.fn(() => {})
set: vi.fn(() => {}),
send: vi.fn(() => {})
};
request = {
@@ -77,7 +75,7 @@ describe("server_functions tests", () => {
fetchResponseHeadersText.mockImplementation(() => responseData);
let sentData;
corsResponse.send = jest.fn((input) => {
corsResponse.send = vi.fn((input) => {
sentData = input;
});
@@ -94,7 +92,7 @@ describe("server_functions tests", () => {
});
let sentData;
corsResponse.send = jest.fn((input) => {
corsResponse.send = vi.fn((input) => {
sentData = input;
});
@@ -145,17 +143,17 @@ describe("server_functions tests", () => {
});
it("Gets User-Agent from configuration", async () => {
config = {};
global.config = {};
let userAgent;
userAgent = getUserAgent();
expect(userAgent).toContain("Mozilla/5.0 (Node.js ");
config.userAgent = "Mozilla/5.0 (Foo)";
global.config.userAgent = "Mozilla/5.0 (Foo)";
userAgent = getUserAgent();
expect(userAgent).toBe("Mozilla/5.0 (Foo)");
config.userAgent = () => "Mozilla/5.0 (Bar)";
global.config.userAgent = () => "Mozilla/5.0 (Bar)";
userAgent = getUserAgent();
expect(userAgent).toBe("Mozilla/5.0 (Bar)");
});