UNPKG

3.69 kBJavaScriptView Raw
1// @remove-on-eject-begin
2/**
3 * Copyright (c) 2015-present, Facebook, Inc.
4 *
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8// @remove-on-eject-end
9'use strict';
10
11// Do this as the first thing so that any code reading it knows the right env.
12process.env.BABEL_ENV = 'test';
13process.env.NODE_ENV = 'test';
14process.env.PUBLIC_URL = '';
15
16// Makes the script crash on unhandled rejections instead of silently
17// ignoring them. In the future, promise rejections that are not handled will
18// terminate the Node.js process with a non-zero exit code.
19process.on('unhandledRejection', err => {
20 throw err;
21});
22
23// Ensure environment variables are read.
24require('../config/env');
25// @remove-on-eject-begin
26// Do the preflight check (only happens before eject).
27const verifyPackageTree = require('./utils/verifyPackageTree');
28if (process.env.SKIP_PREFLIGHT_CHECK !== 'true') {
29 verifyPackageTree();
30}
31const verifyTypeScriptSetup = require('./utils/verifyTypeScriptSetup');
32verifyTypeScriptSetup();
33// @remove-on-eject-end
34
35const jest = require('jest');
36const execSync = require('child_process').execSync;
37let argv = process.argv.slice(2);
38
39function isInGitRepository() {
40 try {
41 execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
42 return true;
43 } catch (e) {
44 return false;
45 }
46}
47
48function isInMercurialRepository() {
49 try {
50 execSync('hg --cwd . root', { stdio: 'ignore' });
51 return true;
52 } catch (e) {
53 return false;
54 }
55}
56
57// Watch unless on CI or explicitly running all tests
58if (
59 !process.env.CI &&
60 argv.indexOf('--watchAll') === -1 &&
61 argv.indexOf('--watchAll=false') === -1
62) {
63 // https://github.com/facebook/create-react-app/issues/5210
64 const hasSourceControl = isInGitRepository() || isInMercurialRepository();
65 argv.push(hasSourceControl ? '--watch' : '--watchAll');
66}
67
68// @remove-on-eject-begin
69// This is not necessary after eject because we embed config into package.json.
70const createJestConfig = require('./utils/createJestConfig');
71const path = require('path');
72const paths = require('../config/paths');
73argv.push(
74 '--config',
75 JSON.stringify(
76 createJestConfig(
77 relativePath => path.resolve(__dirname, '..', relativePath),
78 path.resolve(paths.appSrc, '..'),
79 false
80 )
81 )
82);
83
84// This is a very dirty workaround for https://github.com/facebook/jest/issues/5913.
85// We're trying to resolve the environment ourselves because Jest does it incorrectly.
86// TODO: remove this as soon as it's fixed in Jest.
87const resolve = require('resolve');
88function resolveJestDefaultEnvironment(name) {
89 const jestDir = path.dirname(
90 resolve.sync('jest', {
91 basedir: __dirname,
92 })
93 );
94 const jestCLIDir = path.dirname(
95 resolve.sync('jest-cli', {
96 basedir: jestDir,
97 })
98 );
99 const jestConfigDir = path.dirname(
100 resolve.sync('jest-config', {
101 basedir: jestCLIDir,
102 })
103 );
104 return resolve.sync(name, {
105 basedir: jestConfigDir,
106 });
107}
108let cleanArgv = [];
109let env = 'jsdom';
110let next;
111do {
112 next = argv.shift();
113 if (next === '--env') {
114 env = argv.shift();
115 } else if (next.indexOf('--env=') === 0) {
116 env = next.substring('--env='.length);
117 } else {
118 cleanArgv.push(next);
119 }
120} while (argv.length > 0);
121argv = cleanArgv;
122let resolvedEnv;
123try {
124 resolvedEnv = resolveJestDefaultEnvironment(`jest-environment-${env}`);
125} catch (e) {
126 // ignore
127}
128if (!resolvedEnv) {
129 try {
130 resolvedEnv = resolveJestDefaultEnvironment(env);
131 } catch (e) {
132 // ignore
133 }
134}
135const testEnvironment = resolvedEnv || env;
136argv.push('--env', testEnvironment);
137// @remove-on-eject-end
138jest.run(argv);