question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 5 years ago
  • Reactions:6
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
Akryumcommented, Oct 17, 2019

Here is a runnable reproduction of the bug:

https://codesandbox.io/s/boring-wilbur-ibkmj

Reproduction steps

1 - Un-comment globalSetup line in jest.config.js:

globalSetup: './tests/globalSetup.js'

2 - Run the following in the terminal:

yarn test

Error output:

Error: Cannot find module '@/utils'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/sandbox/tests/globalSetup.js:3:5)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Module._compile (/sandbox/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Object.newLoader [as .js] (/sandbox/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:653:32)
0reactions
wanton7commented, Feb 9, 2022

I had to do my own monkey patching of require in globalSetup 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.

const Mod = require('module');
const { compilerOptions } = require('../../tsconfig');
var path = require('path');

const currentDir = process.cwd();

const req = Mod.prototype.require;

Mod.prototype.require = function () {
  const modulePath = arguments[0];
  for (const fromPath of Object.keys(compilerOptions.paths)) {
    let toPath = compilerOptions.paths[fromPath][0];
    if (!fromPath || !fromPath.endsWith('/*') || !toPath || !toPath.endsWith('/*')) {
      throw Error('Only /* suffix supported');
    }
    if (!toPath.startsWith('./')) throw Error('Only destination prefix ./ supported');

    if (modulePath.startsWith(fromPath.slice(0, -1))) {
      arguments[0] = path.join(currentDir, toPath.slice(2, -1), modulePath.slice(fromPath.length - 1));
    }
  }

  return req.apply(this, arguments);
};
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found