// Test.js
import createHistory from 'history/createBrowserHistory';
describe('something', () => {
let pushSpy;
beforeEach(() => {
pushSpy = jest.fn();
createHistory = function() {
return {
push: pushSpy;
}
}
});
it('does something', () => {
subject.doThing();
// Why does this test pass if I haven't explicitly told Implementation.js
// to use my mutated copy of createHistory?
expect(pushSpy).toHaveBeenCalledWith("parameter")
});
});
// Implementation.js
import createHistory from 'history/createBrowserHistory';
export default function doThing() {
let history = createHistory();
history.push("parameter")
}
Answer: When you import a module, webpack gives you a reference to the same object every time. This is because webpack caches imports. Therefore changes you make to a module imported in one file will propagate anywhere else you import that module.