theKashey/rewiremock

Not getting any results at all using mocha/node

bwillem opened this issue · 4 comments

Mocking seems to not do anything. The node runtime still looks for the dependency from the project's node_modules directory, and ignores the stub.

// Component.js
import dependency from "dependency-in-node-modules"

export default () => {
  dependecy.method();
 
  return <div />;
}

// component.test.js
import Component from "../Component"
import render from "react-native-testing-library

rewiremock("dependency-in-node-modules").with(() => null);
rewiremock.enable();

render(<Component  />);

Runtime is still looking here: /project/node_modules/dependency-in-node-modules/

That's how it should work - you've already imported Component and nothing could change it.

There are two ways to fix:

  1. Require it later
// component.test.js
-import Component from "../Component"
import render from "react-native-testing-library

rewiremock("dependency-in-node-modules").with(() => null);
rewiremock.enable();
+const Component = require("../Component"); // require when rewiremock is active
render(<Component  />);
  1. Use babel plugin (see readme section about it)
// component.test.js
+ import rewiremock from 'rewiremock'; // should be imported "above" Component

import Component from "../Component"
import render from "react-native-testing-library

rewiremock("dependency-in-node-modules").with(() => null);
- rewiremock.enable(); no need to enable it

render(<Component  />);

having the same issue in a typescript x mocha project. rewiremock seems to do absolutely nothing, even when using the babel plugin to support jest-like hoisting.

@unicornware - any chance to provide an example I can fix?

@theKashey i would, but i already removed rewiremock from my project and don't have the bandwidth to do a re-drop and repro at the moment. if i get a chance, i'll open a new issue