e2e-simple.sh 8.94 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
18
# http://unix.stackexchange.com/a/84980
temp_app_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_app_path'`

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

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

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

41
42
43
44
45
46
47
# Check for the existence of one or more files.
function exists {
  for f in $*; do
    test -e "$f"
  done
}

48
49
50
51
52
# 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
53
54
55
56

# Echo every command being executed
set -x

Dan Abramov's avatar
Dan Abramov committed
57
# Go to root
Christopher Chedeau's avatar
Christopher Chedeau committed
58
cd ..
Dan Abramov's avatar
Dan Abramov committed
59
root_path=$PWD
60

61
62
63
64
65
66
67
68
69
70
71
72
73
# 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
74
75
76
77
78
79
if hash npm 2>/dev/null
then
  npm i -g npm@latest
  npm cache clean || npm cache verify
fi

80
# Prevent bootstrap, we only want top-level dependencies
81
cp package.json package.json.bak
82
grep -v "postinstall" package.json > temp && mv temp package.json
83
yarn
84
85
mv package.json.bak package.json

Joe Haddad's avatar
Joe Haddad committed
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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
npm set registry http://localhost:4873
yarn config set registry http://localhost:4873

# Login so we can publish packages
npx npm-cli-login@0.0.10 -u user -p password -e user@example.com -r http://localhost:4873 --quotes

99
100
101
# We removed the postinstall, so do it manually here
node bootstrap.js

Dan Abramov's avatar
Dan Abramov committed
102
# Lint own code
103
104
105
106
107
108
109
./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/
110
111
yarn test
yarn build:prod
112
cd ../..
113
cd packages/react-dev-utils/
114
yarn test
115
cd ../..
116

Dan Abramov's avatar
Dan Abramov committed
117
118
119
120
121
# ******************************************************************************
# 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
122
# Test local build command
123
yarn build
Max's avatar
Max committed
124
# Check for expected output
125
126
127
128
129
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
130

131
# Run tests with CI flag
132
CI=true yarn test
Dan Abramov's avatar
Dan Abramov committed
133
# Uncomment when snapshot testing is enabled by default:
134
# exists template/src/__snapshots__/App.test.js.snap
Dan Abramov's avatar
Dan Abramov committed
135
136

# Test local start command
137
yarn start --smoke-test
Christoph Pojer's avatar
Christoph Pojer committed
138

Joe Haddad's avatar
Joe Haddad committed
139
140
git clean -f
./tasks/release.sh --yes --force-publish=* --skip-git --cd-version=prerelease --exact --npm-tag=latest
141

Dan Abramov's avatar
Dan Abramov committed
142
# ******************************************************************************
Joe Haddad's avatar
Joe Haddad committed
143
# Install react-scripts prerelease via create-react-app prerelease.
Dan Abramov's avatar
Dan Abramov committed
144
145
# ******************************************************************************

Christopher Chedeau's avatar
Christopher Chedeau committed
146
147
# Install the app in a temporary location
cd $temp_app_path
Joe Haddad's avatar
Joe Haddad committed
148
149
150
npx create-react-app test-app

# TODO: verify we installed prerelease
Dan Abramov's avatar
Dan Abramov committed
151
152
153
154
155
156

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

157
158
159
160
161
162
163
164
165
166
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

167
  yarn build
168
169
170
171
172
  # 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

173
  PUBLIC_URL="/anabsolute" yarn build
174
175
176
177
178
179
  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

180
  yarn build
181
182
183
  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

184
  PUBLIC_URL="https://www.example.net/overridetest" yarn build
185
186
187
188
189
190
191
  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

192
  yarn build
193
194
195
  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

196
  PUBLIC_URL="https://www.example.net/overridetest" yarn build
197
198
199
200
201
202
203
204
205
  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
206
207
208
209
210
211
212
213
214
215
216
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
217
  yarn build; test $? -eq 1 || exit 1
Joe Haddad's avatar
Joe Haddad committed
218
219
220
221
222
223
224
  # 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
225
# Enter the app directory
Christopher Chedeau's avatar
Christopher Chedeau committed
226
227
228
cd test-app

# Test the build
229
yarn build
230
# Check for expected output
231
232
233
234
235
exists build/*.html
exists build/static/js/*.js
exists build/static/css/*.css
exists build/static/media/*.svg
exists build/favicon.ico
236

237
# Run tests with CI flag
238
CI=true yarn test
Dan Abramov's avatar
Dan Abramov committed
239
# Uncomment when snapshot testing is enabled by default:
240
# exists src/__snapshots__/App.test.js.snap
Christoph Pojer's avatar
Christoph Pojer committed
241

Dan Abramov's avatar
Dan Abramov committed
242
# Test the server
243
yarn start --smoke-test
Dan Abramov's avatar
Dan Abramov committed
244

245
246
247
# Test environment handling
verify_env_url

Joe Haddad's avatar
Joe Haddad committed
248
249
250
# Test reliance on webpack internals
verify_module_scope

Dan Abramov's avatar
Dan Abramov committed
251
252
253
254
# ******************************************************************************
# Finally, let's check that everything still works after ejecting.
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
255
# Eject...
Christopher Chedeau's avatar
Christopher Chedeau committed
256
257
echo yes | npm run eject

Dan Abramov's avatar
Dan Abramov committed
258
# Test the build
259
yarn build
260
# Check for expected output
261
262
263
264
265
exists build/*.html
exists build/static/js/*.js
exists build/static/css/*.css
exists build/static/media/*.svg
exists build/favicon.ico
266

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

Dan Abramov's avatar
Dan Abramov committed
275
# Test the server
276
yarn start --smoke-test
Dan Abramov's avatar
Dan Abramov committed
277

278
279
# Test environment handling
verify_env_url
280

Joe Haddad's avatar
Joe Haddad committed
281
282
283
# Test reliance on webpack internals
verify_module_scope

Christopher Chedeau's avatar
Christopher Chedeau committed
284
# Cleanup
285
cleanup