'Jest - how to isolate tests when they use the same class?
I am trying to test a class with some properties and methods on it and running into some strange behaviour of the new keyword using Jest. The code that is being tested is for a simple vote based game. Players vote to either pass or fail a mission and the code in question should either increment the 'pass' property or increment the 'fail' property on class GameController.
import GameController from '../gameController';
import {room,largeRoomSeven,largeRoomTen} from '../../models/__tests__/mocks/room_mock';
import {playersMock} from './mocks/mock_players';
import {mockRoom,mockRoomCode} from './mocks/mock_room';
const setupTest = () => {
let gameController = new GameController();
let game = gameController.createGame(mockRoom);
return {
gameController,
game
}
}
test('updatePlayerVote called with true should increment number of passes by 1',()=>{
let {gameController,game} = setupTest();
gameController.setGameWithRoomcode(gameController.updatePlayerVote(game,'xudsbfjsd',true));
expect(game.rounds[game.currentRound].numberOfFail).toEqual(0);
expect(game.rounds[game.currentRound].numberOfPass).toEqual(1);
})
test('updatePlayerVote called with false should increment number of fails by 1',()=>{
let {gameController,game} = setupTest();
gameController.setGameWithRoomcode(gameController.updatePlayerVote(game,'xudsbfjsd',false));
expect(game.rounds[game.currentRound].numberOfPass).toEqual(0);
expect(game.rounds[game.currentRound].numberOfFail).toEqual(1);
})
The method updatePlayerVote on GameController simply increments a property on an object passed to it and returns the updated object:
updatePlayerVote = function(gameToUpdate,selfId,missionPass){
let game = gameToUpdate;
missionPass ? game.rounds[game.currentRound].numberOfPass++ : game.rounds[game.currentRound].numberOfFail++;
game.rounds[game.currentRound].playersOnMission.push(selfId);
return game;
}
I am expecting that for the first test that numberOfFail should be 0 and numberOfPass should be 1. For the second test that numberOfFail should be 1 and numberOfPass should be 0. However the second test fails because both are 1. The error Jest gives is as follows:
expect(received).toEqual(expected) // deep equality
Expected: 0 Received: 1
When I comment out the first test the second test passes leading me to believe that the test failing is due to my misunderstanding of Jest. Is this issue caused because the tests run in parallel and so both tests access the same instance of GameController? Why does using new GameController() not actually create an entirely new instance specific to the test which is running? If I replace the code above with the code below the same test result occurs even though the two instances of GameController should be completely independent between tests.
const setupTest = () => {
const gameController = new GameController();
const game = gameController.createGame(mockRoom);
return [
gameController,
game
]
}
test('updatePlayerVote called with true should increment number of passes by 1',()=>{
let [gameController1,game1] = setupTest();
expect(game1.rounds[game1.currentRound].numberOfFail).toEqual(0);
expect(game1.rounds[game1.currentRound].numberOfPass).toEqual(0);
game1 = gameController1.updatePlayerVote(game1,'xudsbfjsd',true);
expect(game1.rounds[game1.currentRound].numberOfFail).toEqual(0);
expect(game1.rounds[game1.currentRound].numberOfPass).toEqual(1);
})
test('updatePlayerVote called with false should increment number of fails by 1',()=>{
let [gameController2,game2] = setupTest();
expect(game2.rounds[game2.currentRound].numberOfFail).toEqual(0);
expect(game2.rounds[game2.currentRound].numberOfPass).toEqual(0);
game2 = gameController2.updatePlayerVote(game2,'xudsbfjsd',true);
expect(game2.rounds[game2.currentRound].numberOfFail).toEqual(1);
expect(game2.rounds[game2.currentRound].numberOfPass).toEqual(0);
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
