UNPKG

3.09 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}
31// @remove-on-eject-end
32
33const jest = require('jest');
34let argv = process.argv.slice(2);
35
36// Watch unless on CI, in coverage mode, or explicitly running all tests
37if (
38 !process.env.CI &&
39 argv.indexOf('--coverage') === -1 &&
40 argv.indexOf('--watchAll') === -1
41) {
42 argv.push('--watch');
43}
44
45// @remove-on-eject-begin
46// This is not necessary after eject because we embed config into package.json.
47const createJestConfig = require('./utils/createJestConfig');
48const path = require('path');
49const paths = require('../config/paths');
50argv.push(
51 '--config',
52 JSON.stringify(
53 createJestConfig(
54 relativePath => path.resolve(__dirname, '..', relativePath),
55 path.resolve(paths.appSrc, '..'),
56 false
57 )
58 )
59);
60
61// This is a very dirty workaround for https://github.com/facebook/jest/issues/5913.
62// We're trying to resolve the environment ourselves because Jest does it incorrectly.
63// TODO: remove this (and the `resolve` dependency) as soon as it's fixed in Jest.
64const resolve = require('resolve');
65function resolveJestDefaultEnvironment(name) {
66 const jestDir = path.dirname(
67 resolve.sync('jest', {
68 basedir: __dirname,
69 })
70 );
71 const jestCLIDir = path.dirname(
72 resolve.sync('jest-cli', {
73 basedir: jestDir,
74 })
75 );
76 const jestConfigDir = path.dirname(
77 resolve.sync('jest-config', {
78 basedir: jestCLIDir,
79 })
80 );
81 return resolve.sync(name, {
82 basedir: jestConfigDir,
83 });
84}
85let cleanArgv = [];
86let env = 'jsdom';
87let next;
88do {
89 next = argv.shift();
90 if (next === '--env') {
91 env = argv.shift();
92 } else if (next.indexOf('--env=') === 0) {
93 env = next.substring('--env='.length);
94 } else {
95 cleanArgv.push(next);
96 }
97} while (argv.length > 0);
98argv = cleanArgv;
99let resolvedEnv;
100try {
101 resolvedEnv = resolveJestDefaultEnvironment(`jest-environment-${env}`);
102} catch (e) {
103 // ignore
104}
105if (!resolvedEnv) {
106 try {
107 resolvedEnv = resolveJestDefaultEnvironment(env);
108 } catch (e) {
109 // ignore
110 }
111}
112const testEnvironment = resolvedEnv || env;
113argv.push('--env', testEnvironment);
114// @remove-on-eject-end
115jest.run(argv);