e2e-simple.sh 10.4 KB
Newer Older
1
#!/bin/bash
Christopher Chedeau's avatar
Christopher Chedeau committed
2
# Copyright (c) 2015-present, Facebook, Inc.
Joe Haddad's avatar
Joe Haddad committed
3
4
5
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
Christopher Chedeau's avatar
Christopher Chedeau committed
6

7
8
9
10
11
# ******************************************************************************
# This is an end-to-end test intended to run on CI.
# You can also run it locally but it's slow.
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
12
# Start in tasks/ even if run from root directory
Christopher Chedeau's avatar
Christopher Chedeau committed
13
14
cd "$(dirname "$0")"

15
16
17
18
19
# CLI and app temporary locations
# http://unix.stackexchange.com/a/84980
temp_cli_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_cli_path'`
temp_app_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_app_path'`

20
21
function cleanup {
  echo 'Cleaning up.'
22
  cd "$root_path"
Dan Abramov's avatar
Dan Abramov committed
23
  # Uncomment when snapshot testing is enabled by default:
Ville Immonen's avatar
Ville Immonen committed
24
  # rm ./packages/react-scripts/template/src/__snapshots__/App.test.js.snap
25
  rm -rf "$temp_cli_path" $temp_app_path
26
27
}

Dan Abramov's avatar
Dan Abramov committed
28
# Error messages are redirected to stderr
29
function handle_error {
Dan Abramov's avatar
Dan Abramov committed
30
  echo "$(basename $0): ERROR! An error was encountered executing line $1." 1>&2;
Dan Abramov's avatar
Dan Abramov committed
31
  cleanup
32
  echo 'Exiting with error.' 1>&2;
33
34
35
36
37
  exit 1
}

function handle_exit {
  cleanup
38
  echo 'Exiting without error.' 1>&2;
39
40
41
  exit
}

42
function create_react_app {
43
  node "$temp_cli_path"/node_modules/create-react-app/index.js "$@"
44
45
}

46
47
48
49
50
51
52
53
54
55
56
57
58
59
function install_package {
  local pkg=$(basename $1)

  # Clean target (for safety)
  rm -rf node_modules/$pkg/
  rm -rf node_modules/**/$pkg/

  # Copy package into node_modules/ ignoring installed deps
  # rsync -a ${1%/} node_modules/ --exclude node_modules
  cp -R ${1%/} node_modules/
  rm -rf node_modules/$pkg/node_modules/

  # Install `dependencies`
  cd node_modules/$pkg/
60
  yarn --production
61
62
63
64
65
  # Remove our packages to ensure side-by-side versions are used (which we link)
  rm -rf node_modules/{babel-preset-react-app,eslint-config-react-app,react-dev-utils,react-error-overlay,react-scripts}
  cd ../..
}

66
67
68
69
70
71
72
# Check for the existence of one or more files.
function exists {
  for f in $*; do
    test -e "$f"
  done
}

73
74
75
76
77
# Exit the script with a helpful error message when any error is encountered
trap 'set +x; handle_error $LINENO $BASH_COMMAND' ERR

# Cleanup before exit on any termination signal
trap 'set +x; handle_exit' SIGQUIT SIGTERM SIGINT SIGKILL SIGHUP
Christopher Chedeau's avatar
Christopher Chedeau committed
78
79
80
81

# Echo every command being executed
set -x

Dan Abramov's avatar
Dan Abramov committed
82
# Go to root
Christopher Chedeau's avatar
Christopher Chedeau committed
83
cd ..
Dan Abramov's avatar
Dan Abramov committed
84
root_path=$PWD
85

86
87
88
89
90
91
92
93
94
95
96
97
98
# Make sure we don't introduce accidental references to PATENTS.
EXPECTED='packages/react-error-overlay/fixtures/bundle.mjs
packages/react-error-overlay/fixtures/bundle.mjs.map
packages/react-error-overlay/fixtures/bundle_u.mjs
packages/react-error-overlay/fixtures/bundle_u.mjs.map
tasks/e2e-simple.sh'
ACTUAL=$(git grep -l PATENTS)
if [ "$EXPECTED" != "$ACTUAL" ]; then
  echo "PATENTS crept into some new files?"
  diff -u <(echo "$EXPECTED") <(echo "$ACTUAL") || true
  exit 1
fi

99
# Prevent bootstrap, we only want top-level dependencies
100
cp package.json package.json.bak
101
grep -v "postinstall" package.json > temp && mv temp package.json
102
yarn
103
104
mv package.json.bak package.json

105
106
107
# We removed the postinstall, so do it manually here
node bootstrap.js

Dan Abramov's avatar
Dan Abramov committed
108
# Lint own code
109
110
111
112
113
114
115
./node_modules/.bin/eslint --max-warnings 0 packages/babel-preset-react-app/
./node_modules/.bin/eslint --max-warnings 0 packages/create-react-app/
./node_modules/.bin/eslint --max-warnings 0 packages/eslint-config-react-app/
./node_modules/.bin/eslint --max-warnings 0 packages/react-dev-utils/
./node_modules/.bin/eslint --max-warnings 0 packages/react-scripts/
cd packages/react-error-overlay/
./node_modules/.bin/eslint --max-warnings 0 src/
116
117
yarn test
yarn build:prod
118
cd ../..
119
cd packages/react-dev-utils/
120
yarn test
121
cd ../..
122

