How Do I Run Some Config Before Every Jest Test Run

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.



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.