Guest User

Untitled

a guest
Feb 24th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. # Why am I able to mutate a module in a test and see my changes in my implementation file?
  2. ```js
  3. // Test.js
  4.  
  5. import createHistory from 'history/createBrowserHistory';
  6.  
  7. describe('something', () => {
  8. let pushSpy;
  9. beforeEach(() => {
  10. pushSpy = jest.fn();
  11. createHistory = function() {
  12. return {
  13. push: pushSpy;
  14. }
  15. }
  16. });
  17.  
  18. it('does something', () => {
  19. subject.doThing();
  20.  
  21. // Why does this test pass if I haven't explicitly told Implementation.js
  22. // to use my mutated copy of createHistory?
  23. expect(pushSpy).toHaveBeenCalledWith("parameter")
  24. });
  25. });
  26.  
  27. // Implementation.js
  28.  
  29. import createHistory from 'history/createBrowserHistory';
  30.  
  31. export default function doThing() {
  32. let history = createHistory();
  33. history.push("parameter")
  34. }
  35. ```
  36.  
  37. **Answer**: When you import a module, webpack gives you a reference to the same object every time. This is because webpack caches imports.
  38. 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