Dan Abramov's avatar
Dan Abramov committed
123
124
125
126
127
# ******************************************************************************
# First, test the create-react-app development environment.
# This does not affect our users but makes sure we can develop it.
# ******************************************************************************

Max's avatar
Max committed
128
# Test local build command
129
yarn build
Max's avatar
Max committed
130
# Check for expected output
131
132
133
134
135
exists build/*.html
exists build/static/js/*.js
exists build/static/css/*.css
exists build/static/media/*.svg
exists build/favicon.ico
Max's avatar
Max committed
136

137
# Run tests with CI flag
138
CI=true yarn test
Dan Abramov's avatar
Dan Abramov committed
139
# Uncomment when snapshot testing is enabled by default:
140
# exists template/src/__snapshots__/App.test.js.snap
Dan Abramov's avatar
Dan Abramov committed
141
142

# Test local start command
143
yarn start --smoke-test
Christoph Pojer's avatar
Christoph Pojer committed
144

Dan Abramov's avatar
Dan Abramov committed
145
146
147
148
# ******************************************************************************
# Next, pack react-scripts and create-react-app so we can verify they work.
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
149
# Pack CLI
150
cd "$root_path"/packages/create-react-app
Christopher Chedeau's avatar
Christopher Chedeau committed
151
152
cli_path=$PWD/`npm pack`

Dan Abramov's avatar
Dan Abramov committed
153
# Go to react-scripts
154
cd "$root_path"/packages/react-scripts
Ville Immonen's avatar
Ville Immonen committed
155

156
157
158
159
160
# Save package.json because we're going to touch it
cp package.json package.json.orig

# Replace own dependencies (those in the `packages` dir) with the local paths
# of those packages.
161
node "$root_path"/tasks/replace-own-deps.js
Ville Immonen's avatar
Ville Immonen committed
162

Dan Abramov's avatar
Dan Abramov committed
163
# Finally, pack react-scripts
164
scripts_path="$root_path"/packages/react-scripts/`npm pack`
Dan Abramov's avatar
Dan Abramov committed
165

166
167
168
169
# Restore package.json
rm package.json
mv package.json.orig package.json

Dan Abramov's avatar
Dan Abramov committed
170
171
172
173
# ******************************************************************************
# Now that we have packed them, create a clean app folder and install them.
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
174
# Install the CLI in a temporary location
175
cd "$temp_cli_path"
176
177
178

# Initialize package.json before installing the CLI because npm will not install
# the CLI properly in the temporary location if it is missing.
179
yarn init --yes
180
181

# Now we can install the CLI from the local package.
182
yarn add "$cli_path"
Christopher Chedeau's avatar
Christopher Chedeau committed
183
184
185

# Install the app in a temporary location
cd $temp_app_path
186
create_react_app --scripts-version="$scripts_path" test-app
Dan Abramov's avatar
Dan Abramov committed
187
188
189
190
191
192

# ******************************************************************************
# Now that we used create-react-app to create an app depending on react-scripts,
# let's make sure all npm scripts are in the working state.
# ******************************************************************************

193
194
195
196
197
198
199
200
201
202
function verify_env_url {
  # Backup package.json because we're going to make it dirty
  cp package.json package.json.orig

  # Test default behavior
  grep -F -R --exclude=*.map "\"/static/" build/ -q; test $? -eq 0 || exit 1

  # Test relative path build
  awk -v n=2 -v s="  \"homepage\": \".\"," 'NR == n {print s} {print}' package.json > tmp && mv tmp package.json

203
  yarn build
204
205
206
207
208
  # Disabled until this can be tested
  # grep -F -R --exclude=*.map "../../static/" build/ -q; test $? -eq 0 || exit 1
  grep -F -R --exclude=*.map "\"./static/" build/ -q; test $? -eq 0 || exit 1
  grep -F -R --exclude=*.map "\"/static/" build/ -q; test $? -eq 1 || exit 1

209
  PUBLIC_URL="/anabsolute" yarn build
210
211
212
213
214
215
  grep -F -R --exclude=*.map "/anabsolute/static/" build/ -q; test $? -eq 0 || exit 1
  grep -F -R --exclude=*.map "\"/static/" build/ -q; test $? -eq 1 || exit 1

  # Test absolute path build
  sed "2s/.*/  \"homepage\": \"\/testingpath\",/" package.json > tmp && mv tmp package.json

216
  yarn build
217
218
219
  grep -F -R --exclude=*.map "/testingpath/static/" build/ -q; test $? -eq 0 || exit 1
  grep -F -R --exclude=*.map "\"/static/" build/ -q; test $? -eq 1 || exit 1

220
  PUBLIC_URL="https://www.example.net/overridetest" yarn build
