Skip to content

Commit 8ffcf7d

Browse files
authored
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 1ce210e commit 8ffcf7d

File tree

3 files changed

+57
-55
lines changed

3 files changed

+57
-55
lines changed

packages/react-scripts/config/polyfills.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@
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");
25+
26+
// To support React 16, we need to mock-fill requestAnimationFrame
27+
global.requestAnimationFrame = function(callback) {
28+
setTimeout(callback, 0);
29+
};

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

+37-37
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,53 @@
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}'],
25-
setupFiles: [resolve('config/polyfills.js')],
24+
collectCoverageFrom: ["src/**/*.{js,jsx}"],
25+
setupFiles: [resolve("config/polyfills.js")],
2626
setupTestFrameworkScriptFile: setupTestsFile,
2727
testMatch: [
28-
'<rootDir>/src/**/__tests__/**/*.js?(x)',
29-
'<rootDir>/src/**/?(*.)(spec|test).js?(x)',
28+
"<rootDir>/src/**/__tests__/**/*.js?(x)",
29+
"<rootDir>/src/**/?(*.)(spec|test).js?(x)"
3030
],
31-
testEnvironment: 'node',
32-
testURL: 'http://localhost',
31+
testEnvironment: "node",
32+
testURL: "http://localhost",
3333
transform: {
34-
'^.+\\.(js|jsx)$': isEjecting
35-
? '<rootDir>/node_modules/babel-jest'
36-
: resolve('config/jest/babelTransform.js'),
37-
'^.+\\.css$': resolve('config/jest/cssTransform.js'),
38-
'^(?!.*\\.(js|jsx|css|json)$)': resolve('config/jest/fileTransform.js'),
34+
"^.+\\.(js|jsx)$": isEjecting
35+
? "<rootDir>/node_modules/babel-jest"
36+
: resolve("config/jest/babelTransform.js"),
37+
"^.+\\.(css|s[ca]ss)$": resolve("config/jest/cssTransform.js"),
38+
"^(?!.*\\.(js|jsx|css|json)$)": resolve("config/jest/fileTransform.js")
3939
},
40-
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
40+
transformIgnorePatterns: ["[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$"],
4141
moduleNameMapper: {
42-
'^react-native$': 'react-native-web',
42+
"^react-native$": "react-native-web"
4343
},
44-
moduleFileExtensions: ['web.js', 'js', 'json', 'web.jsx', 'jsx', 'node'],
44+
moduleFileExtensions: ["web.js", "js", "json", "web.jsx", "jsx", "node"]
4545
};
4646
if (rootDir) {
4747
config.rootDir = rootDir;
4848
}
4949
const overrides = Object.assign({}, require(paths.appPackageJson).jest);
5050
const supportedKeys = [
51-
'collectCoverageFrom',
52-
'coverageReporters',
53-
'coverageThreshold',
54-
'snapshotSerializers',
51+
"collectCoverageFrom",
52+
"coverageReporters",
53+
"coverageThreshold",
54+
"snapshotSerializers"
5555
];
5656
if (overrides) {
5757
supportedKeys.forEach(key => {
@@ -64,21 +64,21 @@ module.exports = (resolve, rootDir, isEjecting) => {
6464
if (unsupportedKeys.length) {
6565
console.error(
6666
chalk.red(
67-
'Out of the box, Create React App only supports overriding ' +
68-
'these Jest options:\n\n' +
69-
supportedKeys.map(key => chalk.bold(' \u2022 ' + key)).join('\n') +
70-
'.\n\n' +
71-
'These options in your package.json Jest configuration ' +
72-
'are not currently supported by Create React App:\n\n' +
67+
"Out of the box, Create React App only supports overriding " +
68+
"these Jest options:\n\n" +
69+
supportedKeys.map(key => chalk.bold(" \u2022 " + key)).join("\n") +
70+
".\n\n" +
71+
"These options in your package.json Jest configuration " +
72+
"are not currently supported by Create React App:\n\n" +
7373
unsupportedKeys
74-
.map(key => chalk.bold(' \u2022 ' + key))
75-
.join('\n') +
76-
'\n\nIf you wish to override other Jest options, you need to ' +
77-
'eject from the default setup. You can do so by running ' +
78-
chalk.bold('npm run eject') +
79-
' but remember that this is a one-way operation. ' +
80-
'You may also file an issue with Create React App to discuss ' +
81-
'supporting more options out of the box.\n'
74+
.map(key => chalk.bold(" \u2022 " + key))
75+
.join("\n") +
76+
"\n\nIf you wish to override other Jest options, you need to " +
77+
"eject from the default setup. You can do so by running " +
78+
chalk.bold("npm run eject") +
79+
" but remember that this is a one-way operation. " +
80+
"You may also file an issue with Create React App to discuss " +
81+
"supporting more options out of the box.\n"
8282
)
8383
);
8484
process.exit(1);

packages/react-scripts/yarn.lock

+9-12
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)