From e70138a5a862ae183f8c36173a3b27b60e1afeda Mon Sep 17 00:00:00 2001 From: Sirz Benjie <142067137+SirzBenjie@users.noreply.github.com> Date: Thu, 27 Mar 2025 17:55:16 -0500 Subject: [PATCH] Fix tests not working with --no-isolate --- src/system/game-data.ts | 66 +++++++++---------- test/abilities/steely_spirit.test.ts | 4 +- test/abilities/supreme_overlord.test.ts | 7 +- test/abilities/wonder_skin.test.ts | 13 ++-- test/data/status_effect.test.ts | 6 -- test/moves/copycat.test.ts | 3 +- test/moves/dynamax_cannon.test.ts | 4 +- test/moves/fusion_flare_bolt.test.ts | 7 +- test/moves/hard_press.test.ts | 4 +- test/moves/last_respects.test.ts | 7 +- test/moves/metronome.test.ts | 3 +- test/moves/rage_fist.test.ts | 4 +- test/moves/retaliate.test.ts | 4 +- test/moves/shell_side_arm.test.ts | 7 +- test/moves/spit_up.test.ts | 4 +- test/moves/tera_blast.test.ts | 8 ++- test/moves/triple_arrows.test.ts | 10 ++- .../mystery-encounter/encounter-test-utils.ts | 10 --- .../plugins/api/pokerogue-account-api.test.ts | 10 ++- test/plugins/api/pokerogue-admin-api.test.ts | 10 ++- test/plugins/api/pokerogue-api.test.ts | 10 ++- test/plugins/api/pokerogue-daily-api.test.ts | 10 ++- .../api/pokerogue-savedata-api.test.ts | 10 ++- .../pokerogue-session-savedata-api.test.ts | 10 ++- .../api/pokerogue-system-savedata-api.test.ts | 3 +- test/testUtils/gameManager.ts | 2 +- test/testUtils/gameWrapper.ts | 25 ++++++- test/testUtils/testFileInitialization.ts | 9 ++- test/vitest.setup.ts | 25 +++++-- vitest.config.ts | 2 +- 30 files changed, 196 insertions(+), 101 deletions(-) diff --git a/src/system/game-data.ts b/src/system/game-data.ts index 6b0a16cde1b..7845d50b2ad 100644 --- a/src/system/game-data.ts +++ b/src/system/game-data.ts @@ -202,39 +202,6 @@ export interface DexEntry { ivs: number[]; } -export const DexAttr = { - NON_SHINY: 1n, - SHINY: 2n, - MALE: 4n, - FEMALE: 8n, - DEFAULT_VARIANT: 16n, - VARIANT_2: 32n, - VARIANT_3: 64n, - DEFAULT_FORM: 128n, -}; - -export interface DexAttrProps { - shiny: boolean; - female: boolean; - variant: Variant; - formIndex: number; -} - -export const AbilityAttr = { - ABILITY_1: 1, - ABILITY_2: 2, - ABILITY_HIDDEN: 4, -}; - -export type RunHistoryData = Record; - -export interface RunEntry { - entry: SessionSaveData; - isVictory: boolean; - /*Automatically set to false at the moment - implementation TBD*/ - isFavorite: boolean; -} - export type StarterMoveset = [Moves] | [Moves, Moves] | [Moves, Moves, Moves] | [Moves, Moves, Moves, Moves]; export interface StarterFormMoveData { @@ -261,6 +228,39 @@ export interface StarterPreferences { [key: number]: StarterAttributes; } +export interface DexAttrProps { + shiny: boolean; + female: boolean; + variant: Variant; + formIndex: number; +} + +export type RunHistoryData = Record; + +export interface RunEntry { + entry: SessionSaveData; + isVictory: boolean; + /*Automatically set to false at the moment - implementation TBD*/ + isFavorite: boolean; +} + +export const DexAttr = { + NON_SHINY: 1n, + SHINY: 2n, + MALE: 4n, + FEMALE: 8n, + DEFAULT_VARIANT: 16n, + VARIANT_2: 32n, + VARIANT_3: 64n, + DEFAULT_FORM: 128n, +}; + +export const AbilityAttr = { + ABILITY_1: 1, + ABILITY_2: 2, + ABILITY_HIDDEN: 4, +}; + // the latest data saved/loaded for the Starter Preferences. Required to reduce read/writes. Initialize as "{}", since this is the default value and no data needs to be stored if present. // if they ever add private static variables, move this into StarterPrefs const StarterPrefers_DEFAULT: string = "{}"; diff --git a/test/abilities/steely_spirit.test.ts b/test/abilities/steely_spirit.test.ts index 9b4d32efcae..b180ff8919e 100644 --- a/test/abilities/steely_spirit.test.ts +++ b/test/abilities/steely_spirit.test.ts @@ -12,7 +12,8 @@ describe("Abilities - Steely Spirit", () => { let game: GameManager; const steelySpiritMultiplier = 1.5; const moveToCheck = Moves.IRON_HEAD; - const ironHeadPower = allMoves[moveToCheck].power; + + let ironHeadPower: number; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -25,6 +26,7 @@ describe("Abilities - Steely Spirit", () => { }); beforeEach(() => { + ironHeadPower = allMoves[moveToCheck].power; game = new GameManager(phaserGame); game.override.battleType("double"); game.override.enemySpecies(Species.SHUCKLE); diff --git a/test/abilities/supreme_overlord.test.ts b/test/abilities/supreme_overlord.test.ts index b70acbd8d17..a71bf0a9354 100644 --- a/test/abilities/supreme_overlord.test.ts +++ b/test/abilities/supreme_overlord.test.ts @@ -1,4 +1,5 @@ import { Moves } from "#app/enums/moves"; +import type Move from "#app/data/moves/move"; import { Abilities } from "#enums/abilities"; import { Species } from "#enums/species"; import { BattlerIndex } from "#app/battle"; @@ -12,8 +13,8 @@ describe("Abilities - Supreme Overlord", () => { let phaserGame: Phaser.Game; let game: GameManager; - const move = allMoves[Moves.TACKLE]; - const basePower = move.power; + let move: Move; + let basePower: number; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -26,6 +27,8 @@ describe("Abilities - Supreme Overlord", () => { }); beforeEach(() => { + move = allMoves[Moves.TACKLE]; + basePower = move.power; game = new GameManager(phaserGame); game.override .battleType("single") diff --git a/test/abilities/wonder_skin.test.ts b/test/abilities/wonder_skin.test.ts index f2cb0faed72..db746831753 100644 --- a/test/abilities/wonder_skin.test.ts +++ b/test/abilities/wonder_skin.test.ts @@ -56,16 +56,21 @@ describe("Abilities - Wonder Skin", () => { expect(moveToCheck.calculateBattleAccuracy).toHaveReturnedWith(100); }); - const bypassAbilities = [Abilities.MOLD_BREAKER, Abilities.TERAVOLT, Abilities.TURBOBLAZE]; + const bypassAbilities = [ + [Abilities.MOLD_BREAKER, "Mold Breaker"], + [Abilities.TERAVOLT, "Teravolt"], + [Abilities.TURBOBLAZE, "Turboblaze"], + ]; bypassAbilities.forEach(ability => { - it(`does not affect pokemon with ${allAbilities[ability].name}`, async () => { + it(`does not affect pokemon with ${ability[1]}`, async () => { const moveToCheck = allMoves[Moves.CHARM]; - game.override.ability(ability); + // @ts-ignore ts doesn't know that ability[0] is an ability and not a string... + game.override.ability(ability[0]); vi.spyOn(moveToCheck, "calculateBattleAccuracy"); - await game.startBattle([Species.PIKACHU]); + await game.classicMode.startBattle([Species.PIKACHU]); game.move.select(Moves.CHARM); await game.phaseInterceptor.to(MoveEffectPhase); diff --git a/test/data/status_effect.test.ts b/test/data/status_effect.test.ts index 61dafc1c9b8..0fd2daa308b 100644 --- a/test/data/status_effect.test.ts +++ b/test/data/status_effect.test.ts @@ -13,17 +13,12 @@ import { Species } from "#enums/species"; import { StatusEffect } from "#enums/status-effect"; import GameManager from "#test/testUtils/gameManager"; import { mockI18next } from "#test/testUtils/testUtils"; -import i18next from "i18next"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; const pokemonName = "PKM"; const sourceText = "SOURCE"; describe("Status Effect Messages", () => { - beforeAll(async () => { - await i18next.init(); - }); - describe("NONE", () => { const statusEffect = StatusEffect.NONE; @@ -31,7 +26,6 @@ describe("Status Effect Messages", () => { mockI18next(); const text = getStatusEffectObtainText(statusEffect, pokemonName); - console.log("text:", text); expect(text).toBe(""); const emptySourceText = getStatusEffectObtainText(statusEffect, pokemonName, ""); diff --git a/test/moves/copycat.test.ts b/test/moves/copycat.test.ts index c7242f0940e..0d9b0951f89 100644 --- a/test/moves/copycat.test.ts +++ b/test/moves/copycat.test.ts @@ -13,7 +13,7 @@ describe("Moves - Copycat", () => { let phaserGame: Phaser.Game; let game: GameManager; - const randomMoveAttr = allMoves[Moves.METRONOME].getAttrs(RandomMoveAttr)[0]; + let randomMoveAttr: RandomMoveAttr; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -26,6 +26,7 @@ describe("Moves - Copycat", () => { }); beforeEach(() => { + randomMoveAttr = allMoves[Moves.METRONOME].getAttrs(RandomMoveAttr)[0]; game = new GameManager(phaserGame); game.override .moveset([Moves.COPYCAT, Moves.SPIKY_SHIELD, Moves.SWORDS_DANCE, Moves.SPLASH]) diff --git a/test/moves/dynamax_cannon.test.ts b/test/moves/dynamax_cannon.test.ts index 0ff0712710d..9cf3106b9c1 100644 --- a/test/moves/dynamax_cannon.test.ts +++ b/test/moves/dynamax_cannon.test.ts @@ -3,6 +3,7 @@ import { allMoves } from "#app/data/moves/move"; import { DamageAnimPhase } from "#app/phases/damage-anim-phase"; import { MoveEffectPhase } from "#app/phases/move-effect-phase"; import { Moves } from "#enums/moves"; +import type Move from "#app/data/moves/move"; import { Species } from "#enums/species"; import GameManager from "#test/testUtils/gameManager"; import Phaser from "phaser"; @@ -12,7 +13,7 @@ describe("Moves - Dynamax Cannon", () => { let phaserGame: Phaser.Game; let game: GameManager; - const dynamaxCannon = allMoves[Moves.DYNAMAX_CANNON]; + let dynamaxCannon: Move; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -25,6 +26,7 @@ describe("Moves - Dynamax Cannon", () => { }); beforeEach(() => { + dynamaxCannon = allMoves[Moves.DYNAMAX_CANNON]; game = new GameManager(phaserGame); game.override.moveset([dynamaxCannon.id]); diff --git a/test/moves/fusion_flare_bolt.test.ts b/test/moves/fusion_flare_bolt.test.ts index f004057dae0..c340aeea63f 100644 --- a/test/moves/fusion_flare_bolt.test.ts +++ b/test/moves/fusion_flare_bolt.test.ts @@ -1,6 +1,7 @@ import { Stat } from "#enums/stat"; import { BattlerIndex } from "#app/battle"; import { allMoves } from "#app/data/moves/move"; +import type Move from "#app/data/moves/move"; import { DamageAnimPhase } from "#app/phases/damage-anim-phase"; import { MoveEffectPhase } from "#app/phases/move-effect-phase"; import { MoveEndPhase } from "#app/phases/move-end-phase"; @@ -15,8 +16,8 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => { let phaserGame: Phaser.Game; let game: GameManager; - const fusionFlare = allMoves[Moves.FUSION_FLARE]; - const fusionBolt = allMoves[Moves.FUSION_BOLT]; + let fusionFlare: Move; + let fusionBolt: Move; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -29,6 +30,8 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => { }); beforeEach(() => { + fusionFlare = allMoves[Moves.FUSION_FLARE]; + fusionBolt = allMoves[Moves.FUSION_BOLT]; game = new GameManager(phaserGame); game.override.moveset([fusionFlare.id, fusionBolt.id]); game.override.startingLevel(1); diff --git a/test/moves/hard_press.test.ts b/test/moves/hard_press.test.ts index 1bb6adc8e90..8891f0bf0e2 100644 --- a/test/moves/hard_press.test.ts +++ b/test/moves/hard_press.test.ts @@ -6,12 +6,13 @@ import { Species } from "#enums/species"; import GameManager from "#test/testUtils/gameManager"; import Phaser from "phaser"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import type Move from "#app/data/moves/move"; describe("Moves - Hard Press", () => { let phaserGame: Phaser.Game; let game: GameManager; - const moveToCheck = allMoves[Moves.HARD_PRESS]; + let moveToCheck: Move; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -24,6 +25,7 @@ describe("Moves - Hard Press", () => { }); beforeEach(() => { + moveToCheck = allMoves[Moves.HARD_PRESS]; game = new GameManager(phaserGame); game.override.battleType("single"); game.override.ability(Abilities.BALL_FETCH); diff --git a/test/moves/last_respects.test.ts b/test/moves/last_respects.test.ts index 57752cea1af..ccab8a43415 100644 --- a/test/moves/last_respects.test.ts +++ b/test/moves/last_respects.test.ts @@ -4,6 +4,7 @@ import { Species } from "#enums/species"; import { Abilities } from "#enums/abilities"; import GameManager from "#test/testUtils/gameManager"; import { allMoves } from "#app/data/moves/move"; +import type Move from "#app/data/moves/move"; import { MoveEffectPhase } from "#app/phases/move-effect-phase"; import Phaser from "phaser"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; @@ -12,8 +13,8 @@ describe("Moves - Last Respects", () => { let phaserGame: Phaser.Game; let game: GameManager; - const move = allMoves[Moves.LAST_RESPECTS]; - const basePower = move.power; + let move: Move; + let basePower: number; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -27,6 +28,8 @@ describe("Moves - Last Respects", () => { beforeEach(() => { game = new GameManager(phaserGame); + move = allMoves[Moves.LAST_RESPECTS]; + basePower = move.power; game.override .battleType("single") .disableCrits() diff --git a/test/moves/metronome.test.ts b/test/moves/metronome.test.ts index 15790777ed3..80f32a3a6fb 100644 --- a/test/moves/metronome.test.ts +++ b/test/moves/metronome.test.ts @@ -13,7 +13,7 @@ describe("Moves - Metronome", () => { let phaserGame: Phaser.Game; let game: GameManager; - const randomMoveAttr = allMoves[Moves.METRONOME].getAttrs(RandomMoveAttr)[0]; + let randomMoveAttr: RandomMoveAttr; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -26,6 +26,7 @@ describe("Moves - Metronome", () => { }); beforeEach(() => { + randomMoveAttr = allMoves[Moves.METRONOME].getAttrs(RandomMoveAttr)[0]; game = new GameManager(phaserGame); game.override .moveset([Moves.METRONOME, Moves.SPLASH]) diff --git a/test/moves/rage_fist.test.ts b/test/moves/rage_fist.test.ts index 8bcb212d60e..f44901c5aba 100644 --- a/test/moves/rage_fist.test.ts +++ b/test/moves/rage_fist.test.ts @@ -3,6 +3,7 @@ import { Abilities } from "#enums/abilities"; import { Moves } from "#enums/moves"; import { Species } from "#enums/species"; import { allMoves } from "#app/data/moves/move"; +import type Move from "#app/data/moves/move"; import GameManager from "#test/testUtils/gameManager"; import Phaser from "phaser"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; @@ -10,7 +11,7 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vite describe("Moves - Rage Fist", () => { let phaserGame: Phaser.Game; let game: GameManager; - const move = allMoves[Moves.RAGE_FIST]; + let move: Move; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -23,6 +24,7 @@ describe("Moves - Rage Fist", () => { }); beforeEach(() => { + move = allMoves[Moves.RAGE_FIST]; game = new GameManager(phaserGame); game.override .battleType("single") diff --git a/test/moves/retaliate.test.ts b/test/moves/retaliate.test.ts index 5cc0b08ccc6..e916c9ffeaa 100644 --- a/test/moves/retaliate.test.ts +++ b/test/moves/retaliate.test.ts @@ -4,12 +4,13 @@ import GameManager from "#test/testUtils/gameManager"; import { Species } from "#enums/species"; import { Moves } from "#enums/moves"; import { allMoves } from "#app/data/moves/move"; +import type Move from "#app/data/moves/move"; describe("Moves - Retaliate", () => { let phaserGame: Phaser.Game; let game: GameManager; - const retaliate = allMoves[Moves.RETALIATE]; + let retaliate: Move; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -22,6 +23,7 @@ describe("Moves - Retaliate", () => { }); beforeEach(() => { + retaliate = allMoves[Moves.RETALIATE]; game = new GameManager(phaserGame); game.override .battleType("single") diff --git a/test/moves/shell_side_arm.test.ts b/test/moves/shell_side_arm.test.ts index 47da5e1c2f7..a5b065b76cb 100644 --- a/test/moves/shell_side_arm.test.ts +++ b/test/moves/shell_side_arm.test.ts @@ -1,5 +1,6 @@ import { BattlerIndex } from "#app/battle"; import { allMoves, ShellSideArmCategoryAttr } from "#app/data/moves/move"; +import type Move from "#app/data/moves/move"; import { Abilities } from "#enums/abilities"; import { Moves } from "#enums/moves"; import { Species } from "#enums/species"; @@ -10,8 +11,8 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vite describe("Moves - Shell Side Arm", () => { let phaserGame: Phaser.Game; let game: GameManager; - const shellSideArm = allMoves[Moves.SHELL_SIDE_ARM]; - const shellSideArmAttr = shellSideArm.getAttrs(ShellSideArmCategoryAttr)[0]; + let shellSideArm: Move; + let shellSideArmAttr: ShellSideArmCategoryAttr; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -24,6 +25,8 @@ describe("Moves - Shell Side Arm", () => { }); beforeEach(() => { + shellSideArm = allMoves[Moves.SHELL_SIDE_ARM]; + shellSideArmAttr = shellSideArm.getAttrs(ShellSideArmCategoryAttr)[0]; game = new GameManager(phaserGame); game.override .moveset([Moves.SHELL_SIDE_ARM, Moves.SPLASH]) diff --git a/test/moves/spit_up.test.ts b/test/moves/spit_up.test.ts index d986ae4d141..d71647bda52 100644 --- a/test/moves/spit_up.test.ts +++ b/test/moves/spit_up.test.ts @@ -7,6 +7,7 @@ import { MoveResult } from "#app/field/pokemon"; import GameManager from "#test/testUtils/gameManager"; import { Abilities } from "#enums/abilities"; import { Moves } from "#enums/moves"; +import type Move from "#app/data/moves/move"; import { Species } from "#enums/species"; import Phaser from "phaser"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; @@ -17,7 +18,7 @@ describe("Moves - Spit Up", () => { let phaserGame: Phaser.Game; let game: GameManager; - const spitUp = allMoves[Moves.SPIT_UP]; + let spitUp: Move; beforeAll(() => { phaserGame = new Phaser.Game({ type: Phaser.HEADLESS }); @@ -28,6 +29,7 @@ describe("Moves - Spit Up", () => { }); beforeEach(() => { + spitUp = allMoves[Moves.SPIT_UP]; game = new GameManager(phaserGame); game.override.battleType("single"); diff --git a/test/moves/tera_blast.test.ts b/test/moves/tera_blast.test.ts index dffe39f4d87..c1a2b999fa0 100644 --- a/test/moves/tera_blast.test.ts +++ b/test/moves/tera_blast.test.ts @@ -1,6 +1,7 @@ import { BattlerIndex } from "#app/battle"; import { Stat } from "#enums/stat"; import { allMoves, TeraMoveCategoryAttr } from "#app/data/moves/move"; +import type Move from "#app/data/moves/move"; import { PokemonType } from "#enums/pokemon-type"; import { Abilities } from "#app/enums/abilities"; import { HitResult } from "#app/field/pokemon"; @@ -13,13 +14,16 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vite describe("Moves - Tera Blast", () => { let phaserGame: Phaser.Game; let game: GameManager; - const moveToCheck = allMoves[Moves.TERA_BLAST]; - const teraBlastAttr = moveToCheck.getAttrs(TeraMoveCategoryAttr)[0]; + + let moveToCheck: Move; + let teraBlastAttr: TeraMoveCategoryAttr; beforeAll(() => { phaserGame = new Phaser.Game({ type: Phaser.HEADLESS, }); + moveToCheck = allMoves[Moves.TERA_BLAST]; + teraBlastAttr = moveToCheck.getAttrs(TeraMoveCategoryAttr)[0]; }); afterEach(() => { diff --git a/test/moves/triple_arrows.test.ts b/test/moves/triple_arrows.test.ts index c1114b69b99..eb434b25815 100644 --- a/test/moves/triple_arrows.test.ts +++ b/test/moves/triple_arrows.test.ts @@ -1,6 +1,7 @@ import { allMoves, FlinchAttr, StatStageChangeAttr } from "#app/data/moves/move"; import { Abilities } from "#enums/abilities"; import { Moves } from "#enums/moves"; +import type Move from "#app/data/moves/move"; import { Species } from "#enums/species"; import GameManager from "#test/testUtils/gameManager"; import Phaser from "phaser"; @@ -9,14 +10,17 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vite describe("Moves - Triple Arrows", () => { let phaserGame: Phaser.Game; let game: GameManager; - const tripleArrows = allMoves[Moves.TRIPLE_ARROWS]; - const flinchAttr = tripleArrows.getAttrs(FlinchAttr)[0]; - const defDropAttr = tripleArrows.getAttrs(StatStageChangeAttr)[0]; + let tripleArrows: Move; + let flinchAttr: FlinchAttr; + let defDropAttr: StatStageChangeAttr; beforeAll(() => { phaserGame = new Phaser.Game({ type: Phaser.HEADLESS, }); + tripleArrows = allMoves[Moves.TRIPLE_ARROWS]; + flinchAttr = tripleArrows.getAttrs(FlinchAttr)[0]; + defDropAttr = tripleArrows.getAttrs(StatStageChangeAttr)[0]; }); afterEach(() => { diff --git a/test/mystery-encounter/encounter-test-utils.ts b/test/mystery-encounter/encounter-test-utils.ts index 19ec364618e..8c54e0dd606 100644 --- a/test/mystery-encounter/encounter-test-utils.ts +++ b/test/mystery-encounter/encounter-test-utils.ts @@ -48,16 +48,6 @@ export async function runMysteryEncounterToEnd( ); if (isBattle) { - game.onNextPrompt( - "DamageAnimPhase", - Mode.MESSAGE, - () => { - game.setMode(Mode.MESSAGE); - game.endPhase(); - }, - () => game.isCurrentPhase(CommandPhase), - ); - game.onNextPrompt( "CheckSwitchPhase", Mode.CONFIRM, diff --git a/test/plugins/api/pokerogue-account-api.test.ts b/test/plugins/api/pokerogue-account-api.test.ts index 9ec98b6a59f..e7e1b2d52b0 100644 --- a/test/plugins/api/pokerogue-account-api.test.ts +++ b/test/plugins/api/pokerogue-account-api.test.ts @@ -4,11 +4,17 @@ import { PokerogueAccountApi } from "#app/plugins/api/pokerogue-account-api"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import * as Utils from "#app/utils"; import { http, HttpResponse } from "msw"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); const accountApi = new PokerogueAccountApi(apiBase); -const { server } = global; +let server: SetupServerApi; + +beforeAll(async () => { + server = await initServerForApiTests(); +}); afterEach(() => { server.resetHandlers(); diff --git a/test/plugins/api/pokerogue-admin-api.test.ts b/test/plugins/api/pokerogue-admin-api.test.ts index 0ce727b88da..08c4cf0dc45 100644 --- a/test/plugins/api/pokerogue-admin-api.test.ts +++ b/test/plugins/api/pokerogue-admin-api.test.ts @@ -9,11 +9,17 @@ import type { import { PokerogueAdminApi } from "#app/plugins/api/pokerogue-admin-api"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import { http, HttpResponse } from "msw"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); const adminApi = new PokerogueAdminApi(apiBase); -const { server } = global; +let server: SetupServerApi; + +beforeAll(async () => { + server = await initServerForApiTests(); +}); afterEach(() => { server.resetHandlers(); diff --git a/test/plugins/api/pokerogue-api.test.ts b/test/plugins/api/pokerogue-api.test.ts index 241453866a5..c53a38e23ab 100644 --- a/test/plugins/api/pokerogue-api.test.ts +++ b/test/plugins/api/pokerogue-api.test.ts @@ -2,10 +2,16 @@ import type { TitleStatsResponse } from "#app/@types/PokerogueApi"; import { pokerogueApi } from "#app/plugins/api/pokerogue-api"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import { http, HttpResponse } from "msw"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); -const { server } = global; +let server: SetupServerApi; + +beforeAll(async () => { + server = await initServerForApiTests(); +}); afterEach(() => { server.resetHandlers(); diff --git a/test/plugins/api/pokerogue-daily-api.test.ts b/test/plugins/api/pokerogue-daily-api.test.ts index 95d938e6625..563e6d09009 100644 --- a/test/plugins/api/pokerogue-daily-api.test.ts +++ b/test/plugins/api/pokerogue-daily-api.test.ts @@ -3,11 +3,17 @@ import { PokerogueDailyApi } from "#app/plugins/api/pokerogue-daily-api"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import { ScoreboardCategory, type RankingEntry } from "#app/ui/daily-run-scoreboard"; import { http, HttpResponse } from "msw"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); const dailyApi = new PokerogueDailyApi(apiBase); -const { server } = global; +let server: SetupServerApi; + +beforeAll(async () => { + server = await initServerForApiTests(); +}); afterEach(() => { server.resetHandlers(); diff --git a/test/plugins/api/pokerogue-savedata-api.test.ts b/test/plugins/api/pokerogue-savedata-api.test.ts index 47eafa0a933..481ba62c19d 100644 --- a/test/plugins/api/pokerogue-savedata-api.test.ts +++ b/test/plugins/api/pokerogue-savedata-api.test.ts @@ -2,11 +2,17 @@ import type { UpdateAllSavedataRequest } from "#app/@types/PokerogueSavedataApi" import { PokerogueSavedataApi } from "#app/plugins/api/pokerogue-savedata-api"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import { http, HttpResponse } from "msw"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); const savedataApi = new PokerogueSavedataApi(apiBase); -const { server } = global; +let server: SetupServerApi; + +beforeAll(async () => { + server = await initServerForApiTests(); +}); afterEach(() => { server.resetHandlers(); diff --git a/test/plugins/api/pokerogue-session-savedata-api.test.ts b/test/plugins/api/pokerogue-session-savedata-api.test.ts index 67abc9c9336..d4c235ac51a 100644 --- a/test/plugins/api/pokerogue-session-savedata-api.test.ts +++ b/test/plugins/api/pokerogue-session-savedata-api.test.ts @@ -10,11 +10,17 @@ import { PokerogueSessionSavedataApi } from "#app/plugins/api/pokerogue-session- import type { SessionSaveData } from "#app/system/game-data"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import { http, HttpResponse } from "msw"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); const sessionSavedataApi = new PokerogueSessionSavedataApi(apiBase); -const { server } = global; + +let server: SetupServerApi; +beforeAll(async () => { + server = await initServerForApiTests(); +}); afterEach(() => { server.resetHandlers(); diff --git a/test/plugins/api/pokerogue-system-savedata-api.test.ts b/test/plugins/api/pokerogue-system-savedata-api.test.ts index 6c04c7fa2ab..0c69ab8f922 100644 --- a/test/plugins/api/pokerogue-system-savedata-api.test.ts +++ b/test/plugins/api/pokerogue-system-savedata-api.test.ts @@ -10,11 +10,12 @@ import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import { http, HttpResponse } from "msw"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); const systemSavedataApi = new PokerogueSystemSavedataApi(getApiBaseUrl()); -let server; +let server: SetupServerApi; beforeAll(async () => { server = await initServerForApiTests(); diff --git a/test/testUtils/gameManager.ts b/test/testUtils/gameManager.ts index 981a2dbd2f1..390e71af126 100644 --- a/test/testUtils/gameManager.ts +++ b/test/testUtils/gameManager.ts @@ -57,7 +57,7 @@ import fs from "node:fs"; import { expect, vi } from "vitest"; import { globalScene } from "#app/global-scene"; import type StarterSelectUiHandler from "#app/ui/starter-select-ui-handler"; -import { MockFetch } from "./mocks/mockFetch"; +import { MockFetch } from "#test/testUtils/mocks/mockFetch"; /** * Class to manage the game state and transitions between phases. diff --git a/test/testUtils/gameWrapper.ts b/test/testUtils/gameWrapper.ts index 76de21e3984..28c7c4af80f 100644 --- a/test/testUtils/gameWrapper.ts +++ b/test/testUtils/gameWrapper.ts @@ -1,12 +1,21 @@ +/* eslint-disable */ // @ts-nocheck import BattleScene, * as battleScene from "#app/battle-scene"; import { MoveAnim } from "#app/data/battle-anims"; import Pokemon from "#app/field/pokemon"; +import * as Utils from "#app/utils"; +import { blobToString } from "#test/testUtils/gameManagerUtils"; import { MockClock } from "#test/testUtils/mocks/mockClock"; +import { MockConsoleLog } from "#test/testUtils/mocks/mockConsoleLog"; +import { MockFetch } from "#test/testUtils/mocks/mockFetch"; import MockLoader from "#test/testUtils/mocks/mockLoader"; +import { mockLocalStorage } from "#test/testUtils/mocks/mockLocalStorage"; +import { MockImage } from "#test/testUtils/mocks/mocksContainer/mockImage"; import MockTextureManager from "#test/testUtils/mocks/mockTextureManager"; import fs from "node:fs"; import Phaser from "phaser"; +import InputText from "phaser3-rex-plugins/plugins/inputtext"; +import BBCodeText from "phaser3-rex-plugins/plugins/bbcodetext"; import { vi } from "vitest"; import { MockGameObjectCreator } from "./mocks/mockGameObjectCreator"; import InputManager = Phaser.Input.InputManager; @@ -18,6 +27,20 @@ import UpdateList = Phaser.GameObjects.UpdateList; import { version } from "../../package.json"; import { MockTimedEventManager } from "./mocks/mockTimedEventManager"; +window.URL.createObjectURL = (blob: Blob) => { + blobToString(blob).then((data: string) => { + localStorage.setItem("toExport", data); + }); + return null; +}; +navigator.getGamepads = () => []; +global.fetch = vi.fn(MockFetch); +Utils.setCookie(Utils.sessionIdKey, "fake_token"); + +window.matchMedia = () => ({ + matches: false, +}); + export default class GameWrapper { public game: Phaser.Game; public scene: BattleScene; @@ -186,8 +209,6 @@ export default class GameWrapper { this.scene.time = new MockClock(this.scene); this.scene.remove = vi.fn(); // TODO: this should be stubbed differently this.scene.eventManager = new MockTimedEventManager(); // Disable Timed Events - - Pokemon.prototype.updateInfo = async () => {}; } } diff --git a/test/testUtils/testFileInitialization.ts b/test/testUtils/testFileInitialization.ts index 9deda972c77..2b41f3aa29a 100644 --- a/test/testUtils/testFileInitialization.ts +++ b/test/testUtils/testFileInitialization.ts @@ -1,10 +1,10 @@ -import { initLoggedInUser } from "#app/account"; import { SESSION_ID_COOKIE_NAME } from "#app/constants"; +import { initLoggedInUser } from "#app/account"; import { initAbilities } from "#app/data/ability"; -import { allMoves, initMoves } from "#app/data/moves/move"; import { initBiomes } from "#app/data/balance/biomes"; import { initEggMoves } from "#app/data/balance/egg-moves"; import { initPokemonPrevolutions } from "#app/data/balance/pokemon-evolutions"; +import { initMoves } from "#app/data/moves/move"; import { initMysteryEncounters } from "#app/data/mystery-encounters/mystery-encounters"; import { initPokemonForms } from "#app/data/pokemon-forms"; import { initSpecies } from "#app/data/pokemon-species"; @@ -22,6 +22,7 @@ import InputText from "phaser3-rex-plugins/plugins/inputtext"; import BBCodeText from "phaser3-rex-plugins/plugins/bbcodetext"; import { manageListeners } from "./listenersManager"; +let wasInitialized = false; /** * An initialization function that is run at the beginning of every test file (via `beforeAll()`). */ @@ -83,7 +84,9 @@ export function initTestFile() { HTMLCanvasElement.prototype.getContext = () => mockContext; // Initialize all of these things if and only if they have not been initialized yet - if (allMoves.length === 0) { + // initSpecies(); + if (!wasInitialized) { + wasInitialized = true; initVouchers(); initAchievements(); initStatsKeys(); diff --git a/test/vitest.setup.ts b/test/vitest.setup.ts index 2e1fd426891..b9da0850306 100644 --- a/test/vitest.setup.ts +++ b/test/vitest.setup.ts @@ -1,7 +1,23 @@ import "vitest-canvas-mock"; + +import { initLoggedInUser } from "#app/account"; +import { initAbilities } from "#app/data/ability"; +import { initBiomes } from "#app/data/balance/biomes"; +import { initEggMoves } from "#app/data/balance/egg-moves"; +import { initPokemonPrevolutions } from "#app/data/balance/pokemon-evolutions"; +import { initMoves } from "#app/data/moves/move"; +import { initMysteryEncounters } from "#app/data/mystery-encounters/mystery-encounters"; +import { initPokemonForms } from "#app/data/pokemon-forms"; +import { initSpecies } from "#app/data/pokemon-species"; +import { initAchievements } from "#app/system/achv"; +import { initVouchers } from "#app/system/voucher"; +import { initStatsKeys } from "#app/ui/game-stats-ui-handler"; import { afterAll, beforeAll, vi } from "vitest"; + import { initTestFile } from "./testUtils/testFileInitialization"; +/** Set the timezone to UTC for tests. */ + /** Mock the override import to always return default values, ignoring any custom overrides. */ vi.mock("#app/overrides", async importOriginal => { // eslint-disable-next-line @typescript-eslint/consistent-type-imports @@ -9,9 +25,8 @@ vi.mock("#app/overrides", async 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 }, + defaultOverrides, + // eslint-disable-next-line @typescript-eslint/consistent-type-imports } satisfies typeof import("#app/overrides"); }); @@ -52,11 +67,7 @@ vi.mock("i18next", async importOriginal => { global.testFailed = false; beforeAll(() => { - console.log("====================================="); - console.log("Initializing test file..."); initTestFile(); - console.log("Test file initialized!"); - console.log("====================================="); }); afterAll(() => { diff --git a/vitest.config.ts b/vitest.config.ts index 3026d6349d9..c781bde97ed 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -3,7 +3,7 @@ import { defaultConfig } from "./vite.config"; import { BaseSequencer, type TestSpecification } from "vitest/node"; function getTestOrder(testName: string): number { - if (testName.includes("battle_scene.test.ts")) { + if (testName.includes("battle-scene.test.ts")) { return 1; } if (testName.includes("inputs.test.ts")) {