e2e-simple.sh 9.22 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
if hash npm 2>/dev/null
then
  npm i -g npm@latest
fi

84
# Bootstrap monorepo
85
yarn
86

Joe Haddad's avatar
Joe Haddad committed
87
88
# Start local registry
tmp_registry_log=`mktemp`
89
(cd && nohup npx verdaccio@3.8.2 -c "$root_path"/tasks/verdaccio.yaml &>$tmp_registry_log &)
Joe Haddad's avatar
Joe Haddad committed
90
91
92
93
# Wait for `verdaccio` to boot
grep -q 'http address' <(tail -f $tmp_registry_log)

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

# Login so we can publish packages
98
(cd && npx npm-auth-to-token@1.0.0 -u user -p password -e user@example.com -r "$custom_registry_url")
Joe Haddad's avatar
Joe Haddad committed
99

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

255
256
257
# Test environment handling
verify_env_url

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

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

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

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

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

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

288
289
# Test environment handling
verify_env_url
290

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

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