| 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.
|
|---|
| 12 | process.env.BABEL_ENV = 'test';
|
|---|
| 13 | process.env.NODE_ENV = 'test';
|
|---|
| 14 | process.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.
|
|---|
| 19 | process.on('unhandledRejection', err => {
|
|---|
| 20 | throw err;
|
|---|
| 21 | });
|
|---|
| 22 |
|
|---|
| 23 | // Ensure environment variables are read.
|
|---|
| 24 | require('../config/env');
|
|---|
| 25 |
|
|---|
| 26 | const jest = require('jest');
|
|---|
| 27 | const execSync = require('child_process').execSync;
|
|---|
| 28 | let argv = process.argv.slice(2);
|
|---|
| 29 |
|
|---|
| 30 | function isInGitRepository() {
|
|---|
| 31 | try {
|
|---|
| 32 | execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
|
|---|
| 33 | return true;
|
|---|
| 34 | } catch (e) {
|
|---|
| 35 | return false;
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | function isInMercurialRepository() {
|
|---|
| 40 | try {
|
|---|
| 41 | execSync('hg --cwd . root', { stdio: 'ignore' });
|
|---|
| 42 | return true;
|
|---|
| 43 | } catch (e) {
|
|---|
| 44 | return false;
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | // Watch unless on CI or explicitly running all tests
|
|---|
| 49 | if (
|
|---|
| 50 | !process.env.CI &&
|
|---|
| 51 | argv.indexOf('--watchAll') === -1 &&
|
|---|
| 52 | argv.indexOf('--watchAll=false') === -1
|
|---|
| 53 | ) {
|
|---|
| 54 | // https://github.com/facebook/create-react-app/issues/5210
|
|---|
| 55 | const hasSourceControl = isInGitRepository() || isInMercurialRepository();
|
|---|
| 56 | argv.push(hasSourceControl ? '--watch' : '--watchAll');
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | // @remove-on-eject-begin
|
|---|
| 60 | // This is not necessary after eject because we embed config into package.json.
|
|---|
| 61 | const createJestConfig = require('./utils/createJestConfig');
|
|---|
| 62 | const path = require('path');
|
|---|
| 63 | const paths = require('../config/paths');
|
|---|
| 64 | argv.push(
|
|---|
| 65 | '--config',
|
|---|
| 66 | JSON.stringify(
|
|---|
| 67 | createJestConfig(
|
|---|
| 68 | relativePath => path.resolve(__dirname, '..', relativePath),
|
|---|
| 69 | path.resolve(paths.appSrc, '..'),
|
|---|
| 70 | false
|
|---|
| 71 | )
|
|---|
| 72 | )
|
|---|
| 73 | );
|
|---|
| 74 |
|
|---|
| 75 | // This is a very dirty workaround for https://github.com/facebook/jest/issues/5913.
|
|---|
| 76 | // We're trying to resolve the environment ourselves because Jest does it incorrectly.
|
|---|
| 77 | // TODO: remove this as soon as it's fixed in Jest.
|
|---|
| 78 | const resolve = require('resolve');
|
|---|
| 79 | function resolveJestDefaultEnvironment(name) {
|
|---|
| 80 | const jestDir = path.dirname(
|
|---|
| 81 | resolve.sync('jest', {
|
|---|
| 82 | basedir: __dirname,
|
|---|
| 83 | })
|
|---|
| 84 | );
|
|---|
| 85 | const jestCLIDir = path.dirname(
|
|---|
| 86 | resolve.sync('jest-cli', {
|
|---|
| 87 | basedir: jestDir,
|
|---|
| 88 | })
|
|---|
| 89 | );
|
|---|
| 90 | const jestConfigDir = path.dirname(
|
|---|
| 91 | resolve.sync('jest-config', {
|
|---|
| 92 | basedir: jestCLIDir,
|
|---|
| 93 | })
|
|---|
| 94 | );
|
|---|
| 95 | return resolve.sync(name, {
|
|---|
| 96 | basedir: jestConfigDir,
|
|---|
| 97 | });
|
|---|
| 98 | }
|
|---|
| 99 | let cleanArgv = [];
|
|---|
| 100 | let env = 'jsdom';
|
|---|
| 101 | let next;
|
|---|
| 102 | do {
|
|---|
| 103 | next = argv.shift();
|
|---|
| 104 | if (next === '--env') {
|
|---|
| 105 | env = argv.shift();
|
|---|
| 106 | } else if (next.indexOf('--env=') === 0) {
|
|---|
| 107 | env = next.substring('--env='.length);
|
|---|
| 108 | } else {
|
|---|
| 109 | cleanArgv.push(next);
|
|---|
| 110 | }
|
|---|
| 111 | } while (argv.length > 0);
|
|---|
| 112 | argv = cleanArgv;
|
|---|
| 113 | let resolvedEnv;
|
|---|
| 114 | try {
|
|---|
| 115 | resolvedEnv = resolveJestDefaultEnvironment(`jest-environment-${env}`);
|
|---|
| 116 | } catch (e) {
|
|---|
| 117 | // ignore
|
|---|
| 118 | }
|
|---|
| 119 | if (!resolvedEnv) {
|
|---|
| 120 | try {
|
|---|
| 121 | resolvedEnv = resolveJestDefaultEnvironment(env);
|
|---|
| 122 | } catch (e) {
|
|---|
| 123 | // ignore
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 | const testEnvironment = resolvedEnv || env;
|
|---|
| 127 | argv.push('--env', testEnvironment);
|
|---|
| 128 | // @remove-on-eject-end
|
|---|
| 129 | jest.run(argv);
|
|---|