1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 |
|
12 | process.env.BABEL_ENV = 'test';
|
13 | process.env.NODE_ENV = 'test';
|
14 | process.env.PUBLIC_URL = '';
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | process.on('unhandledRejection', err => {
|
20 | throw err;
|
21 | });
|
22 |
|
23 |
|
24 | require('../config/env');
|
25 |
|
26 |
|
27 | const verifyPackageTree = require('./utils/verifyPackageTree');
|
28 | if (process.env.SKIP_PREFLIGHT_CHECK !== 'true') {
|
29 | verifyPackageTree();
|
30 | }
|
31 | const verifyTypeScriptSetup = require('./utils/verifyTypeScriptSetup');
|
32 | verifyTypeScriptSetup();
|
33 |
|
34 |
|
35 | const jest = require('jest');
|
36 | const execSync = require('child_process').execSync;
|
37 | let argv = process.argv.slice(2);
|
38 |
|
39 | function 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 |
|
48 | function isInMercurialRepository() {
|
49 | try {
|
50 | execSync('hg --cwd . root', { stdio: 'ignore' });
|
51 | return true;
|
52 | } catch (e) {
|
53 | return false;
|
54 | }
|
55 | }
|
56 |
|
57 |
|
58 | if (
|
59 | !process.env.CI &&
|
60 | argv.indexOf('--coverage') === -1 &&
|
61 | argv.indexOf('--watchAll') === -1
|
62 | ) {
|
63 |
|
64 | const hasSourceControl = isInGitRepository() || isInMercurialRepository();
|
65 | argv.push(hasSourceControl ? '--watch' : '--watchAll');
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 | const createJestConfig = require('./utils/createJestConfig');
|
71 | const path = require('path');
|
72 | const paths = require('../config/paths');
|
73 | argv.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 |
|
85 |
|
86 |
|
87 | const resolve = require('resolve');
|
88 | function 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 | }
|
108 | let cleanArgv = [];
|
109 | let env = 'jsdom';
|
110 | let next;
|
111 | do {
|
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);
|
121 | argv = cleanArgv;
|
122 | let resolvedEnv;
|
123 | try {
|
124 | resolvedEnv = resolveJestDefaultEnvironment(`jest-environment-${env}`);
|
125 | } catch (e) {
|
126 |
|
127 | }
|
128 | if (!resolvedEnv) {
|
129 | try {
|
130 | resolvedEnv = resolveJestDefaultEnvironment(env);
|
131 | } catch (e) {
|
132 |
|
133 | }
|
134 | }
|
135 | const testEnvironment = resolvedEnv || env;
|
136 | argv.push('--env', testEnvironment);
|
137 |
|
138 | jest.run(argv);
|