221
222
223
224
225
226
227
  grep -F -R --exclude=*.map "https://www.example.net/overridetest/static/" build/ -q; test $? -eq 0 || exit 1
  grep -F -R --exclude=*.map "\"/static/" build/ -q; test $? -eq 1 || exit 1
  grep -F -R --exclude=*.map "testingpath/static" build/ -q; test $? -eq 1 || exit 1

  # Test absolute url build
  sed "2s/.*/  \"homepage\": \"https:\/\/www.example.net\/testingpath\",/" package.json > tmp && mv tmp package.json

228
  yarn build
229
230
231
  grep -F -R --exclude=*.map "/testingpath/static/" build/ -q; test $? -eq 0 || exit 1
  grep -F -R --exclude=*.map "\"/static/" build/ -q; test $? -eq 1 || exit 1

232
  PUBLIC_URL="https://www.example.net/overridetest" yarn build
233
234
235
236
237
238
239
240
241
  grep -F -R --exclude=*.map "https://www.example.net/overridetest/static/" build/ -q; test $? -eq 0 || exit 1
  grep -F -R --exclude=*.map "\"/static/" build/ -q; test $? -eq 1 || exit 1
  grep -F -R --exclude=*.map "testingpath/static" build/ -q; test $? -eq 1 || exit 1

  # Restore package.json
  rm package.json
  mv package.json.orig package.json
}

Joe Haddad's avatar
Joe Haddad committed
242
243
244
245
246
247
248
249
250
251
252
function verify_module_scope {
  # Create stub json file
  echo "{}" >> sample.json

  # Save App.js, we're going to modify it
  cp src/App.js src/App.js.bak

  # Add an out of scope import
  echo "import sampleJson from '../sample'" | cat - src/App.js > src/App.js.temp && mv src/App.js.temp src/App.js

  # Make sure the build fails
253
  yarn build; test $? -eq 1 || exit 1
Joe Haddad's avatar
Joe Haddad committed
254
255
256
257
258
259
260
  # TODO: check for error message

  # Restore App.js
  rm src/App.js
  mv src/App.js.bak src/App.js
}

Dan Abramov's avatar
Dan Abramov committed
261
# Enter the app directory
Christopher Chedeau's avatar
Christopher Chedeau committed
262
263
264
cd test-app

# Test the build
265
yarn build
266
# Check for expected output
267
268
269
270
271
exists build/*.html
exists build/static/js/*.js
exists build/static/css/*.css
exists build/static/media/*.svg
exists build/favicon.ico
272

273
# Run tests with CI flag
274
CI=true yarn test
Dan Abramov's avatar
Dan Abramov committed
275
# Uncomment when snapshot testing is enabled by default:
276
# exists src/__snapshots__/App.test.js.snap
Christoph Pojer's avatar
Christoph Pojer committed
277

Dan Abramov's avatar
Dan Abramov committed
278
# Test the server
279
yarn start --smoke-test
Dan Abramov's avatar
Dan Abramov committed
280

281
282
283
# Test environment handling
verify_env_url

Joe Haddad's avatar
Joe Haddad committed
284
285
286
# Test reliance on webpack internals
verify_module_scope

Dan Abramov's avatar
Dan Abramov committed
287
288
289
290
# ******************************************************************************
# Finally, let's check that everything still works after ejecting.
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
291
# Eject...
Christopher Chedeau's avatar
Christopher Chedeau committed
292
293
echo yes | npm run eject

Dan Abramov's avatar
Dan Abramov committed
294
# ...but still link to the local packages
295
296
297
install_package "$root_path"/packages/babel-preset-react-app
install_package "$root_path"/packages/eslint-config-react-app
install_package "$root_path"/packages/react-dev-utils
Dan Abramov's avatar
Dan Abramov committed
298

Dan Abramov's avatar
Dan Abramov committed
299
# Test the build
300
yarn build
301
# Check for expected output
302
303
304
305
306
exists build/*.html
exists build/static/js/*.js
exists build/static/css/*.css
exists build/static/media/*.svg
exists build/favicon.ico
307

Dan Abramov's avatar
Dan Abramov committed
308
# Run tests, overring the watch option to disable it.
309
# `CI=true yarn test` won't work here because `yarn test` becomes just `jest`.
Dan Abramov's avatar
Dan Abramov committed
310
311
# We should either teach Jest to respect CI env variable, or make
# `scripts/test.js` survive ejection (right now it doesn't).
312
yarn test --watch=no
Dan Abramov's avatar
Dan Abramov committed
313
# Uncomment when snapshot testing is enabled by default:
314
# exists src/__snapshots__/App.test.js.snap
Christoph Pojer's avatar
Christoph Pojer committed
315

Dan Abramov's avatar
Dan Abramov committed
316
# Test the server
317
yarn start --smoke-test
Dan Abramov's avatar
Dan Abramov committed
318

319
320
# Test environment handling
verify_env_url
321

Joe Haddad's avatar
Joe Haddad committed
322
323
324
# Test reliance on webpack internals
verify_module_scope

Christopher Chedeau's avatar
Christopher Chedeau committed
325
# Cleanup
326
cleanup