I saw the documentation for Jest mocks using the mocks folder, but I want to wind a module with one layout in one test and mock the same module with a different layout in another test.
For example, with rewire and jasmine, you can do something like this:
//module2.js module.exports = { callFoo: function () { require('moduleToMock').foo(); } }; //module2Test.js describe("test1", function () { var mock; beforeEach(function () { var rewire = require('rewire'); mock = jasmine.createSpyObj('mock', ['foo']); }); it("should be mocked with type1", function () { mock.foo.and.returnValue("type1"); rewire('moduleToMock', mock); var moduleUsingMockModule = require('module2'); expect(moduleUsingMockModule.callFoo()).toEqual("type1"); }); }); describe("test2", function () { it("should be mocked with type2", function () { mock.foo.and.returnValue("type2"); rewire('moduleToMock', mock); var moduleUsingMockModule = require('module2'); expect(moduleUsingMockModule.callFoo()).toEqual("type2"); }); });
Can this be done with Jest? The difference is that I define the layout in the test, and not in some external folder that is used for all tests.
unit-testing mocking jestjs
Evan layman
source share