refactor: Refactor tests to not use module mocks, so bun test can be used
Some checks failed
CodeQL Scan / Analyze (javascript-typescript) (push) Failing after 45s
Build Docker Images / lint (push) Successful in 27s
Build Docker Images / check (push) Successful in 1m7s
Build Docker Images / tests (push) Failing after 6s
Build Docker Images / build (server, Dockerfile, ${{ github.repository_owner }}/server) (push) Has been skipped
Build Docker Images / build (worker, Worker.Dockerfile, ${{ github.repository_owner }}/worker) (push) Has been skipped
Deploy Docs to GitHub Pages / build (push) Failing after 12s
Mirror to Codeberg / Mirror (push) Failing after 0s
Deploy Docs to GitHub Pages / Deploy (push) Has been skipped
Nix Build / check (push) Failing after 32m31s

This commit is contained in:
Jesse Wierzbinski 2025-03-23 04:12:28 +01:00
parent ec506241f0
commit 7112a66e4c
No known key found for this signature in database
10 changed files with 71 additions and 527 deletions

View file

@ -1,3 +1,4 @@
import { mock } from "bun:test";
import { generateChallenge } from "@/challenges";
import { randomString } from "@/math";
import { Client as VersiaClient } from "@versia/client";
@ -9,7 +10,6 @@ import { appFactory } from "~/app";
import { searchManager } from "~/classes/search/search-manager";
import { config } from "~/config.ts";
import { setupDatabase } from "~/drizzle/db";
await setupDatabase();
if (config.search.enabled) {
@ -210,3 +210,33 @@ export const getSolvedChallenge = async (): Promise<string> => {
}),
).toString("base64");
};
/**
* Mocks a module for the duration of a test
* Workaround for https://github.com/oven-sh/bun/issues/7823
*
* @param modulePath - The path starting from this files' path.
* @param renderMocks - Function to generate mocks (by their named or default exports)
* @returns An object with a dispose method
*/
export const mockModule = async (
modulePath: string,
renderMocks: () => Record<string, unknown>,
): Promise<{
[Symbol.dispose](): void;
}> => {
const original = {
...(await import(modulePath)),
};
const mocks = renderMocks();
const result = {
...original,
...mocks,
};
mock.module(modulePath, () => result);
return {
[Symbol.dispose]: (): void => {
mock.module(modulePath, () => original);
},
};
};