Open JemiloII opened 7 years ago
const {a, b} = require('./numbers.js');
const add = () => a + b;
Here a
and b
are evaluated right away and thus cannot be changed when you mock require('./numbers.js')
const numbers = require('./numbers.js');
const add = () => numbers.a + numbers.b;
Whereas here numbers.a
and numbers.b
aren't evaluated until you call add()
, therefore mocking numbers
work in this case.
This would also not work for the same reason:
const numbers = require('./numbers.js');
const a = numbers.a
const b = numbers.b
const add = () => a + b;
Ahhh, okay thanks for the clarification!
Just trying to use some destructing.
If I have.
The test fails when I go to mock numbers. However, if I rewrite the module to not have destructuring...
My test will pass. It's kinda annoying actually. Is there anything I can do about this? I thought the mockery was suppose to overwrite the require. Or is mockery simply just passing the object around and with destructuring, new objects are created rather than references?