Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Why am I able to mutate a module in a test and see my changes in my implementation file?
- ```js
- // 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.
Add Comment
Please, Sign In to add comment