globalSetup doesn't process styleMocks / moduleNameMappers
See original GitHub issue🐛 Bug Report
globalSetup does not work with moduleNameMappers. In most setups this would be ok, but there are situations such as SSR where you want to setup a server once, you can run into issues with this.
Related to: https://github.com/facebook/jest/issues/5164
To Reproduce
- Set up a styleMock and a globalSetup file
- import a React component that imports a stylesheet in a globalSetup file
Expected behavior
globalSetup processes moduleNameMappers. The idea that globalSetup runs before “everything” is not practical. Setup will generally require some configuration options for things to work.
Run npx envinfo --preset jest
Paste the results here:
System:
OS: macOS Sierra 10.12.6
CPU: x64 Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz
Binaries:
Node: 8.9.4 - ~/.nvm/versions/node/v8.9.4/bin/node
Yarn: 1.5.1 - /usr/local/bin/yarn
npm: 6.0.0 - /Users/Shared/java/projects/react/projects/mobile-minds/node_modules/.bin/npm
npmPackages:
jest: 22.4.3 => 22.4.3
The workaround I currently use for both this issue and the related babel-transpile issue:
require('babel-register');
require('babel-polyfill');
['.css', '.eot', '.woff', '.ttf'].forEach(ext => {
require.extensions[ext] = () => ({});
});
['.svg', '.png', '.jpg'].forEach(ext => {
require.extensions[ext] = () => '';
});
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:9 (3 by maintainers)
Top Results From Across the Web
How to use at modules imports (@) in Jest's globalSetup ...
I've described moduleNameMapper and it's working in tests, but seems it is not in setupGlobal function. How can I fix that? As I...
Read more >Using with webpack - Jest
Jest can be used in projects that use webpack to manage assets, styles, and compilation. webpack does offer some unique challenges over ...
Read more >Top 5 jest-config Code Examples - Snyk
To help you get started, we've selected a few jest-config examples, based on popular ways it is used in public projects. Secure your...
Read more >Testing React Typescript with Jest and Enzyme - Medium
However, when we run “npm run test”, jest does not recognize all the syntaxes beyond ... create a file styleMock.js for jest to...
Read more >9 JEST Configuration Parameters for testing your React App
This feature is useful for the production environment where even the failure of one test case willhalt the complete deployment process. 2. moduleNameMapper...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here is a runnable reproduction of the bug:
https://codesandbox.io/s/boring-wilbur-ibkmj
Reproduction steps
1 - Un-comment
globalSetup
line injest.config.js
:2 - Run the following in the terminal:
Error output:
I had to do my own monkey patching of
require
inglobalSetup
to make tsconfig “paths” work. I hope this code helps someone else. I run this code top of global setup JavaScript file. It doesn’t support all kinds of paths supported in tsconfig but works in cases we have.