You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Stumbled across a scenario, where I had passing tests written for a function with seemingly working mocks to ensure the function works correctly.
Afterwards, I was starting to refactor the mocked functionality into separate modules, and for clarity, ran the tests at time when the mocked functions would not exist anymore in the tested module. I was expecting the test that verified the mocked function was called to fail due to the function being undefined, but was surprised that the test actually passed.
Investigated how things work when using __with__ (I assume set works kinda similar way) to provide mocked functionality, and it seems to indeed inject the exact mock functionality into tested module, even though it is not part of the real implementation.
Ie. if you provide a mock function in test, and forget to require the same function in the tested module before calling it --> test verifying the mock was called will pass.
I had issues to setup a lean Codesandbox demo for this, so had to rely on separate dummy repo with demonstrating unit test. So in order to reproduce:
Ensure, that the single unit test will pass, even though it should throw error from the undefined bar function.
Dunno if rewire is supposed to work this way, but for me from mocking perspective, this seems like a huge pitfall, when refactoring code and relying on unit tests to verify things do not break unexpectedly. Before running into this, I was assuming rewire would search for keys that do exist in the tested module and replace them, instead of really injecting new functionality into tested module.
The text was updated successfully, but these errors were encountered:
vileppanen
changed the title
Injecting mocks add functionality rather than replacing it
Injecting mocks add functionality rather than replace it
May 12, 2020
vileppanen
changed the title
Injecting mocks add functionality rather than replace it
Injected mocks add functionality rather than replace it
May 12, 2020
Out of curiosity, created this micro-library, which provides a wrapper around rewire to expose __replaceWith__ function that calls the actual __with__ function, after it has removed the mocked entities that do not exist in the tested module. Whether this same behavior would be implemented in the rewire module, I'd gladly hear in order to deprecate the micro-lib project.
Stumbled across a scenario, where I had passing tests written for a function with seemingly working mocks to ensure the function works correctly.
Afterwards, I was starting to refactor the mocked functionality into separate modules, and for clarity, ran the tests at time when the mocked functions would not exist anymore in the tested module. I was expecting the test that verified the mocked function was called to fail due to the function being undefined, but was surprised that the test actually passed.
Investigated how things work when using
__with__
(I assume set works kinda similar way) to provide mocked functionality, and it seems to indeed inject the exact mock functionality into tested module, even though it is not part of the real implementation.Ie. if you provide a mock function in test, and forget to require the same function in the tested module before calling it --> test verifying the mock was called will pass.
I had issues to setup a lean Codesandbox demo for this, so had to rely on separate dummy repo with demonstrating unit test. So in order to reproduce:
npm ci
npm test
bar
function.Dunno if
rewire
is supposed to work this way, but for me from mocking perspective, this seems like a huge pitfall, when refactoring code and relying on unit tests to verify things do not break unexpectedly. Before running into this, I was assumingrewire
would search for keys that do exist in the tested module and replace them, instead of really injecting new functionality into tested module.The text was updated successfully, but these errors were encountered: