Vra

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.

Was dit nuttig?

Oplossing 2

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

Ander wenke

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

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

Gelisensieer onder: CC-BY-SA met toeskrywing
Nie verbonde aan StackOverflow
scroll top