reactjs - How to mock return value in another file? -


i'm testing actions file in flux jest , can't seem figure out how mock return value in it.

when specify return value (see code below) mocked module in test file, works fine, when same function called in module i'm testing, comes undefined.

#missionactions-test.js  jest.dontmock('../missionactions'); describe('missionactions', function() {     var gamestore = require(root + 'stores/game/gamestore');     var missionactions;      beforeeach(function() {         missionactions = require('../missionactions');     });      it('should...', function() {         gamestore.getgame.mockreturnvalue({test: "test"});         console.log(gamestore.getgame()); // prints {test : "test"}         missionactions.addmissionfrombank();     }); }); 

and missionactions.js being tested.

# missionactions.js var gamestore = require('../../stores/game/gamestore');  var missionactions = {     addmissionfrombank: function(bankmission) {         var game = gamestore.getgame();         console.log(game); // prints undefined     } }  module.exports = missionactions; 

my interpretation of api & automatic mocking setting mockreturnvalue on mocked module (in case gamestore) applies throughout test, not within test file.

how would go mocking gamestore.getgame() method applies within missionactions.js well?

i've run before well.

the fix is, in test, move gamestore require beforeeach block.

beforeeach(function() {     missionactions = require('../missionactions');     gamestore = require(root + 'stores/game/gamestore'); }); 

i'm not 100% sure on this, believe mockregistry scoped per test. when require inside of describe block, mocks expect. then, when require missionactions inside of beforeeach, starts new scope. mocks missionactions, starts mock it's dependencies , brand new mock of gamestore.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -