test.js 3.02 KB
Newer Older
1
// @remove-on-eject-begin
Christoph Pojer's avatar
Christoph Pojer committed
2
3
4
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
Sophie Alpert's avatar
Sophie Alpert committed
5
6
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
Christoph Pojer's avatar
Christoph Pojer committed
7
 */
8
// @remove-on-eject-end
9
10
'use strict';

11
12
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'test';
Christoph Pojer's avatar
Christoph Pojer committed
13
process.env.NODE_ENV = 'test';
14
process.env.PUBLIC_URL = '';
Christoph Pojer's avatar
Christoph Pojer committed
15

16
17
18
19
20
21
22
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', err => {
  throw err;
});

23
24
// Ensure environment variables are read.
require('../config/env');
25
26
27
28
29
30
31
// @remove-on-eject-begin
// Do the preflight check (only happens before eject).
const verifyPackageTree = require('./utils/verifyPackageTree');
if (process.env.SKIP_PREFLIGHT_CHECK !== 'true') {
  verifyPackageTree();
}
// @remove-on-eject-end
32

Christoph Pojer's avatar
Christoph Pojer committed
33
const jest = require('jest');
34
let argv = process.argv.slice(2);
Christoph Pojer's avatar
Christoph Pojer committed
35

36
37
38
39
40
41
// Watch unless on CI, in coverage mode, or explicitly running all tests
if (
  !process.env.CI &&
  argv.indexOf('--coverage') === -1 &&
  argv.indexOf('--watchAll') === -1
) {
42
43
44
  argv.push('--watch');
}

45
46
// @remove-on-eject-begin
// This is not necessary after eject because we embed config into package.json.
Daniel Grant's avatar
Daniel Grant committed
47
const createJestConfig = require('./utils/createJestConfig');
Dan Abramov's avatar
Dan Abramov committed
48
49
const path = require('path');
const paths = require('../config/paths');
50
51
52
53
54
55
argv.push(
  '--config',
  JSON.stringify(
    createJestConfig(
      relativePath => path.resolve(__dirname, '..', relativePath),
      path.resolve(paths.appSrc, '..'),
56
      paths.srcPaths
57
58
59
    )
  )
);
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

// This is a very dirty workaround for https://github.com/facebook/jest/issues/5913.
// We're trying to resolve the environment ourselves because Jest does it incorrectly.
// TODO: remove this (and the `resolve` dependency) as soon as it's fixed in Jest.
const resolve = require('resolve');
function resolveJestDefaultEnvironment(name) {
  const jestDir = path.dirname(
    resolve.sync('jest', {
      basedir: __dirname,
    })
  );
  const jestCLIDir = path.dirname(
    resolve.sync('jest-cli', {
      basedir: jestDir,
    })
  );
  const jestConfigDir = path.dirname(
    resolve.sync('jest-config', {
      basedir: jestCLIDir,
    })
  );
  return resolve.sync(name, {
    basedir: jestConfigDir,
  });
}
let cleanArgv = [];
let env = 'node';
let next;
do {
  next = argv.shift();
  if (next === '--env') {
    env = argv.shift();
  } else if (next.indexOf('--env=') === 0) {
    env = next.substring('--env='.length);
  } else {
    cleanArgv.push(next);
  }
} while (argv.length > 0);
argv = cleanArgv;
let resolvedEnv;
try {
  resolvedEnv = resolveJestDefaultEnvironment(`jest-environment-${env}`);
} catch (e) {
  // ignore
}
if (!resolvedEnv) {
  try {
    resolvedEnv = resolveJestDefaultEnvironment(env);
  } catch (e) {
    // ignore
  }
}
const testEnvironment = resolvedEnv || env;
argv.push('--env', testEnvironment);
114
// @remove-on-eject-end
Christoph Pojer's avatar
Christoph Pojer committed
115
jest.run(argv);