pokerogue/test/setup/vitest.setup.ts
Bertie690 f48ec4c51e
[Dev] Ensure i18n module is initialized immediately when imported (#6317)
* [Dev] Ensure `i18n` module is initialized immediately when imported

* Fixed missing await?

* Update src/main.ts

* Update init.ts

* Update src/main.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Update src/plugins/i18n.ts

* Update trainer-config.ts

* ran biome & made `@module` comment

* Update src/plugins/i18n.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Update src/plugins/i18n.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Fixed import typo

* flubber

* Ran Biome

* foo

* Remove default re-export of `i18next`

* Update i18n.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* fixde import issues

* Move `i18n` initialization to `main.ts` from `init.ts`

* Remove some redundant & incorrect comments from `trainer-config.ts`

* Fix tests

* Apply Biome

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2025-12-17 18:39:30 -08:00

79 lines
2.6 KiB
TypeScript

import "vitest-canvas-mock";
import "#plugins/i18n"; // tests don't go through `main.ts`, requiring this to be imported here as well
import { MockConsole } from "#test/test-utils/mocks/mock-console/mock-console";
import { logTestEnd, logTestStart } from "#test/test-utils/setup/test-end-log";
import { initTests } from "#test/test-utils/test-file-initialization";
import chalk from "chalk";
import { afterAll, afterEach, beforeAll, beforeEach, vi } from "vitest";
//#region Mocking
// Mock the override import to always return default values, ignoring any custom overrides.
vi.mock(import("#app/overrides"), async importOriginal => {
const { defaultOverrides } = await importOriginal();
return {
default: defaultOverrides,
// Export `defaultOverrides` as a *copy*.
// This ensures we can easily reset `overrides` back to its default values after modifying it.
defaultOverrides: { ...defaultOverrides },
} satisfies typeof import("#app/overrides");
});
/**
* This is a hacky way to mock the i18n backend requests (with the help of {@link https://mswjs.io/ | msw}).
* The reason to put it inside of a mock is to elevate it.
* This is necessary because how our code is structured.
* Do NOT try to put any of this code into external functions, it won't work as it's elevated during runtime.
*/
vi.mock(import("i18next"), async importOriginal => {
console.log("Mocking i18next");
const { setupServer } = await import("msw/node");
const { http, HttpResponse } = await import("msw");
global.server = setupServer(
http.get("/locales/en/*", async req => {
const filename = req.params[0];
try {
const localeFiles = import.meta.glob("../../locales/en/**/*.json", { eager: true });
const json = localeFiles[`../../locales/en/${filename}`] || {};
if (import.meta.env.VITE_I18N_DEBUG === "1") {
console.log("Loaded locale", filename);
}
return HttpResponse.json(json);
} catch (err) {
console.error(`Failed to load locale ${filename}\n`, err);
return HttpResponse.json({});
}
}),
http.get("https://fonts.googleapis.com/*", () => {
return HttpResponse.text("");
}),
);
global.server.listen({ onUnhandledRequest: "error" });
console.log("i18n MSW server listening!");
return await importOriginal();
});
global.testFailed = false;
beforeAll(() => {
initTests();
});
beforeEach(context => {
logTestStart(context.task);
});
afterEach(context => {
logTestEnd(context.task);
});
afterAll(() => {
global.server.close();
MockConsole.printPostTestWarnings();
console.log(chalk.hex("#dfb8d8")("Closing i18n MSW server!"));
});