How to do some configuration before each test run of Jest

I am writing tests for a React application that uses Fluxxor to provide an event dispatcher. Doing this requires Jest not to make fun of the few modules that are used internally and provided by Node themselves.

This means that I can’t just add them to the unmockedModulePathPatterns configuration key, but should use the following code instead:

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

However, I cannot find anything useful to use it. I have setupEnvScriptFile that installs several globals that I use in almost all of my tests, but the jest object does not seem to be available in this context, so I cannot just install mocks there.

As a hacker stop measure, I wrapped the code above in the function that I call at the beginning of any describe block for testing Fluxxor stores, but this is far from ideal.

+8
unit-testing reactjs reactjs-flux jestjs
source share
3 answers

It seems that the answer, at least for the time being, is β€œyou cannot in this case,” but there are problems open to two changes that need to be made to support it.

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

0
source share

Have you tried config.setupTestFrameworkScriptFile ? Looks like this would be a good place for monkey api patches, according to the docs.

+2
source share

FWIW. Here is the solution we used to add Fluxxor and React-Router support to our test specifications.

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

0
source share

All Articles