Skip to content

Commit 76fea02

Browse files
ianschmitziansu
andcommitted
Cleanup Jest config (#6654)
General cleanup after the [Jest 24 PR](#6278). This also includes `jsdom@14` via https://www.npmjs.com/package/jest-environment-jsdom-fourteen. Since we have a node >= 8.10 requirement, we are able to use the latest version of `jsdom` which includes additional implementations of browser APIs such as `MutationObserver` (which we had an issue filed for over at #6617). /cc @SimenB. Is there more you recommend we do to cleanup our [Jest config](https://github.com/facebook/create-react-app/blob/b0cbf2caa18ee8267855b14578ebc3dee826f552/packages/react-scripts/scripts/utils/createJestConfig.js) for TypeScript? Co-authored-by: Ian Sutherland <ian@iansutherland.ca>
1 parent e630238 commit 76fea02

File tree

15 files changed

+165
-149
lines changed

15 files changed

+165
-149
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"get-port": "^4.2.0",
2626
"globby": "^9.1.0",
2727
"husky": "^1.3.1",
28-
"jest": "^24.5.0",
28+
"jest": "24.7.1",
2929
"lerna": "2.9.1",
3030
"lerna-changelog": "~0.8.2",
3131
"lint-staged": "^8.0.4",

packages/babel-plugin-named-asset-import/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"@babel/core": "^7.1.0"
1616
},
1717
"devDependencies": {
18-
"babel-plugin-tester": "^5.5.1",
19-
"jest": "^24.5.0"
18+
"babel-plugin-tester": "^6.0.1",
19+
"jest": "24.7.1"
2020
},
2121
"scripts": {
2222
"test": "jest"

packages/confusing-browser-globals/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
"index.js"
1717
],
1818
"devDependencies": {
19-
"jest": "24.5.0"
19+
"jest": "24.7.1"
2020
}
2121
}

