mirror of
https://github.com/zhigang1992/create-react-app.git
synced 2026-01-12 22:46:30 +08:00
Work around Jest environment resolving bug (#4247)
This commit is contained in:
@@ -56,6 +56,7 @@
|
||||
"promise": "8.0.1",
|
||||
"raf": "3.4.0",
|
||||
"react-dev-utils": "^5.0.0",
|
||||
"resolve": "1.6.0",
|
||||
"style-loader": "0.19.1",
|
||||
"svgr": "1.8.1",
|
||||
"sw-precache-webpack-plugin": "0.11.4",
|
||||
|
||||
56
packages/react-scripts/scripts/test.js
vendored
56
packages/react-scripts/scripts/test.js
vendored
@@ -31,7 +31,7 @@ if (process.env.SKIP_PREFLIGHT_CHECK !== 'true') {
|
||||
// @remove-on-eject-end
|
||||
|
||||
const jest = require('jest');
|
||||
const argv = process.argv.slice(2);
|
||||
let argv = process.argv.slice(2);
|
||||
|
||||
// Watch unless on CI, in coverage mode, or explicitly running all tests
|
||||
if (
|
||||
@@ -57,5 +57,59 @@ argv.push(
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// This is a very dirty workaround for https://github.com/facebook/jest/issues/5913.
|
||||
// We're trying to resolve the environment ourselves because Jest does it incorrectly.
|
||||
// TODO: remove this (and the `resolve` dependency) as soon as it's fixed in Jest.
|
||||
const resolve = require('resolve');
|
||||
function resolveJestDefaultEnvironment(name) {
|
||||
const jestDir = path.dirname(
|
||||
resolve.sync('jest', {
|
||||
basedir: __dirname,
|
||||
})
|
||||
);
|
||||
const jestCLIDir = path.dirname(
|
||||
resolve.sync('jest-cli', {
|
||||
basedir: jestDir,
|
||||
})
|
||||
);
|
||||
const jestConfigDir = path.dirname(
|
||||
resolve.sync('jest-config', {
|
||||
basedir: jestCLIDir,
|
||||
})
|
||||
);
|
||||
return resolve.sync(name, {
|
||||
basedir: jestConfigDir,
|
||||
});
|
||||
}
|
||||
let cleanArgv = [];
|
||||
let env = 'node';
|
||||
let next;
|
||||
do {
|
||||
next = argv.shift();
|
||||
if (next === '--env') {
|
||||
env = argv.shift();
|
||||
} else if (next.indexOf('--env=') === 0) {
|
||||
env = next.substring('--env='.length);
|
||||
} else {
|
||||
cleanArgv.push(next);
|
||||
}
|
||||
} while (argv.length > 0);
|
||||
argv = cleanArgv;
|
||||
let resolvedEnv;
|
||||
try {
|
||||
resolvedEnv = resolveJestDefaultEnvironment(`jest-environment-${env}`);
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
if (!resolvedEnv) {
|
||||
try {
|
||||
resolvedEnv = resolveJestDefaultEnvironment(env);
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
const testEnvironment = resolvedEnv || env;
|
||||
argv.push('--env', testEnvironment);
|
||||
// @remove-on-eject-end
|
||||
jest.run(argv);
|
||||
|
||||
Reference in New Issue
Block a user