Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injected mocks add functionality rather than replace it #182

Open
vileppanen opened this issue May 12, 2020 · 1 comment
Open

Injected mocks add functionality rather than replace it #182

vileppanen opened this issue May 12, 2020 · 1 comment

Comments

@vileppanen
Copy link

vileppanen commented May 12, 2020

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:

  1. Clone https://github.com/vileppanen/rewire-sandbox
  2. Install deps: npm ci
  3. Run npm test
  4. 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.

@vileppanen vileppanen changed the title Injecting mocks add functionality rather than replacing it Injecting mocks add functionality rather than replace it May 12, 2020
@vileppanen vileppanen changed the title Injecting mocks add functionality rather than replace it Injected mocks add functionality rather than replace it May 12, 2020
@vileppanen
Copy link
Author

vileppanen commented May 17, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant