60frames / jestpack

Jest Webpack Integration
MIT License
66 stars 3 forks source link

Auto mocking #11

Closed justinwyllie closed 8 years ago

justinwyllie commented 8 years ago

This is brilliant. Thank you.

I have question about auto mocking. I am testing React components which use ReactBootstrap.

If I explicitly call : jest.mock('react-bootstrap') I get the behaviour I would expect with a mocked dependency. However; if I don't do that I would expect to get exactly the same behaviour because the default is to mock?

Does your module loader not automatically mock or (and I think this is the case) I am missing something here?

justinwyllie commented 8 years ago

ok. in general jest auto mocking works as expected. i think this may be an issue with my configuration.

richardscarrott commented 8 years ago

You're right in saying it defaults to auto mocking -- I've tried to keep the mocking behaviour as similar to Jest's default module loader as possible.

Perhaps 'react-bootstrap' is being matched by a path in Jest's config.unmockedModulePathPatterns as that would prevent it from automatically being mocked but would still allow you to explicitly mock it with jest.mock?

richardscarrott commented 8 years ago

Also, if you can't figure it out then feel free to paste your config or send a link and I'll see if I can help.

justinwyllie commented 8 years ago

Thanks for this.

This is my unmockedModulePatterns config:

"unmockedModulePathPatterns": [ "|react$|", "react-addons-linked-state-mixin", "react-addons-pure-render-mixin", "react-addons-test-utils", "react-dom", "react-overlays", "react-prop-types", "react-router", "react-tools", "fbjs", "react-dom", "core-js", "babel-runtime", "classnames", "invariant", "keycode", "lodash-compat", "react-overlays", "react-prop-types", "react-overlays", "uncontrollable", "warning", "babel", "babel-jest", "babel-core", "dom-helpers", "bundle-loader" ]

The regexp (have I done this right) for react is to try to prevent it matching react-bootstrap. The proof that it is not mocked by default is if I do:

B = require 'react-bootstrap' console.log(B.Row)

then the output shows a 'real' module.

But if I do

jest.mock 'react-bootstrap' before those two lines the output shows a mocked module

Thanks for your help