Skip to content

Commit 97ee064

Browse files
committed
fix: ensure jest can run with React 16 and CSS Modules support (#2)
- extend polyfil to handle rAR, which is needed by React 16 - added s[ac]ss extension to moduleNameMapper in jest config to proxy import of css modules to {}
1 parent 34c4881 commit 97ee064

File tree

3 files changed

+63
-66
lines changed

3 files changed

+63
-66
lines changed

packages/react-scripts/config/polyfills.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88
// @remove-on-eject-end
9-
'use strict';
9+
"use strict";
1010

11-
if (typeof Promise === 'undefined') {
11+
if (typeof Promise === "undefined") {
1212
// Rejection tracking prevents a common issue where React gets into an
1313
// inconsistent state due to an error, but it gets swallowed by a Promise,
1414
// and the user has no idea what causes React's erratic future behavior.
15-
require('promise/lib/rejection-tracking').enable();
16-
window.Promise = require('promise/lib/es6-extensions.js');
15+
require("promise/lib/rejection-tracking").enable();
16+
window.Promise = require("promise/lib/es6-extensions.js");
1717
}
1818

1919
// fetch() polyfill for making API calls.
20-
require('whatwg-fetch');
20+
require("whatwg-fetch");
2121

2222
// Object.assign() is commonly used with React.
2323
// It will use the native implementation if it's present and isn't buggy.
24-
Object.assign = require('object-assign');
24+
Object.assign = require("object-assign");
2525

2626
// In tests, polyfill requestAnimationFrame since jsdom doesn't provide it yet.
2727
// We don't polyfill it in the browser--this is user's responsibility.
28-
if (process.env.NODE_ENV === 'test') {
29-
require('raf').polyfill(global);
28+
if (process.env.NODE_ENV === "test") {
29+
require("raf").polyfill(global);
3030
}

packages/react-scripts/scripts/utils/createJestConfig.js

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,63 @@
55
* This source code is licensed under the MIT license found in the
66
* LICENSE file in the root directory of this source tree.
77
*/
8-
'use strict';
8+
"use strict";
99

10-
const fs = require('fs');
11-
const chalk = require('chalk');
12-
const paths = require('../../config/paths');
10+
const fs = require("fs");
11+
const chalk = require("chalk");
12+
const paths = require("../../config/paths");
1313

1414
module.exports = (resolve, rootDir, isEjecting) => {
1515
// Use this instead of `paths.testsSetup` to avoid putting
1616
// an absolute filename into configuration after ejecting.
1717
const setupTestsFile = fs.existsSync(paths.testsSetup)
18-
? '<rootDir>/src/setupTests.js'
18+
? "<rootDir>/src/setupTests.js"
1919
: undefined;
2020

2121
// TODO: I don't know if it's safe or not to just use / as path separator
2222
// in Jest configs. We need help from somebody with Windows to determine this.
2323
const config = {
24-
collectCoverageFrom: ['src/**/*.{js,jsx,mjs}'],
25-
setupFiles: [resolve('config/polyfills.js')],
24+
collectCoverageFrom: ["src/**/*.{js,jsx,mjs}"],
25+
setupFiles: [resolve("config/polyfills.js")],
2626
setupTestFrameworkScriptFile: setupTestsFile,
2727
testMatch: [
28-
'<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}',
29-
'<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}',
28+
"<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}",
29+
"<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"
3030
],
31-
testEnvironment: 'node',
32-
testURL: 'http://localhost',
31+
testEnvironment: "node",
32+
testURL: "http://localhost",
3333
transform: {
34-
'^.+\\.(js|jsx|mjs)$': isEjecting
35-
? '<rootDir>/node_modules/babel-jest'
36-
: resolve('config/jest/babelTransform.js'),
37-
'^.+\\.css$': resolve('config/jest/cssTransform.js'),
38-
'^(?!.*\\.(js|jsx|mjs|css|json)$)': resolve(
39-
'config/jest/fileTransform.js'
40-
),
34+
"^.+\\.(js|jsx|mjs)$": isEjecting
35+
? "<rootDir>/node_modules/babel-jest"
36+
: resolve("config/jest/babelTransform.js"),
37+
"^.+\\.css$": resolve("config/jest/cssTransform.js"),
38+
"^(?!.*\\.(js|jsx|mjs|css|json)$)": resolve(
39+
"config/jest/fileTransform.js"
40+
)
4141
},
42-
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$'],
42+
transformIgnorePatterns: ["[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"],
4343
moduleNameMapper: {
44-
'^react-native$': 'react-native-web',
44+
"^react-native$": "react-native-web"
4545
},
4646
moduleFileExtensions: [
47-
'web.js',
48-
'mjs',
49-
'js',
50-
'json',
51-
'web.jsx',
52-
'jsx',
53-
'node',
54-
],
47+
"web.js",
48+
"mjs",
49+
"js",
50+
"json",
51+
"web.jsx",
52+
"jsx",
53+
"node"
54+
]
5555
};
5656
if (rootDir) {
5757
config.rootDir = rootDir;
5858
}
5959
const overrides = Object.assign({}, require(paths.appPackageJson).jest);
6060
const supportedKeys = [
61-
'collectCoverageFrom',
62-
'coverageReporters',
63-
'coverageThreshold',
64-
'snapshotSerializers',
61+
"collectCoverageFrom",
62+
"coverageReporters",
63+
"coverageThreshold",
64+
"snapshotSerializers"
6565
];
6666
if (overrides) {
6767
supportedKeys.forEach(key => {
@@ -74,21 +74,21 @@ module.exports = (resolve, rootDir, isEjecting) => {
7474
if (unsupportedKeys.length) {
7575
console.error(
7676
chalk.red(
77-
'Out of the box, Create React App only supports overriding ' +
78-
'these Jest options:\n\n' +
79-
supportedKeys.map(key => chalk.bold(' \u2022 ' + key)).join('\n') +
80-
'.\n\n' +
81-
'These options in your package.json Jest configuration ' +
82-
'are not currently supported by Create React App:\n\n' +
77+
"Out of the box, Create React App only supports overriding " +
78+
"these Jest options:\n\n" +
79+
supportedKeys.map(key => chalk.bold(" \u2022 " + key)).join("\n") +
80+
".\n\n" +
81+
"These options in your package.json Jest configuration " +
82+
"are not currently supported by Create React App:\n\n" +
8383
unsupportedKeys
84-
.map(key => chalk.bold(' \u2022 ' + key))
85-
.join('\n') +
86-
'\n\nIf you wish to override other Jest options, you need to ' +
87-
'eject from the default setup. You can do so by running ' +
88-
chalk.bold('npm run eject') +
89-
' but remember that this is a one-way operation. ' +
90-
'You may also file an issue with Create React App to discuss ' +
91-
'supporting more options out of the box.\n'
84+
.map(key => chalk.bold(" \u2022 " + key))
85+
.join("\n") +
86+
"\n\nIf you wish to override other Jest options, you need to " +
87+
"eject from the default setup. You can do so by running " +
88+
chalk.bold("npm run eject") +
89+
" but remember that this is a one-way operation. " +
90+
"You may also file an issue with Create React App to discuss " +
91+
"supporting more options out of the box.\n"
9292
)
9393
);
9494
process.exit(1);

packages/react-scripts/yarn.lock

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4464,11 +4464,7 @@ mime-types@^2.1.12, mime-types@~2.1.15, mime-types@~2.1.16, mime-types@~2.1.17,
44644464
dependencies:
44654465
mime-db "~1.30.0"
44664466

4467-
4468-
version "1.3.6"
4469-
resolved "https://registry.yarnpkg.com/mime/-/mime-1.3.6.tgz#591d84d3653a6b0b4a3b9df8de5aa8108e72e5e0"
4470-
4471-
[email protected], mime@^1.3.4:
4467+
[email protected], mime@^1.3.4, mime@^1.4.1:
44724468
version "1.4.1"
44734469
resolved "https://registry.yarnpkg.com/mime/-/mime-1.4.1.tgz#121f9ebc49e3766f311a76e1fa1c8003c4b03aa6"
44744470

@@ -6401,9 +6397,9 @@ strip-json-comments@~2.0.1:
64016397
version "2.0.1"
64026398
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
64036399

6404-
style-loader@0.18.2:
6405-
version "0.18.2"
6406-
resolved "https://registry.yarnpkg.com/style-loader/-/style-loader-0.18.2.tgz#cc31459afbcd6d80b7220ee54b291a9fd66ff5eb"
6400+
style-loader@0.19.0:
6401+
version "0.19.0"
6402+
resolved "https://registry.yarnpkg.com/style-loader/-/style-loader-0.19.0.tgz#7258e788f0fee6a42d710eaf7d6c2412a4c50759"
64076403
dependencies:
64086404
loader-utils "^1.0.2"
64096405
schema-utils "^0.3.0"
@@ -6708,12 +6704,13 @@ urix@^0.1.0, urix@~0.1.0:
67086704
version "0.1.0"
67096705
resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72"
67106706

6711-
url-loader@0.5.9:
6712-
version "0.5.9"
6713-
resolved "https://registry.yarnpkg.com/url-loader/-/url-loader-0.5.9.tgz#cc8fea82c7b906e7777019250869e569e995c295"
6707+
url-loader@0.6.2:
6708+
version "0.6.2"
6709+
resolved "https://registry.yarnpkg.com/url-loader/-/url-loader-0.6.2.tgz#a007a7109620e9d988d14bce677a1decb9a993f7"
67146710
dependencies:
67156711
loader-utils "^1.0.2"
6716-
mime "1.3.x"
6712+
mime "^1.4.1"
6713+
schema-utils "^0.3.0"
67176714

67186715
url-parse-lax@^1.0.0:
67196716
version "1.0.0"

0 commit comments

Comments
 (0)