Skip to content

Commit 5c352cb

Browse files
committed
E2E: run tests when react is ready
1 parent bc2fc80 commit 5c352cb

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"dependencies": {
33
"babel-preset-latest": "6.16.0",
4-
"babel-register": "6.18.0",
4+
"babel-register": "6.22.0",
55
"babel-polyfill": "6.20.0",
66
"chai": "3.5.0",
77
"jsdom": "9.8.3",

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ const path = require('path')
55

66
let getMarkup
77
let resourceLoader
8-
// this value could be tweaked in order to let the resource
9-
// retriever get every file and jsdom execute react
10-
let timeToWaitForJsToExecute
8+
let resolveOnReady
119

1210
if (process.env.E2E_FILE) {
1311
const file = path.isAbsolute(process.env.E2E_FILE)
@@ -22,7 +20,7 @@ if (process.env.E2E_FILE) {
2220
fs.readFileSync(path.join(path.dirname(file), resource.url.pathname), 'utf8')
2321
)
2422

25-
timeToWaitForJsToExecute = 0
23+
resolveOnReady = (doc, resolve) => doc.defaultView.addEventListener('load', () => resolve(doc), false)
2624
} else if (process.env.E2E_URL) {
2725
getMarkup = () => new Promise(resolve => {
2826
http.get(process.env.E2E_URL, (res) => {
@@ -36,7 +34,7 @@ if (process.env.E2E_FILE) {
3634
return resource.defaultFetch(callback)
3735
}
3836

39-
timeToWaitForJsToExecute = 100
37+
resolveOnReady = (doc, resolve) => doc.addEventListener('ReactFeatureDidMount', () => resolve(doc), false)
4038
} else {
4139
it.only('can run jsdom (at least one of "E2E_FILE" or "E2E_URL" environment variables must be provided)', () => {
4240
expect(new Error('This isn\'t the error you are looking for.')).toBeUndefined()
@@ -47,16 +45,14 @@ export default feature => new Promise(async resolve => {
4745
const markup = await getMarkup()
4846
const host = process.env.E2E_URL || 'http://localhost:3000'
4947
const doc = jsdom.jsdom(markup, {
50-
features : {
51-
FetchExternalResources : ['script', 'css'],
52-
ProcessExternalResources : ['script'],
48+
features: {
49+
FetchExternalResources: ['script', 'css'],
50+
ProcessExternalResources: ['script'],
5351
},
5452
resourceLoader,
5553
url: `${host}#${feature}`,
5654
virtualConsole: jsdom.createVirtualConsole().sendTo(console),
5755
})
5856

59-
doc.defaultView.addEventListener('load', () => {
60-
setTimeout(() => resolve(doc), timeToWaitForJsToExecute)
61-
}, false)
57+
resolveOnReady(doc, resolve)
6258
})

packages/react-scripts/fixtures/kitchensink/src/App.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import React from 'react';
22

3+
class BuiltEmitter extends React.Component {
4+
componentDidMount() {
5+
document.dispatchEvent(new Event('ReactFeatureDidMount'));
6+
}
7+
8+
render() {
9+
return <div>{this.props.children}</div>
10+
}
11+
}
12+
313
class App extends React.Component {
414
constructor(props) {
515
super(props);
@@ -96,7 +106,7 @@ class App extends React.Component {
96106

97107
render() {
98108
const Feature = this.state.feature;
99-
return Feature ? <Feature /> : null;
109+
return Feature ? <BuiltEmitter><Feature /></BuiltEmitter> : null;
100110
}
101111
}
102112

tasks/e2e-kitchensink.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ E2E_URL="http://localhost:3001" \
151151
E2E_FILE=./build/index.html \
152152
CI=true \
153153
NODE_PATH=src \
154-
node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.js
154+
node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
155155

156156
# Uncomment when snapshot testing is enabled by default:
157157
# test -e src/__snapshots__/App.test.js.snap
@@ -182,6 +182,8 @@ NODE_PATH=src REACT_APP_SHELL_ENV_MESSAGE=fromtheshell npm run build
182182
test -e build/*.html
183183
test -e build/static/js/main.*.js
184184

185+
cat package.json
186+
185187
# Unit tests
186188
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
187189
CI=true \
@@ -199,14 +201,14 @@ E2E_URL="http://localhost:3002" \
199201
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
200202
CI=true NODE_PATH=src \
201203
NODE_ENV=production \
202-
node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.js
204+
node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
203205

204206
# Test "production" environment
205207
E2E_FILE=./build/index.html \
206208
CI=true \
207209
NODE_ENV=production \
208210
NODE_PATH=src \
209-
node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.js
211+
node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
210212

211213
# Uncomment when snapshot testing is enabled by default:
212214
# test -e src/__snapshots__/App.test.js.snap

0 commit comments

Comments
 (0)