Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

globalSetup doesn't process styleMocks / moduleNameMappers #6179

Closed
mschipperheyn opened this issue May 13, 2018 · 12 comments
Closed

globalSetup doesn't process styleMocks / moduleNameMappers #6179

mschipperheyn opened this issue May 13, 2018 · 12 comments
Labels

Comments

@mschipperheyn
Copy link

mschipperheyn commented May 13, 2018

🐛 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: #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] = () => '';
});

@alexk111
Copy link

Any updates on this issue?

@rickhanlonii
Copy link
Member

This seems like a feature request, can you post a full example of the bug?

@dortamiguel

This comment has been minimized.

@Akryum
Copy link

Akryum commented 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)

@stnwk
Copy link

stnwk commented Apr 5, 2020

Hi everyone! friendly ping

@rickhanlonii @SimenB

This seems like a feature request, can you post a full example of the bug?

I have to disagree. I don't think this is a feature request but rather a bug:
The moduleNameMapper configuration is correctly respected in all setupFilesAfterEnv and setupFiles for example. globalSetup and globalTeardown surprisingly don't respect that :/

Reproduction on latest jest version

I prepared a simple reproduction on the latest jest version 25.2.7, see https://codesandbox.io/s/jest-globalsetup-modulenamemapper-bug-g1ol7 and the README.

This is currently preventing us from speeding up our test setup substantially, because we cannot re-use a service for all tests and instead have to re-create it for every test file by putting it in a setupFiles entry. We'd love to see this fixed, let me know if I can be of any more help!

Thank you :)

@rickhanlonii
Copy link
Member

Thanks for the repro cases, I agree this is a bug now (or a feature request to change the behavior, however you want to look at it). If anyone wants to submit a PR to fix it I'm happy to review!

@stnwk
Copy link

stnwk commented Apr 20, 2020

If anyone wants to submit a PR to fix it I'm happy to review!

@rickhanlonii I'm happy to take a look at it. Do you have any recommendations into where I might start looking as a first-time contributor? Thanks!

@rickhanlonii
Copy link
Member

@stnwk I do not - I would grep for setupFilesAfterEnv and track down how it uses moduleNameMapper, and then grep for globalSetup to see why it doesn't use it. Let me know if you have any questions 👍

@wanton7
Copy link

wanton7 commented 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);
};

@github-actions
Copy link

github-actions bot commented Feb 9, 2023

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 9, 2023
@github-actions
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 11, 2023
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants