Jasmine's BeforeAll inner functions are called after a spec

Jasmine's BeforeAll inner functions are called after a spec



I am making a game with the Phaser-framework and I am coding automatic tests with Jasmine. All works fine in this code exept the function beforeAll() (called after a it (spec)) The console prints:
test2
test
when it should print test test2. I tried the beforeEach() but it does not make any difference.


describe("Hit Box", function()
var collide = false;
beforeAll(function()

game = new Phaser.Game(400, 400, Phaser.AUTO, '', preload: preload, create: create, render:render, false, true);

function preload()

game.load.image('blue', 'assets/magicien100.png');

game.load.image('fire_ball', 'assets/red.png');



function create()
game.physics.startSystem(Phaser.Physics.ARCADE);
game.world.setBounds(0, 0, 400, 400);
game.dummyPlayer = game.add.sprite(100,100,'blue');

game.dummyPlayer.width = 100;
game.dummyPlayer.height = 100;

game.physics.arcade.enable(game.dummyPlayer);

game.dummySpell = game.add.sprite(50, 50, 'fire_ball');

game.dummySpell.width = 75;
game.dummySpell.height = 75;

game.physics.arcade.enable(game.dummySpell);

game.debug.spriteBounds(game.dummyPlayer);
game.debug.spriteBounds(game.dummySpell);

if (game.physics.arcade.overlap(game.dummyPlayer, game.dummySpell))
collide = true;
console.log('test');



function render()
game.debug.spriteBounds(game.dummyPlayer);
game.debug.spriteBounds(game.dummySpell);


);


it("Should have a collision", function()
expect(collide).toBe(true);
console.log('test2');
);
);






maybe new Phaser.Game is async?

– marzelin
Sep 6 '18 at 16:42


new Phaser.Game






Have you tried to use done in beforeAll?

– marzelin
Sep 6 '18 at 16:43


done


beforeAll






those tests make no sense. You do all logic in beforeAll and only do checking in it. it should do something and then test the result.

– marzelin
Sep 6 '18 at 16:46


beforeAll


it


it






you should only unit test your own functions (pure functions preferably) not the framework itself.

– marzelin
Sep 6 '18 at 16:57




1 Answer
1



If Phaser.Game is asynchronous, it may still be creating when the it function is run.



Try returning a promise from the beforeAll:


describe("Hit Box", function() {

var collide = false;
beforeEach(function()
return new Promise(function(resolve, reject)

game = new Phaser.Game(400, 400, Phaser.AUTO, '', preload: preload, create: create, render:render, false, true);

function preload()
// preload code


function create()
// create code
resolve(); // to complete the promise

);



);






I tested what you told me to do but everything is fine. I put more console.log() and I figured out that the functions preload, create and render are called AFTER the spec but I have no idea why.

– JuZDePeche
Sep 6 '18 at 16:10






Is there anything asynchronous about how Phaser.Game is created. Perhaps it is still running when the it function is called. You could take a look at the async documentation from Jasmine: jasmine.github.io/tutorials/async

– Joel Richman
Sep 6 '18 at 16:38






I updated the answer with an asynchronous solution.

– Joel Richman
Sep 6 '18 at 16:49






It worked! The spec now waits the preload and create function.

– JuZDePeche
Sep 6 '18 at 19:11






Glad to hear that it worked. When you have a moment can you mark it as the accepted answer?

– Joel Richman
Sep 6 '18 at 19:53



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)