From aafbd00fbd61b984c278396b741b623a0f18e471 Mon Sep 17 00:00:00 2001 From: flx-sta <50131232+flx-sta@users.noreply.github.com> Date: Wed, 9 Oct 2024 15:44:09 -0700 Subject: [PATCH] fix tests --- global.d.ts | 2 +- .../plugins/api/pokerogue-account-api.test.ts | 15 +++-------- .../plugins/api/pokerogue-admin-api.test.ts | 13 ++-------- src/test/plugins/api/pokerogue-api.test.ts | 13 ++-------- .../plugins/api/pokerogue-daily-api.test.ts | 13 ++-------- .../api/pokerogue-savedata-api.test.ts | 13 ++-------- .../pokerogue-session-savedata-api.test.ts | 13 ++-------- .../api/pokerogue-system-savedata-api.test.ts | 15 +++-------- src/test/system/game_data.test.ts | 26 ++++--------------- src/test/vitest.setup.ts | 11 +++++--- 10 files changed, 29 insertions(+), 105 deletions(-) diff --git a/global.d.ts b/global.d.ts index f4dfa7d4cb2..c896a4983e4 100644 --- a/global.d.ts +++ b/global.d.ts @@ -10,5 +10,5 @@ declare global { * * To set up your own server in a test see `game_data.test.ts` */ - var i18nServer: SetupServerApi; + var server: SetupServerApi; } diff --git a/src/test/plugins/api/pokerogue-account-api.test.ts b/src/test/plugins/api/pokerogue-account-api.test.ts index 5e4014ef1b9..90a7d3639ad 100644 --- a/src/test/plugins/api/pokerogue-account-api.test.ts +++ b/src/test/plugins/api/pokerogue-account-api.test.ts @@ -2,22 +2,13 @@ import type { AccountInfoResponse } from "#app/@types/PokerogueAccountApi"; import { SESSION_ID_COOKIE_NAME } from "#app/constants"; import { PokerogueAccountApi } from "#app/plugins/api/pokerogue-account-api"; import { getApiBaseUrl } from "#app/test/utils/testUtils"; -import { http, HttpResponse } from "msw"; -import { setupServer } from "msw/node"; -import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import * as Utils from "#app/utils"; +import { http, HttpResponse } from "msw"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const apiBase = getApiBaseUrl(); const accountApi = new PokerogueAccountApi(apiBase); -const server = setupServer(); - -beforeAll(() => { - server.listen({ onUnhandledRequest: "error" }); -}); - -afterAll(() => { - server.close(); -}); +const { server } = global; afterEach(() => { server.resetHandlers(); diff --git a/src/test/plugins/api/pokerogue-admin-api.test.ts b/src/test/plugins/api/pokerogue-admin-api.test.ts index c7b9b28e5ae..66ce179e9fb 100644 --- a/src/test/plugins/api/pokerogue-admin-api.test.ts +++ b/src/test/plugins/api/pokerogue-admin-api.test.ts @@ -2,20 +2,11 @@ import type { LinkAccountToDiscordIdRequest } from "#app/@types/PokerogueAdminAp import { PokerogueAdminApi } from "#app/plugins/api/pokerogue-admin-api"; import { getApiBaseUrl } from "#app/test/utils/testUtils"; import { http, HttpResponse } from "msw"; -import { setupServer } from "msw/node"; -import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const apiBase = getApiBaseUrl(); const adminApi = new PokerogueAdminApi(apiBase); -const server = setupServer(); - -beforeAll(() => { - server.listen({ onUnhandledRequest: "error" }); -}); - -afterAll(() => { - server.close(); -}); +const { server } = global; afterEach(() => { server.resetHandlers(); diff --git a/src/test/plugins/api/pokerogue-api.test.ts b/src/test/plugins/api/pokerogue-api.test.ts index 527361005dd..a62174c226d 100644 --- a/src/test/plugins/api/pokerogue-api.test.ts +++ b/src/test/plugins/api/pokerogue-api.test.ts @@ -2,19 +2,10 @@ import type { TitleStatsResponse } from "#app/@types/PokerogueApi"; import { pokerogueApi } from "#app/plugins/api/pokerogue-api"; import { getApiBaseUrl } from "#app/test/utils/testUtils"; import { http, HttpResponse } from "msw"; -import { setupServer } from "msw/node"; -import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const apiBase = getApiBaseUrl(); -const server = setupServer(); - -beforeAll(() => { - server.listen({ onUnhandledRequest: "error" }); -}); - -afterAll(() => { - server.close(); -}); +const { server } = global; afterEach(() => { server.resetHandlers(); diff --git a/src/test/plugins/api/pokerogue-daily-api.test.ts b/src/test/plugins/api/pokerogue-daily-api.test.ts index 39071f19e73..569e7cbb15d 100644 --- a/src/test/plugins/api/pokerogue-daily-api.test.ts +++ b/src/test/plugins/api/pokerogue-daily-api.test.ts @@ -3,20 +3,11 @@ import { PokerogueDailyApi } from "#app/plugins/api/pokerogue-daily-api"; import { getApiBaseUrl } from "#app/test/utils/testUtils"; import { ScoreboardCategory, type RankingEntry } from "#app/ui/daily-run-scoreboard"; import { http, HttpResponse } from "msw"; -import { setupServer } from "msw/node"; -import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const apiBase = getApiBaseUrl(); const dailyApi = new PokerogueDailyApi(apiBase); -const server = setupServer(); - -beforeAll(() => { - server.listen({ onUnhandledRequest: "error" }); -}); - -afterAll(() => { - server.close(); -}); +const { server } = global; afterEach(() => { server.resetHandlers(); diff --git a/src/test/plugins/api/pokerogue-savedata-api.test.ts b/src/test/plugins/api/pokerogue-savedata-api.test.ts index 984ada09c0c..6dd402206e5 100644 --- a/src/test/plugins/api/pokerogue-savedata-api.test.ts +++ b/src/test/plugins/api/pokerogue-savedata-api.test.ts @@ -2,20 +2,11 @@ import type { UpdateAllSavedataRequest } from "#app/@types/PokerogueSavedataApi" import { PokerogueSavedataApi } from "#app/plugins/api/pokerogue-savedata-api"; import { getApiBaseUrl } from "#app/test/utils/testUtils"; import { http, HttpResponse } from "msw"; -import { setupServer } from "msw/node"; -import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const apiBase = getApiBaseUrl(); const savedataApi = new PokerogueSavedataApi(apiBase); -const server = setupServer(); - -beforeAll(() => { - server.listen({ onUnhandledRequest: "error" }); -}); - -afterAll(() => { - server.close(); -}); +const { server } = global; afterEach(() => { server.resetHandlers(); diff --git a/src/test/plugins/api/pokerogue-session-savedata-api.test.ts b/src/test/plugins/api/pokerogue-session-savedata-api.test.ts index 6928814ded7..d9f6216c4cf 100644 --- a/src/test/plugins/api/pokerogue-session-savedata-api.test.ts +++ b/src/test/plugins/api/pokerogue-session-savedata-api.test.ts @@ -10,20 +10,11 @@ import { PokerogueSessionSavedataApi } from "#app/plugins/api/pokerogue-session- import type { SessionSaveData } from "#app/system/game-data"; import { getApiBaseUrl } from "#app/test/utils/testUtils"; import { http, HttpResponse } from "msw"; -import { setupServer } from "msw/node"; -import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const apiBase = getApiBaseUrl(); const sessionSavedataApi = new PokerogueSessionSavedataApi(apiBase); -const server = setupServer(); - -beforeAll(() => { - server.listen({ onUnhandledRequest: "error" }); -}); - -afterAll(() => { - server.close(); -}); +const { server } = global; afterEach(() => { server.resetHandlers(); diff --git a/src/test/plugins/api/pokerogue-system-savedata-api.test.ts b/src/test/plugins/api/pokerogue-system-savedata-api.test.ts index f5abb4b0c3d..af377762b77 100644 --- a/src/test/plugins/api/pokerogue-system-savedata-api.test.ts +++ b/src/test/plugins/api/pokerogue-system-savedata-api.test.ts @@ -8,20 +8,11 @@ import { PokerogueSystemSavedataApi } from "#app/plugins/api/pokerogue-system-sa import type { SystemSaveData } from "#app/system/game-data"; import { getApiBaseUrl } from "#app/test/utils/testUtils"; import { http, HttpResponse } from "msw"; -import { setupServer } from "msw/node"; -import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const apiBase = getApiBaseUrl(); -const systemSavedataApi = new PokerogueSystemSavedataApi(apiBase); -const server = setupServer(); - -beforeAll(() => { - server.listen({ onUnhandledRequest: "error" }); -}); - -afterAll(() => { - server.close(); -}); +const systemSavedataApi = new PokerogueSystemSavedataApi(getApiBaseUrl()); +const { server } = global; afterEach(() => { server.resetHandlers(); diff --git a/src/test/system/game_data.test.ts b/src/test/system/game_data.test.ts index fcb7e9067a3..1e349470302 100644 --- a/src/test/system/game_data.test.ts +++ b/src/test/system/game_data.test.ts @@ -1,36 +1,23 @@ import * as BattleScene from "#app/battle-scene"; +import { pokerogueApi } from "#app/plugins/api/pokerogue-api"; import { SessionSaveData } from "#app/system/game-data"; import { Abilities } from "#enums/abilities"; import { Moves } from "#enums/moves"; import GameManager from "#test/utils/gameManager"; -import { http, HttpResponse } from "msw"; -import { setupServer } from "msw/node"; import Phaser from "phaser"; -import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import * as account from "../../account"; -const apiBase = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:8001"; - -/** We need a custom server. For some reasons I can't extend the listeners of {@linkcode global.i18nServer} with {@linkcode global.i18nServer.use} */ -const server = setupServer(); - describe("System - Game Data", () => { let phaserGame: Phaser.Game; let game: GameManager; beforeAll(() => { - global.i18nServer.close(); - server.listen(); phaserGame = new Phaser.Game({ type: Phaser.HEADLESS, }); }); - afterAll(() => { - server.close(); - global.i18nServer.listen(); - }); - beforeEach(() => { game = new GameManager(phaserGame); game.override @@ -41,7 +28,6 @@ describe("System - Game Data", () => { }); afterEach(() => { - server.resetHandlers(); game.phaseInterceptor.restoreOg(); }); @@ -61,7 +47,7 @@ describe("System - Game Data", () => { }); it("should return [true, true] if successful", async () => { - server.use(http.post(`${apiBase}/savedata/session/clear`, () => HttpResponse.json({ success: true }))); + vi.spyOn(pokerogueApi.savedata.session, "clear").mockResolvedValue({ success: true }); const result = await game.scene.gameData.tryClearSession(game.scene, 0); @@ -70,7 +56,7 @@ describe("System - Game Data", () => { }); it("should return [true, false] if not successful", async () => { - server.use(http.post(`${apiBase}/savedata/session/clear`, () => HttpResponse.json({ success: false }))); + vi.spyOn(pokerogueApi.savedata.session, "clear").mockResolvedValue({ success: false }); const result = await game.scene.gameData.tryClearSession(game.scene, 0); @@ -79,9 +65,7 @@ describe("System - Game Data", () => { }); it("should return [false, false] session is out of date", async () => { - server.use( - http.post(`${apiBase}/savedata/session/clear`, () => HttpResponse.json({ error: "session out of date" })) - ); + vi.spyOn(pokerogueApi.savedata.session, "clear").mockResolvedValue({ error: "session out of date" }); const result = await game.scene.gameData.tryClearSession(game.scene, 0); diff --git a/src/test/vitest.setup.ts b/src/test/vitest.setup.ts index 8438f607db2..0b83d112522 100644 --- a/src/test/vitest.setup.ts +++ b/src/test/vitest.setup.ts @@ -38,7 +38,7 @@ vi.mock("i18next", async (importOriginal) => { const { setupServer } = await import("msw/node"); const { http, HttpResponse } = await import("msw"); - global.i18nServer = setupServer( + global.server = setupServer( http.get("/locales/en/*", async (req) => { const filename = req.params[0]; @@ -50,9 +50,12 @@ vi.mock("i18next", async (importOriginal) => { console.log(`Failed to load locale ${filename}!`, err); return HttpResponse.json({}); } - }) + }), + http.get("https://fonts.googleapis.com/*", () => { + return HttpResponse.text(""); + }), ); - global.i18nServer.listen({ onUnhandledRequest: "error" }); + global.server.listen({ onUnhandledRequest: "error" }); console.log("i18n MSW server listening!"); return await importOriginal(); @@ -83,6 +86,6 @@ beforeAll(() => { }); afterAll(() => { - global.i18nServer.close(); + global.server.close(); console.log("Closing i18n MSW server!"); });