9

I'm writing tests for a React application which makes use of Fluxxor to provide an event dispatcher. Making that work requires telling Jest not to mock a few modules which are used internally, and are provided by Node itself.

That means I can't just add them to the unmockedModulePathPatterns config key, and instead have to use some code like this:

[ 'util', 'events' ].forEach(function (module) {
  jest.setMock(module, require.requireActual(module));
});

However, I can't find anywhere useful to put it. I've got a setupEnvScriptFile which sets up a few globals that I use in almost all my tests, but the jest object doesn't seem to be available in that context, so I can't just set the mocks there.

As a hacky stopgap measure I've wrapped the code above in a function which I call at the beginning of any describe blocks testing Fluxxor stores, but its far from ideal.

Jon Wood
  • 2,589
  • 1
  • 20
  • 17
  • Why exactly can't you use `unmockedModulePathPatterns`? – Felix Kling Jul 30 '14 at 03:06
  • I honestly don't know precisely *why* it doesn't work for me, but any attempts I've made at adding core Node modules to `unmockedModulePathPatterns` has had no effect when I've tried it with the following patterns: `util` `/usr/lib/node/util` `/usr/lib/node` In every case the `util` module gets mocked anyway. – Jon Wood Jul 30 '14 at 08:21
  • Oh, could it be that `util` itself might not be mocked, but that it loads other modules which get mocked? – Felix Kling Jul 30 '14 at 14:37
  • Nope, definitely `util` itself that gets mocked, since when I use `require.requireActual` I get the expected `util` object, and if I don't, then I get a Jest mock. – Jon Wood Jul 30 '14 at 15:30
  • I'm coming across the same issue. From looking at the Jest HasteModuleLoader, all node modules listed in NODE_CORE_MODULES get mocked and it doesn't look in the unmockedModulePathPatterns. Temporarily, I tried updating the _shouldMock method to return false for "util", but then I got a "no such file or directory 'util'" error. So it looks like there is a bigger code change than just telling jest it should not be mocked. So now, I will have to do something like Jon did, except I am able simply do jest.dontMock('util') instead of using setMock in all of my tests. – Randy Aug 05 '14 at 17:06
  • With jasmine it was really easy to add some code that would execute before EVERY suite. You just had to define a "Runner beforeEach" in any of your tests and you were done (https://github.com/pivotal/jasmine/wiki/Before-and-After). "Runner beforeEach() functions are executed before every spec in all suites." Whith Jest, it looks like the "global" beforeEach only impacts the tests in the same file :( This works just fine in jasmine. Everything else I try, I get a "jest is not defined error". I believe you have found the optimal solution, unless some changes are made to Jest. – Randy Aug 05 '14 at 17:59
  • 1
    https://github.com/facebook/jest/issues/106 and https://github.com/facebook/jest/issues/107 were opened for these two issues. – Randy Aug 05 '14 at 18:12

3 Answers3

2

Have you tried config.setupTestFrameworkScriptFile? Seems like it would be the right place to monkey patch the api, as per the docs.

Naruto Sempai
  • 6,233
  • 8
  • 35
  • 51
  • 3
    Docs seem to be updated - I found the details here: http://facebook.github.io/jest/docs/en/configuration.html#setuptestframeworkscriptfile-string – Jono Job Sep 28 '17 at 00:13
0

It seems that the answer, at least currently, is "you can't in this case", but there are issues open for the two changes that need to be made to support it.

https://github.com/facebook/jest/issues/106 https://github.com/facebook/jest/issues/107

Jon Wood
  • 2,589
  • 1
  • 20
  • 17
0

FWIW, Here's a solution that we have been using to add Fluxxor and React-Router support to our test specs.

https://gist.github.com/adjavaherian/a15ef0461e65d58aacd2

4m1r
  • 12,234
  • 9
  • 46
  • 58