e2e-simple.sh 9.21 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")"

Joe Haddad's avatar
Joe Haddad committed
15
# App temporary location
16
17
# http://unix.stackexchange.com/a/84980
temp_app_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_app_path'`
18
19
20
custom_registry_url=http://localhost:4873
original_npm_registry_url=`npm get registry`
original_yarn_registry_url=`yarn config get registry`
21

22
23
function cleanup {
  echo 'Cleaning up.'
24
  cd "$root_path"
Dan Abramov's avatar
Dan Abramov committed
25
  # Uncomment when snapshot testing is enabled by default:
Ville Immonen's avatar
Ville Immonen committed
26
  # rm ./packages/react-scripts/template/src/__snapshots__/App.test.js.snap
Joe Haddad's avatar
Joe Haddad committed
27
  rm -rf "$temp_app_path"
28
29
  npm set registry "$original_npm_registry_url"
  yarn config set registry "$original_yarn_registry_url"
30
31
}

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

function handle_exit {
  cleanup
42
  echo 'Exiting without error.' 1>&2;
43
44
45
  exit
}

46
47
48
49
50
51
52
# Check for the existence of one or more files.
function exists {
  for f in $*; do
    test -e "$f"
  done
}

53
54
55
56
57
# 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
58
59
60
61

# Echo every command being executed
set -x

Dan Abramov's avatar
Dan Abramov committed
62
# Go to root
Christopher Chedeau's avatar
Christopher Chedeau committed
63
cd ..
Dan Abramov's avatar
Dan Abramov committed
64
root_path=$PWD
65

66
67
68
69
70
71
72
73
74
75
76
77
78
# 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

Joe Haddad's avatar
Joe Haddad committed
79
80
81
82
83
84
if hash npm 2>/dev/null
then
  npm i -g npm@latest
  npm cache clean || npm cache verify
fi

85
# Bootstrap monorepo
86
yarn
87

Joe Haddad's avatar
Joe Haddad committed
88
89
90
91
92
93
94
# Start local registry
tmp_registry_log=`mktemp`
nohup npx verdaccio@2.7.2 &>$tmp_registry_log &
# Wait for `verdaccio` to boot
grep -q 'http address' <(tail -f $tmp_registry_log)

# Set registry to local registry
95
96
npm set registry "$custom_registry_url"
yarn config set registry "$custom_registry_url"
Joe Haddad's avatar
Joe Haddad committed
97
98

# Login so we can publish packages
99
npx npm-cli-login@0.0.10 -u user -p password -e user@example.com -r "$custom_registry_url" --quotes
Joe Haddad's avatar
Joe Haddad committed
100

Dan Abramov's avatar
Dan Abramov committed
101
# Lint own code
102
./node_modules/.bin/eslint --max-warnings 0 packages/babel-preset-react-app/
103
./node_modules/.bin/eslint --max-warnings 0 packages/confusing-browser-globals/
104
105
106
107
./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/
108

109
110
cd packages/react-error-overlay/
./node_modules/.bin/eslint --max-warnings 0 src/
111
yarn test
112
113
114
115
if [ $APPVEYOR != 'True' ]; then
  # Flow started hanging on AppVeyor after we moved to Yarn Workspaces :-(
  yarn flow
fi
116
cd ../..
117

118
cd packages/react-dev-utils/
119
yarn test
120
cd ../..
121

122
123
124
125
cd packages/confusing-browser-globals/
yarn test
cd ../..

Dan Abramov's avatar
Dan Abramov committed
126
127
128
129
130
# ******************************************************************************
# 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
131
# Test local build command
132
yarn build
Max's avatar
Max committed
133
# Check for expected output
134
135
136
137
138
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
139

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

# Test local start command
146
yarn start --smoke-test
Christoph Pojer's avatar
Christoph Pojer committed
147

148
149
git clean -df
./tasks/publish.sh --yes --force-publish=* --skip-git --cd-version=prerelease --exact --npm-tag=latest
150

