phaser
phaser copied to clipboard
game.scene.scenes.length is set to 0 / expected : 1
Version
-
Phaser Version: Phaser v3.55.2-FB (Headless | HTML5 Audio)
-
Operating system: Windows 10
-
Browser: Chrome Version 103.0.5060.114 (Build officiel) (64 bits)
Description
I want to jest my phaser game (scenes and prefabs)
First I want to validate Phaser game and first scene are ok to continue my unit tests.
I'm creating a Phaser game, with HEADLESS type (for unit test purpose).
When I want to get my scene from phaser game, I'm not able to do it.
Example Test Code
const game = new Phaser.Game({ type: Phaser.HEADLESS, scene: { preload: () => { } } });
console.log('game.scene.scenes', game.scene.scenes.length); => return 0 => Why ?
So this : const scene = game.scene.getAt(0); console.log('loadGame::scene', scene); it displays null
To add infos : I'm using jest inside a react project (created with create react app CLI)
Here the code I'm using to call game creation
describe('PlayerPrefab', () => { beforeAll(() => {});
beforeEach(() => {
game = loadGameWithTestScene();
});
Additional Information
In my game (in chrome browser), without unit test, i haven't get any error.
I think that's normal. Use the postBoot
callback instead.
@samme you mean : https://newdocs.phaser.io/docs/3.55.2/focus/Phaser.Core.Config-postBoot ?
Yes.
The Scene passed in the game config is added to the internal and private _pending
array until the Scene Manager receives the READY
event from the Game instance. After this, it will process the pending array and create Scenes from the given data. Only once this is complete (POST_BOOT
) will the Scene array have anything in it.