packages/react-dev-utils/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
},
7575
"devDependencies": {
7676
"cross-env": "^5.2.0",
77-
"jest": "^24.5.0"
77+
"jest": "24.7.1"
7878
},
7979
"scripts": {
8080
"test": "cross-env FORCE_COLOR=true jest"

packages/react-error-overlay/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@
3333
"@babel/code-frame": "7.0.0",
3434
"@babel/core": "7.3.4",
3535
"anser": "1.4.8",
36-
"babel-core": "7.0.0-bridge.0",
3736
"babel-eslint": "10.0.1",
38-
"babel-jest": "24.5.0",
37+
"babel-jest": "24.7.1",
3938
"babel-loader": "8.0.5",
4039
"babel-preset-react-app": "^7.0.2",
4140
"chalk": "^2.4.2",
@@ -49,7 +48,7 @@
4948
"eslint-plugin-react": "7.12.4",
5049
"flow-bin": "^0.63.1",
5150
"html-entities": "1.2.1",
52-
"jest": "24.5.0",
51+
"jest": "24.7.1",
5352
"jest-fetch-mock": "2.1.1",
5453
"object-assign": "4.1.1",
5554
"promise": "8.0.2",

packages/react-scripts/fixtures/kitchensink/.template.dependencies.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"dependencies": {
33
"bootstrap": "4.1.1",
4-
"jest": "24.5.0",
4+
"jest": "24.7.1",
55
"node-sass": "4.8.3",
66
"normalize.css": "7.0.0",
77
"prop-types": "15.5.6",

packages/react-scripts/fixtures/kitchensink/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Tests are automatically run by the CI tools.
88
In order to run them locally, without having to manually install and configure everything, the `yarn e2e:docker` CLI command can be used.
99

1010
This is a simple script that runs a **Docker** container, where the node version, git branch to clone, test suite, and whether to run it with `yarn` or `npm` can be chosen.
11-
Simply run `yarn e2e:docker -- --help` to get additional info.
11+
Simply run `yarn e2e:docker --help` to get additional info.
1212

1313
If you need guidance installing **Docker**, you should follow their [official docs](https://docs.docker.com/engine/installation/).
1414

packages/react-scripts/fixtures/kitchensink/integration/env.test.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ import initDOM from './initDOM';
99

1010
describe('Integration', () => {
1111
describe('Environment variables', () => {
12+
let doc;
13+
14+
afterEach(() => {
15+
doc && doc.defaultView.close();
16+
doc = undefined;
17+
});
18+
1219
it('file env variables', async () => {
13-
const doc = await initDOM('file-env-variables');
20+
doc = await initDOM('file-env-variables');
1421

1522
expect(
1623
doc.getElementById('feature-file-env-original-1').textContent
@@ -34,18 +41,16 @@ describe('Integration', () => {
3441
'x-from-development-env'
3542
);
3643
}
37-
doc.defaultView.close();
3844
});
3945

4046
it('NODE_PATH', async () => {
41-
const doc = await initDOM('node-path');
47+
doc = await initDOM('node-path');
4248

4349
expect(doc.getElementById('feature-node-path').childElementCount).toBe(4);
44-
doc.defaultView.close();
4550
});
4651

4752
it('PUBLIC_URL', async () => {
48-
const doc = await initDOM('public-url');
53+
doc = await initDOM('public-url');
4954

5055
const prefix =
5156
process.env.NODE_ENV === 'development'
@@ -57,20 +62,18 @@ describe('Integration', () => {
5762
expect(
5863
doc.querySelector('head link[rel="shortcut icon"]').getAttribute('href')
5964
).toBe(`${prefix}/favicon.ico`);
60-
doc.defaultView.close();
6165
});
6266

6367
it('shell env variables', async () => {
64-
const doc = await initDOM('shell-env-variables');
68+
doc = await initDOM('shell-env-variables');
6569

6670
expect(
6771
doc.getElementById('feature-shell-env-variables').textContent
6872
).toBe('fromtheshell.');
69-
doc.defaultView.close();
7073
});
7174

7275
it('expand .env variables', async () => {
73-
const doc = await initDOM('expand-env-variables');
76+
doc = await initDOM('expand-env-variables');
7477

7578
expect(doc.getElementById('feature-expand-env-1').textContent).toBe(
7679
'basic'
@@ -84,7 +87,6 @@ describe('Integration', () => {
8487
expect(
8588
doc.getElementById('feature-expand-env-existing').textContent
8689
).toBe('fromtheshell');
87-
doc.defaultView.close();
8890
});
8991
});
9092
});

packages/react-scripts/fixtures/kitchensink/integration/initDOM.js

+59-46
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,31 @@
66
*/
77

88
const fs = require('fs');
9-
const http = require('http');
10-
const jsdom = require('jsdom/lib/old-api.js');
9+
const { JSDOM, ResourceLoader } = require('jsdom');
1110
const path = require('path');
11+
const url = require('url');
1212

13-
let getMarkup;
14-
export let resourceLoader;
15-
16-
if (process.env.E2E_FILE) {
17-
const file = path.isAbsolute(process.env.E2E_FILE)
13+
const file =
14+
process.env.E2E_FILE &&
15+
(path.isAbsolute(process.env.E2E_FILE)
1816
? process.env.E2E_FILE
19-
: path.join(process.cwd(), process.env.E2E_FILE);
20-
21-
const markup = fs.readFileSync(file, 'utf8');
22-
getMarkup = () => markup;
17+
: path.join(process.cwd(), process.env.E2E_FILE));
2318

19+
export const fetchFile = url => {
2420
const pathPrefix = process.env.PUBLIC_URL.replace(/^https?:\/\/[^/]+\/?/, '');
21+
return fs.readFileSync(
22+
path.join(path.dirname(file), url.pathname.replace(pathPrefix, '')),
23+
'utf8'
24+
);
25+
};
2526

26-
resourceLoader = (resource, callback) =>
27-
callback(
28-
null,
29-
fs.readFileSync(
30-
path.join(
31-
path.dirname(file),
32-
resource.url.pathname.replace(pathPrefix, '')
33-
),
34-
'utf8'
35-
)
36-
);
37-
} else if (process.env.E2E_URL) {
38-
getMarkup = () =>
39-
new Promise(resolve => {
40-
http.get(process.env.E2E_URL, res => {
41-
let rawData = '';
42-
res.on('data', chunk => (rawData += chunk));
43-
res.on('end', () => resolve(rawData));
44-
});
45-
});
27+
const fileResourceLoader = new class FileResourceLoader extends ResourceLoader {
28+
fetch(href, options) {
29+
return Promise.resolve(fetchFile(url.parse(href)));
30+
}
31+
}();
4632

47-
resourceLoader = (resource, callback) => resource.defaultFetch(callback);
48-
} else {
33+
if (!process.env.E2E_FILE && !process.env.E2E_URL) {
4934
it.only('can run jsdom (at least one of "E2E_FILE" or "E2E_URL" environment variables must be provided)', () => {
5035
expect(
5136
new Error("This isn't the error you are looking for.")
@@ -54,18 +39,46 @@ if (process.env.E2E_FILE) {
5439
}
5540

5641
export default feature =>
57-
new Promise(async resolve => {
58-
const markup = await getMarkup();
59-
const host = process.env.E2E_URL || 'http://www.example.org/spa:3000';
60-
const doc = jsdom.jsdom(markup, {
61-
created: (_, win) =>
62-
win.addEventListener('ReactFeatureDidMount', () => resolve(doc), true),
63-
deferClose: true,
64-
pretendToBeVisual: true,
65-
resourceLoader,
66-
url: `${host}#${feature}`,
67-
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
68-
});
42+
new Promise(async (resolve, reject) => {
43+
try {
44+
const host = process.env.E2E_URL || 'http://www.example.org/spa:3000';
45+
const url = `${host}#${feature}`;
46+
47+
let window;
48+
49+
if (process.env.E2E_FILE) {
50+
window = (await JSDOM.fromFile(file, {
51+
pretendToBeVisual: true,
52+
resources: fileResourceLoader,
53+
runScripts: 'dangerously',
54+
url,
55+
})).window;
56+
} else {
57+
window = (await JSDOM.fromURL(url, {
58+
pretendToBeVisual: true,
59+
resources: 'usable',
60+
runScripts: 'dangerously',
61+
})).window;
62+
}
63+
64+
const { document } = window;
65+
66+
document.addEventListener(
67+
'ReactFeatureDidMount',
68+
() => resolve(document),
69+
{ capture: true, once: true }
70+
);
71+
document.addEventListener(
72+
'ReactFeatureError',
73+
() => {
74+
// Cleanup jsdom instance since we don't need it anymore
75+
window.close();
6976

70-
doc.close();
77+
reject(`Error loading feature: ${feature}`);
78+
},
79+
{ capture: true, once: true }
80+
);
81+
} catch (e) {
82+
reject(e);
83+
}
7184
});

0 commit comments

Comments
 (0)