How do I run some config before every Jest test run
Asked Answered
M

3

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.

Magician answered 29/7, 2014 at 14:10 Comment(7)
Why exactly can't you use unmockedModulePathPatterns?Disgusting
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.Magician
Oh, could it be that util itself might not be mocked, but that it loads other modules which get mocked?Disgusting
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.Magician
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.Thagard
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 (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.Thagard
github.com/facebook/jest/issues/106 and github.com/facebook/jest/issues/107 were opened for these two issues.Thagard
M
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

Magician answered 8/8, 2014 at 17:4 Comment(0)
W
2

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

Whydah answered 9/1, 2015 at 12:31 Comment(1)
Docs seem to be updated - I found the details here: facebook.github.io/jest/docs/en/…Estragon
M
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

Magician answered 8/8, 2014 at 17:4 Comment(0)
D
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

Dietsche answered 2/3, 2015 at 19:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.