Dan Abramov's avatar
Dan Abramov committed
151
# ******************************************************************************
Joe Haddad's avatar
Joe Haddad committed
152
# Install react-scripts prerelease via create-react-app prerelease.
Dan Abramov's avatar
Dan Abramov committed
153
154
# ******************************************************************************

Christopher Chedeau's avatar
Christopher Chedeau committed
155
156
# Install the app in a temporary location
cd $temp_app_path
Joe Haddad's avatar
Joe Haddad committed
157
158
159
npx create-react-app test-app

# TODO: verify we installed prerelease
Dan Abramov's avatar
Dan Abramov committed
160
161
162
163
164
165

# ******************************************************************************
# 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.
# ******************************************************************************

166
167
168
169
170
171
172
173
174
175
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

176
  yarn build
177
178
179
180
181
  # 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

182
  PUBLIC_URL="/anabsolute" yarn build
183
184
185
186
187
188
  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

189
  yarn build
190
191
192
  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

193
  PUBLIC_URL="https://www.example.net/overridetest" yarn build
194
195
196
197
198
199
200
  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

201
  yarn build
202
203
204
  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

205
  PUBLIC_URL="https://www.example.net/overridetest" yarn build
206
207
208
209
210
211
212
213
214
  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
215
216
217
218
219
220
221
222
223
224
225
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
226
  yarn build; test $? -eq 1 || exit 1
Joe Haddad's avatar
Joe Haddad committed
227
228
  # TODO: check for error message

Dan Abramov's avatar
Dan Abramov committed
229
230
  rm sample.json

Joe Haddad's avatar
Joe Haddad committed
231
232
233
234
235
  # Restore App.js
  rm src/App.js
  mv src/App.js.bak src/App.js
}

Dan Abramov's avatar
Dan Abramov committed
236
# Enter the app directory
Christopher Chedeau's avatar
Christopher Chedeau committed
237
238
239
cd test-app

# Test the build
240
yarn build
241
# Check for expected output
242
243
244
245
246
exists build/*.html
exists build/static/js/*.js
exists build/static/css/*.css
exists build/static/media/*.svg
exists build/favicon.ico
247

248
# Run tests with CI flag
249
CI=true yarn test
Dan Abramov's avatar
Dan Abramov committed
250
# Uncomment when snapshot testing is enabled by default:
251
# exists src/__snapshots__/App.test.js.snap
Christoph Pojer's avatar
Christoph Pojer committed
252

Dan Abramov's avatar
Dan Abramov committed
253
# Test the server
254
yarn start --smoke-test
Dan Abramov's avatar
Dan Abramov committed
255

256
257
258
# Test environment handling
verify_env_url

Joe Haddad's avatar
Joe Haddad committed
259
260
261
# Test reliance on webpack internals
verify_module_scope

Dan Abramov's avatar
Dan Abramov committed
262
263
264
265
# ******************************************************************************
# Finally, let's check that everything still works after ejecting.
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
266
# Eject...
Christopher Chedeau's avatar
Christopher Chedeau committed
267
268
echo yes | npm run eject

Dan Abramov's avatar
Dan Abramov committed
269
# Test the build
270
yarn build
271
# Check for expected output
272
273
274
275
276
exists build/*.html
exists build/static/js/*.js
exists build/static/css/*.css
exists build/static/media/*.svg
exists build/favicon.ico
277

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

Dan Abramov's avatar
Dan Abramov committed
286
# Test the server
287
yarn start --smoke-test
Dan Abramov's avatar
Dan Abramov committed
288

289
290
# Test environment handling
verify_env_url
291

Joe Haddad's avatar
Joe Haddad committed
292
293
294
# Test reliance on webpack internals
verify_module_scope

Christopher Chedeau's avatar
Christopher Chedeau committed
295
# Cleanup
296
cleanup