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 should honor modulePaths #6048

Closed
mschipperheyn opened this issue Apr 22, 2018 · 4 comments
Closed

globalSetup should honor modulePaths #6048

mschipperheyn opened this issue Apr 22, 2018 · 4 comments

Comments

@mschipperheyn
Copy link

mschipperheyn commented Apr 22, 2018

Do you want to request a feature or report a bug?
bug
What is the current behavior?
When globalSetup processes a setup, it doesn't seem to process files imported as absolute paths, e.g.
import 'router/Helmet'

They are setup through resolve in webpack and as modulePaths in jest setup.

I'm using babel-register to allow for ìmport statements. I'm wondering if this could be creating the problem as the error states this:

Error: Cannot find module 'router/Helmet'
    at Function.Module._resolveFilename (module.js:538:15)
    at Function.Module._load (module.js:468:25)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/Shared/java/projects/react/projects/my-project/shared/App/index.js:5:1)
    at Module._compile (module.js:643:30)
    at loader (/Users/Shared/java/projects/react/projects/my-project/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/Shared/java/projects/react/projects/mobile-minds/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)

I have this at the top of my global setup file:

require('babel-register');

Before I tried to use global setup everything worked fine in principle. I understand from comments here: #5164 that transform is not applied to global setup, which I hope will change. Perhaps the same goes for modulePaths?

What is the expected behavior?
These imports should work.

Please provide your exact Jest configuration

"jest": {
		"globals": {
			"__DEV__": true
		},
		"moduleNameMapper": {
			"^.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
				"<rootDir>/internal/jest/assetMock.js",
			"^.+\\.(css|less|scss|sass|less)$": "<rootDir>/internal/jest/styleMock.js"
		},
		"modulePaths": ["<rootDir>", "<rootDir>/shared"],
		"collectCoverageFrom": ["s<rootDir>/shared/**/*.{js,jsx}"],
		"snapshotSerializers": ["<rootDir>/node_modules/enzyme-to-json/serializer"],
		"testPathIgnorePatterns": ["<rootDir>/(build|internal|node_modules|flow-typed|public)/"],
		"setupTestFrameworkScriptFile": "<rootDir>/internal/jest/setup",
		"globalSetup": "<rootDir>/server/test/setup",
		"globalTeardown": "<rootDir>/server/test/teardown",
		"transform": {
			"^.+\\.(js)$": "babel-jest"
		},
		"testEnvironment": "node",
		"verbose": true
	},

Run npx envinfo --preset jest in your project directory and 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: 5.6.0 - ~/.nvm/versions/node/v8.9.4/bin/npm
npmPackages:
jest: 22.4.3 => 22.4.3

@mschipperheyn
Copy link
Author

I now realize that global setup runs outside of jest, making it more like a kind of before jest setup. What I want is a global async beforeAll, afterAll inside jest

@mschipperheyn
Copy link
Author

It makes it hard to setup something global, like an express server. Using beforeAll and afterAll to start and stop the express server at test level runs into issues with jest running through a pool. So, you get listen port conflicts. Since global is sandboxed as well, can't use that either at that level.

@rickhanlonii
Copy link
Member

Hey @mschipperheyn thanks for using Jest! As I think you found, this is not a bug

For the follow-up questions please note this issue tracker is not a help forum. We recommend using our discord channel or StackOverflow for questions like this

@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 May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants