Skip to content

Commit a9e5f66

Browse files
gaearonrandycoulman
authored andcommitted
Let Jest handle all file types (facebook#1197)
* Let Jest handle all file types * Update regexes * Fix exclusion regex to also exclude files without extension * Be over-cautious with Windows paths because I'm not sure how Jest handles them * There is no automatic babel-jest discovery now that we use transsform
1 parent 9591168 commit a9e5f66

File tree

6 files changed

+49
-27
lines changed

6 files changed

+49
-27
lines changed

packages/react-scripts/config/jest/CSSStub.js

-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
// @remove-on-eject-begin
2-
/**
3-
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
4-
*
5-
* This source code is licensed under the BSD-style license found in the
6-
* LICENSE file in the root directory of this source tree. An additional grant
7-
* of patent rights can be found in the PATENTS file in the same directory.
8-
*
9-
* @flow
10-
*/
11-
// @remove-on-eject-end
12-
131
/*
142
* ZEAL: Stub style imports with identity proxy to allow testing of
153
* dynamic styles etc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @remove-on-eject-begin
2+
/**
3+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
// @remove-on-eject-end
10+
11+
// This is a custom Jest transformer turning style imports into empty objects.
12+
// http://facebook.github.io/jest/docs/tutorial-webpack.html
13+
14+
module.exports = {
15+
process() {
16+
return 'module.exports = {};';
17+
},
18+
getCacheKey(fileData, filename) {
19+
// The output is always the same.
20+
return 'cssTransform';
21+
},
22+
};

packages/react-scripts/config/jest/FileStub.js packages/react-scripts/config/jest/fileTransform.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55
* This source code is licensed under the BSD-style license found in the
66
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
8-
*
9-
* @flow
108
*/
119
// @remove-on-eject-end
1210

13-
module.exports = "test-file-stub";
11+
const path = require('path');
12+
13+
// This is a custom Jest transformer turning file imports into filenames.
14+
// http://facebook.github.io/jest/docs/tutorial-webpack.html
15+
16+
module.exports = {
17+
process(src, filename) {
18+
return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
19+
},
20+
};

packages/react-scripts/scripts/eject.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ prompt(
5656
path.join('config', 'polyfills.js'),
5757
path.join('config', 'webpack.config.dev.js'),
5858
path.join('config', 'webpack.config.prod.js'),
59-
path.join('config', 'jest', 'CSSStub.js'),
60-
path.join('config', 'jest', 'FileStub.js'),
59+
path.join('config', 'jest', 'cssTransform.js'),
60+
path.join('config', 'jest', 'fileTransform.js'),
6161
path.join('scripts', 'build.js'),
6262
path.join('scripts', 'start.js'),
6363
path.join('scripts', 'test.js')

packages/react-scripts/utils/createJestConfig.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,36 @@ module.exports = (resolve, rootDir, isEjecting) => {
1717
// an absolute filename into configuration after ejecting.
1818
const setupTestsFile = pathExists.sync(paths.testsSetup) ? '<rootDir>/src/setupTests.js' : undefined;
1919

20+
// TODO: I don't know if it's safe or not to just use / as path separator
21+
// in Jest configs. We need help from somebody with Windows to determine this.
2022
const config = {
2123
// ZEAL: Coverage report to inlcude all client src code
2224
collectCoverageFrom: ['client/**/*.js', '!client/**/*-spec.js'],
2325
// ZEAL: Configure resolving imports from client root
2426
moduleDirectories: [paths.appSrc, paths.appNodeModules, paths.ownNodeModules],
25-
moduleFileExtensions: ['jsx', 'js', 'json'],
2627
moduleNameMapper: {
27-
'^.+\\.(ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': resolve('config/jest/FileStub.js'),
2828
'^.+\\.(css|scss)$': resolve('config/jest/CSSStub.js')
2929
},
3030
setupFiles: [resolve('config/polyfills.js')],
3131
setupTestFrameworkScriptFile: setupTestsFile,
32-
testPathIgnorePatterns: ['<rootDir>/(build|docs|node_modules)/'],
32+
testPathIgnorePatterns: [
33+
'<rootDir>[/\\\\](build|docs|node_modules)[/\\\\]'
34+
],
3335
testEnvironment: 'node',
3436
testURL: 'http://localhost',
37+
transform: {
38+
'^.+\\.(js|jsx)$': isEjecting ?
39+
'<rootDir>/node_modules/babel-jest'
40+
: resolve('config/jest/babelTransform.js'),
41+
// '^.+\\.css$': resolve('config/jest/cssTransform.js'),
42+
'^(?!.*\\.(js|jsx|css|json)$)': resolve('config/jest/fileTransform.js'),
43+
},
44+
transformIgnorePatterns: [
45+
'[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'
46+
],
3547
};
3648
if (rootDir) {
3749
config.rootDir = rootDir;
3850
}
39-
if (!isEjecting) {
40-
// This is unnecessary after ejecting because Jest
41-
// will just use .babelrc in the project folder.
42-
config.transform = {
43-
'^.+\\.(js|jsx)$': resolve('config/jest/transform.js'),
44-
};
45-
}
4651
return config;
4752
};

0 commit comments

Comments
 (0)