UNPKG

3.64 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 (!process.env.CI && argv.indexOf('--watchAll') === -1) {
59 // https://github.com/facebook/create-react-app/issues/5210
60 const hasSourceControl = isInGitRepository() || isInMercurialRepository();
61 argv.push(hasSourceControl ? '--watch' : '--watchAll');
62}
63
64// @remove-on-eject-begin
65// This is not necessary after eject because we embed config into package.json.
66const createJestConfig = require('./utils/createJestConfig');
67const path = require('path');
68const paths = require('../config/paths');
69argv.push(
70 '--config',
71 JSON.stringify(
72 createJestConfig(
73 relativePath => path.resolve(__dirname, '..', relativePath),
74 path.resolve(paths.appSrc, '..'),
75 false
76 )
77 )
78);
79
80// This is a very dirty workaround for https://github.com/facebook/jest/issues/5913.
81// We're trying to resolve the environment ourselves because Jest does it incorrectly.
82// TODO: remove this as soon as it's fixed in Jest.
83const resolve = require('resolve');
84function resolveJestDefaultEnvironment(name) {
85 const jestDir = path.dirname(
86 resolve.sync('jest', {
87 basedir: __dirname,
88 })
89 );
90 const jestCLIDir = path.dirname(
91 resolve.sync('jest-cli', {
92 basedir: jestDir,
93 })
94 );
95 const jestConfigDir = path.dirname(
96 resolve.sync('jest-config', {
97 basedir: jestCLIDir,
98 })
99 );
100 return resolve.sync(name, {
101 basedir: jestConfigDir,
102 });
103}
104let cleanArgv = [];
105let env = 'jsdom';
106let next;
107do {
108 next = argv.shift();
109 if (next === '--env') {
110 env = argv.shift();
111 } else if (next.indexOf('--env=') === 0) {
112 env = next.substring('--env='.length);
113 } else {
114 cleanArgv.push(next);
115 }
116} while (argv.length > 0);
117argv = cleanArgv;
118let resolvedEnv;
119try {
120 resolvedEnv = resolveJestDefaultEnvironment(`jest-environment-${env}`);
121} catch (e) {
122 // ignore
123}
124if (!resolvedEnv) {
125 try {
126 resolvedEnv = resolveJestDefaultEnvironment(env);
127 } catch (e) {
128 // ignore
129 }
130}
131const testEnvironment = resolvedEnv || env;
132argv.push('--env', testEnvironment);
133// @remove-on-eject-end
134jest.run(argv);