Skip to content

Commit 5456fff

Browse files
authored
Let Jest handle all file types (#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 cf64077 commit 5456fff

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

Diff for: packages/react-scripts/config/jest/FileStub.js renamed to packages/react-scripts/config/jest/cssTransform.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@
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+
// 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+
};

Diff for: packages/react-scripts/config/jest/CSSStub.js renamed to 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 = {};
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+
};

Diff for: 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')

Diff for: packages/react-scripts/utils/createJestConfig.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,30 @@ 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
collectCoverageFrom: ['src/**/*.{js,jsx}'],
22-
moduleNameMapper: {
23-
'^.+\\.(ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': resolve('config/jest/FileStub.js'),
24-
'^.+\\.css$': resolve('config/jest/CSSStub.js')
25-
},
2624
setupFiles: [resolve('config/polyfills.js')],
2725
setupTestFrameworkScriptFile: setupTestsFile,
28-
testPathIgnorePatterns: ['<rootDir>/(build|docs|node_modules)/'],
26+
testPathIgnorePatterns: [
27+
'<rootDir>[/\\\\](build|docs|node_modules)[/\\\\]'
28+
],
2929
testEnvironment: 'node',
3030
testURL: 'http://localhost',
31+
transform: {
32+
'^.+\\.(js|jsx)$': isEjecting ?
33+
'<rootDir>/node_modules/babel-jest'
34+
: resolve('config/jest/babelTransform.js'),
35+
'^.+\\.css$': resolve('config/jest/cssTransform.js'),
36+
'^(?!.*\\.(js|jsx|css|json)$)': resolve('config/jest/fileTransform.js'),
37+
},
38+
transformIgnorePatterns: [
39+
'[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'
40+
],
3141
};
3242
if (rootDir) {
3343
config.rootDir = rootDir;
3444
}
35-
if (!isEjecting) {
36-
// This is unnecessary after ejecting because Jest
37-
// will just use .babelrc in the project folder.
38-
config.transform = {
39-
'^.+\\.(js|jsx)$': resolve('config/jest/transform.js'),
40-
};
41-
}
4245
return config;
4346
};

0 commit comments

Comments
 (0)