Skip to content

Commit 18427fd

Browse files
committed
Let Jest handle all file types
1 parent d68a989 commit 18427fd

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

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

Lines changed: 12 additions & 3 deletions
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+
};

packages/react-scripts/config/jest/CSSStub.js renamed to packages/react-scripts/config/jest/fileTransform.js

Lines changed: 10 additions & 3 deletions
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+
};

packages/react-scripts/scripts/eject.js

Lines changed: 2 additions & 2 deletions
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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,28 @@ module.exports = (resolve, rootDir, isEjecting) => {
1919

2020
const config = {
2121
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-
},
2622
setupFiles: [resolve('config/polyfills.js')],
2723
setupTestFrameworkScriptFile: setupTestsFile,
2824
testPathIgnorePatterns: ['<rootDir>/(build|docs|node_modules)/'],
2925
testEnvironment: 'node',
3026
testURL: 'http://localhost',
27+
transform: {
28+
'^.+\\.css$': resolve('config/jest/cssTransform.js'),
29+
'^.+\\.(?!(js|jsx|css|json)$)[^\\.]+$': resolve('config/jest/fileTransform.js'),
30+
},
31+
transformIgnorePatterns: [
32+
'/node_modules/.+\.(js|jsx|json)$'
33+
],
3134
};
3235
if (rootDir) {
3336
config.rootDir = rootDir;
3437
}
3538
if (!isEjecting) {
3639
// This is unnecessary after ejecting because Jest
3740
// will just use .babelrc in the project folder.
38-
config.transform = {
39-
'^.+\\.(js|jsx)$': resolve('config/jest/transform.js'),
40-
};
41+
Object.assign(config.transform, {
42+
'^.+\\.(js|jsx)$': resolve('config/jest/babelTransform.js'),
43+
});
4144
}
4245
return config;
4346
};

0 commit comments

Comments
 (0)