Add Jest. (#250)

This commit is contained in:
Christoph Pojer
2016-08-02 08:22:33 +09:00
committed by Dan Abramov
parent c7c1f51d51
commit 0e5abada75
15 changed files with 149 additions and 9 deletions

View File

@@ -7,11 +7,12 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/
var createJestConfig = require('./utils/create-jest-config');
var fs = require('fs');
var path = require('path');
var prompt = require('./utils/prompt');
var rimrafSync = require('rimraf').sync;
var spawnSync = require('cross-spawn').sync;
var prompt = require('./utils/prompt');
prompt(
'Are you sure you want to eject? This action is permanent.',
@@ -37,6 +38,9 @@ prompt(
path.join('config', 'polyfills.js'),
path.join('config', 'webpack.config.dev.js'),
path.join('config', 'webpack.config.prod.js'),
path.join('config', 'jest', 'CSSStub.js'),
path.join('config', 'jest', 'FileStub.js'),
path.join('config', 'jest', 'transform.js'),
path.join('scripts', 'build.js'),
path.join('scripts', 'start.js'),
path.join('scripts', 'utils', 'chrome.applescript'),
@@ -59,6 +63,7 @@ prompt(
// Copy the files over
fs.mkdirSync(path.join(appPath, 'config'));
fs.mkdirSync(path.join(appPath, 'config', 'flow'));
fs.mkdirSync(path.join(appPath, 'config', 'jest'));
fs.mkdirSync(path.join(appPath, 'scripts'));
fs.mkdirSync(path.join(appPath, 'scripts', 'utils'));
@@ -96,6 +101,11 @@ prompt(
});
delete appPackage.scripts['eject'];
appPackage.scripts.test = 'jest';
appPackage.jest = createJestConfig(
filePath => path.join('<rootDir>', filePath)
);
// explicitly specify ESLint config path for editor plugins
appPackage.eslintConfig = {
extends: './config/eslint.js',

View File

@@ -19,13 +19,17 @@ module.exports = function(appPath, appName, verbose, originalDirectory) {
// Copy over some of the devDependencies
appPackage.dependencies = appPackage.dependencies || {};
appPackage.devDependencies = appPackage.devDependencies || {};
['react', 'react-dom'].forEach(function (key) {
appPackage.dependencies[key] = ownPackage.devDependencies[key];
});
['react-test-renderer'].forEach(function (key) {
appPackage.devDependencies[key] = ownPackage.devDependencies[key];
});
// Setup the script rules
appPackage.scripts = {};
['start', 'build', 'eject'].forEach(function(command) {
['start', 'build', 'eject', 'test'].forEach(function(command) {
appPackage.scripts[command] = 'react-scripts ' + command;
});

29
scripts/test.js Normal file
View File

@@ -0,0 +1,29 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
process.env.NODE_ENV = 'test';
const createJestConfig = require('./utils/create-jest-config');
const jest = require('jest');
const path = require('path');
const paths = require('../config/paths');
const argv = process.argv.slice(2);
const index = argv.indexOf('--debug-template');
if (index !== -1) {
argv.splice(index, 1);
}
argv.push('--config', JSON.stringify(createJestConfig(
relativePath => path.resolve(__dirname, '..', relativePath),
path.resolve(paths.appSrc, '..')
)));
jest.run(argv);

View File

@@ -0,0 +1,28 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
module.exports = (resolve, rootDir) => {
const config = {
automock: false,
moduleNameMapper: {
'^[./a-zA-Z0-9$_-]+\\.(jpg|png|gif|eot|svg|ttf|woff|woff2|mp4|webm)$': resolve('config/jest/FileStub.js'),
'^[./a-zA-Z0-9$_-]+\\.css$': resolve('config/jest/CSSStub.js')
},
persistModuleRegistryBetweenSpecs: true,
scriptPreprocessor: resolve('config/jest/transform.js'),
setupFiles: [
resolve('config/polyfills.js')
],
testEnvironment: 'node'
};
if (rootDir) {
config.rootDir = rootDir;
}
return config;
};