Skip to content

Commit 40a8788

Browse files
committed
Add e2e tests
1 parent 511d1c4 commit 40a8788

File tree

7 files changed

+56
-12
lines changed

7 files changed

+56
-12
lines changed

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

+14-6
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,30 @@ import initDOM from './initDOM'
33

44
describe('Integration', () => {
55
describe('Environment variables', () => {
6+
it('file env variables', async () => {
7+
const doc = await initDOM('file-env-variables')
8+
9+
expect(doc.getElementById('feature-file-env-variables').textContent).to.equal('fromtheenvfile.')
10+
})
11+
612
it('NODE_PATH', async () => {
713
const doc = await initDOM('node-path')
814

915
expect(doc.getElementById('feature-node-path').childElementCount).to.equal(4)
1016
})
1117

12-
it('shell env variables', async () => {
13-
const doc = await initDOM('shell-env-variables')
18+
it('PUBLIC_URL', async () => {
19+
const doc = await initDOM('public-url')
1420

15-
expect(doc.getElementById('feature-shell-env-variables').textContent).to.equal('fromtheshell.')
21+
expect(doc.getElementById('feature-public-url').textContent).to.equal('http://www.example.org/spa.')
22+
expect(doc.querySelector('head link[rel="shortcut icon"]').getAttribute('href'))
23+
.to.equal('http://www.example.org/spa/favicon.ico')
1624
})
1725

18-
it('file env variables', async () => {
19-
const doc = await initDOM('file-env-variables')
26+
it('shell env variables', async () => {
27+
const doc = await initDOM('shell-env-variables')
2028

21-
expect(doc.getElementById('feature-file-env-variables').textContent).to.equal('fromtheenvfile.')
29+
expect(doc.getElementById('feature-shell-env-variables').textContent).to.equal('fromtheshell.')
2230
})
2331
})
2432
})

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ if (process.env.E2E_FILE) {
1515
const markup = fs.readFileSync(file, 'utf8')
1616
getMarkup = () => markup
1717

18+
const pathPrefix = process.env.PUBLIC_URL.replace(/^https?:\/\/[^\/]+\/?/, '')
19+
1820
resourceLoader = (resource, callback) => callback(
1921
null,
20-
fs.readFileSync(path.join(path.dirname(file), resource.url.pathname), 'utf8')
22+
fs.readFileSync(path.join(path.dirname(file), resource.url.pathname.replace(pathPrefix, '')), 'utf8')
2123
)
2224
} else if (process.env.E2E_URL) {
2325
getMarkup = () => new Promise(resolve => {
@@ -37,7 +39,7 @@ if (process.env.E2E_FILE) {
3739

3840
export default feature => new Promise(async resolve => {
3941
const markup = await getMarkup()
40-
const host = process.env.E2E_URL || 'http://localhost:3000'
42+
const host = process.env.E2E_URL || 'http://www.example.org/spa:3000'
4143
const doc = jsdom.jsdom(markup, {
4244
features: {
4345
FetchExternalResources: ['script', 'css'],

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class App extends React.Component {
3232
}
3333

3434
componentDidMount() {
35-
switch (location.hash.slice(1)) {
35+
const feature = location.hash.slice(1)
36+
switch (feature) {
3637
case 'array-destructuring':
3738
require.ensure([], () => this.setFeature(require('./features/syntax/ArrayDestructuring').default));
3839
break;
@@ -87,6 +88,9 @@ class App extends React.Component {
8788
case 'promises':
8889
require.ensure([], () => this.setFeature(require('./features/syntax/Promises').default));
8990
break;
91+
case 'public-url':
92+
require.ensure([], () => this.setFeature(require('./features/env/PublicUrl').default));
93+
break;
9094
case 'rest-and-default':
9195
require.ensure([], () => this.setFeature(require('./features/syntax/RestAndDefault').default));
9296
break;
@@ -106,6 +110,9 @@ class App extends React.Component {
106110
require.ensure([], () => this.setFeature(require('./features/webpack/UnknownExtInclusion').default));
107111
break;
108112
default:
113+
if (feature) {
114+
throw new Error(`Missing feature "${feature}"`);
115+
}
109116
this.setFeature(null);
110117
break;
111118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react'
2+
3+
export default () => (
4+
<span id="feature-public-url">{process.env.PUBLIC_URL}.</span>
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import PublicUrl from './PublicUrl';
4+
5+
describe('PUBLIC_URL', () => {
6+
it('renders without crashing', () => {
7+
const div = document.createElement('div');
8+
ReactDOM.render(<PublicUrl />, div);
9+
});
10+
});

packages/react-scripts/scripts/start.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ function runDevServer(host, port, protocol) {
241241
// project directory is dangerous because we may expose sensitive files.
242242
// Instead, we establish a convention that only files in `public` directory
243243
// get served. Our build script will copy `public` into the `build` folder.
244-
// In `index.html`, you can get URL of `public` folder with %PUBLIC_PATH%:
244+
// In `index.html`, you can get URL of `public` folder with %PUBLIC_URL%:
245245
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
246246
// In JavaScript code, you can access it with `process.env.PUBLIC_URL`.
247247
// Note that we only recommend to use `public` folder as an escape hatch

tasks/e2e-kitchensink.sh

+14-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ create_react_app --scripts-version=$scripts_path --internal-testing-template=$ro
111111
cd test-kitchensink
112112

113113
# Test the build
114-
NODE_PATH=src REACT_APP_SHELL_ENV_MESSAGE=fromtheshell npm run build
114+
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
115+
NODE_PATH=src \
116+
PUBLIC_URL=http://www.example.org/spa/ \
117+
npm run build
118+
115119
# Check for expected output
116120
test -e build/*.html
117121
test -e build/static/js/main.*.js
@@ -127,6 +131,7 @@ tmp_server_log=`mktemp`
127131
PORT=3001 \
128132
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
129133
NODE_PATH=src \
134+
PUBLIC_URL=http://www.example.org/spa/ \
130135
nohup npm start &>$tmp_server_log &
131136
grep -q 'The app is running at:' <(tail -f $tmp_server_log)
132137
E2E_URL="http://localhost:3001" \
@@ -138,6 +143,7 @@ E2E_URL="http://localhost:3001" \
138143
E2E_FILE=./build/index.html \
139144
CI=true \
140145
NODE_PATH=src \
146+
PUBLIC_URL=http://www.example.org/spa/ \
141147
node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
142148

143149
# ******************************************************************************
@@ -157,7 +163,11 @@ npm link $root_path/packages/react-scripts
157163
rm .babelrc
158164

159165
# Test the build
160-
NODE_PATH=src REACT_APP_SHELL_ENV_MESSAGE=fromtheshell npm run build
166+
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
167+
NODE_PATH=src \
168+
PUBLIC_URL=http://www.example.org/spa/ \
169+
npm run build
170+
161171
# Check for expected output
162172
test -e build/*.html
163173
test -e build/static/js/main.*.js
@@ -173,6 +183,7 @@ tmp_server_log=`mktemp`
173183
PORT=3002 \
174184
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
175185
NODE_PATH=src \
186+
PUBLIC_URL=http://www.example.org/spa/ \
176187
nohup npm start &>$tmp_server_log &
177188
grep -q 'The app is running at:' <(tail -f $tmp_server_log)
178189
E2E_URL="http://localhost:3002" \
@@ -186,6 +197,7 @@ E2E_FILE=./build/index.html \
186197
CI=true \
187198
NODE_ENV=production \
188199
NODE_PATH=src \
200+
PUBLIC_URL=http://www.example.org/spa/ \
189201
node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
190202

191203
# Cleanup

0 commit comments

Comments
 (0)