e2e-kitchensink.sh 8.91 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
# This is an end-to-end kitchensink test intended to run on CI.
9
10
11
# 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
# CLI, app, and test module temporary locations
16
17
18
# 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'`
Joe Haddad's avatar
Joe Haddad committed
19
temp_module_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_module_path'`
20

21
22
function cleanup {
  echo 'Cleaning up.'
23
  ps -ef | grep 'react-scripts' | grep -v grep | awk '{print $2}' | xargs kill -9
24
  cd "$root_path"
Joe Haddad's avatar
Joe Haddad committed
25
  # TODO: fix "Device or resource busy" and remove ``|| $CI`
Joe Haddad's avatar
Joe Haddad committed
26
  rm -rf "$temp_cli_path" "$temp_app_path" "$temp_module_path" || $CI
27
28
}

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

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

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

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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/
  if [ "$USE_YARN" = "yes" ]
  then
    yarn install --production
  else
    npm install --only=production
  fi
  # 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 ../..
}

72
73
74
75
76
77
78
# Check for the existence of one or more files.
function exists {
  for f in $*; do
    test -e "$f"
  done
}

79
80
81
82
83
# 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
84
85
86
87

# Echo every command being executed
set -x

Dan Abramov's avatar
Dan Abramov committed
88
# Go to root
Christopher Chedeau's avatar
Christopher Chedeau committed
89
cd ..
Dan Abramov's avatar
Dan Abramov committed
90
root_path=$PWD
91

92
93
94
95
96
97
# Clear cache to avoid issues with incorrect packages being used
if hash yarnpkg 2>/dev/null
then
  # AppVeyor uses an old version of yarn.
  # Once updated to 0.24.3 or above, the workaround can be removed
  # and replaced with `yarnpkg cache clean`
98
  # Issues:
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  #    https://github.com/yarnpkg/yarn/issues/2591
  #    https://github.com/appveyor/ci/issues/1576
  #    https://github.com/facebookincubator/create-react-app/pull/2400
  # When removing workaround, you may run into
  #    https://github.com/facebookincubator/create-react-app/issues/2030
  case "$(uname -s)" in
    *CYGWIN*|MSYS*|MINGW*) yarn=yarn.cmd;;
    *) yarn=yarnpkg;;
  esac
  $yarn cache clean
fi

if hash npm 2>/dev/null
then
113
  # npm 5 is too buggy right now
Dan Abramov's avatar
Dan Abramov committed
114
  if [ $(npm -v | head -c 1) -eq 5 ]; then
115
    npm i -g npm@^4.x
Dan Abramov's avatar
Dan Abramov committed
116
  fi;
117
  npm cache clean || npm cache verify
118
119
fi

120
# Prevent bootstrap, we only want top-level dependencies
Joe Haddad's avatar
Joe Haddad committed
121
cp package.json package.json.bak
122
grep -v "postinstall" package.json > temp && mv temp package.json
123
npm install
Joe Haddad's avatar
Joe Haddad committed
124
mv package.json.bak package.json
125

Ville Immonen's avatar
Ville Immonen committed
126
127
128
if [ "$USE_YARN" = "yes" ]
then
  # Install Yarn so that the test can use it to install packages.
Dan Abramov's avatar
Dan Abramov committed
129
  npm install -g yarn
130
  yarn cache clean
Ville Immonen's avatar
Ville Immonen committed
131
132
fi

Joe Haddad's avatar
Joe Haddad committed
133
# We removed the postinstall, so do it manually
134
node bootstrap.js
Joe Haddad's avatar
Joe Haddad committed
135

136
137
138
139
cd packages/react-error-overlay/
npm run build:prod
cd ../..

Dan Abramov's avatar
Dan Abramov committed
140
# ******************************************************************************
141
# First, pack react-scripts and create-react-app so we can use them.
Dan Abramov's avatar
Dan Abramov committed
142
143
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
144
# Pack CLI
145
cd "$root_path"/packages/create-react-app
Christopher Chedeau's avatar
Christopher Chedeau committed
146
147
cli_path=$PWD/`npm pack`

Dan Abramov's avatar
Dan Abramov committed
148
# Go to react-scripts
149
cd "$root_path"/packages/react-scripts
Ville Immonen's avatar
Ville Immonen committed
150

151
152
153
154
155
# 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.
156
node "$root_path"/tasks/replace-own-deps.js
Ville Immonen's avatar
Ville Immonen committed
157

Dan Abramov's avatar
Dan Abramov committed
158
# Finally, pack react-scripts
159
scripts_path="$root_path"/packages/react-scripts/`npm pack`
Dan Abramov's avatar
Dan Abramov committed
160

161
162
163
164
# Restore package.json
rm package.json
mv package.json.orig package.json

Dan Abramov's avatar
Dan Abramov committed
165
166
167
168
# ******************************************************************************
# Now that we have packed them, create a clean app folder and install them.
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
169
# Install the CLI in a temporary location
170
171
cd "$temp_cli_path"
npm install "$cli_path"
Christopher Chedeau's avatar
Christopher Chedeau committed
172
173
174

# Install the app in a temporary location
cd $temp_app_path
175
create_react_app --scripts-version="$scripts_path" --internal-testing-template="$root_path"/packages/react-scripts/fixtures/kitchensink test-kitchensink
Dan Abramov's avatar
Dan Abramov committed
176

Joe Haddad's avatar
Joe Haddad committed
177
178
179
180
# Install the test module
cd "$temp_module_path"
npm install test-integrity@^2.0.1

Dan Abramov's avatar
Dan Abramov committed
181
182
183
184
185
186
# ******************************************************************************
# 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.
# ******************************************************************************

# Enter the app directory
Joe Haddad's avatar
Joe Haddad committed
187
cd "$temp_app_path/test-kitchensink"
Christopher Chedeau's avatar
Christopher Chedeau committed
188

189
# Link to our preset
190
install_package "$root_path"/packages/babel-preset-react-app
191
192
# Link to error overlay package because now it's a dependency
# of react-dev-utils and not react-scripts
193
install_package "$root_path"/packages/react-error-overlay
194

Joe Haddad's avatar
Joe Haddad committed
195
# Link to test module
196
install_package "$temp_module_path/node_modules/test-integrity"
Joe Haddad's avatar
Joe Haddad committed
197

Christopher Chedeau's avatar
Christopher Chedeau committed
198
# Test the build
199
200
201
202
203
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  NODE_PATH=src \
  PUBLIC_URL=http://www.example.org/spa/ \
  npm run build

204
# Check for expected output
205
206
exists build/*.html
exists build/static/js/main.*.js
207
208
209
210
211

# Unit tests
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true \
  NODE_PATH=src \
212
  NODE_ENV=test \
213
  npm test -- --no-cache --testPathPattern=src
214
215
216
217
218
219
220

# Test "development" environment
tmp_server_log=`mktemp`
PORT=3001 \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  NODE_PATH=src \
  nohup npm start &>$tmp_server_log &
221
grep -q 'You can now view' <(tail -f $tmp_server_log)
222
223
224
E2E_URL="http://localhost:3001" \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true NODE_PATH=src \
225
  NODE_ENV=development \
Joe Haddad's avatar
Joe Haddad committed
226
  node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
227
228
229
230
231

# Test "production" environment
E2E_FILE=./build/index.html \
  CI=true \
  NODE_PATH=src \
232
  NODE_ENV=production \
233
  PUBLIC_URL=http://www.example.org/spa/ \
Fabrizio Castellarin's avatar
Fabrizio Castellarin committed
234
  node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
Dan Abramov's avatar
Dan Abramov committed
235

Dan Abramov's avatar
Dan Abramov committed
236
237
238
239
# ******************************************************************************
# Finally, let's check that everything still works after ejecting.
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
240
# Eject...
Christopher Chedeau's avatar
Christopher Chedeau committed
241
242
echo yes | npm run eject

243
244
245
246
247
248
249
250
# Ensure Yarn is ran after eject; at the time of this commit, we don't run Yarn
# after ejecting. Soon, we may only skip Yarn on Windows. Let's try to remove
# this in the near future.
if hash yarnpkg 2>/dev/null
then
  yarn install --check-files
fi

Dan Abramov's avatar
Dan Abramov committed
251
# ...but still link to the local packages
252
253
254
255
install_package "$root_path"/packages/babel-preset-react-app
install_package "$root_path"/packages/eslint-config-react-app
install_package "$root_path"/packages/react-error-overlay
install_package "$root_path"/packages/react-dev-utils
Dan Abramov's avatar
Dan Abramov committed
256

Joe Haddad's avatar
Joe Haddad committed
257
# Link to test module
258
install_package "$temp_module_path/node_modules/test-integrity"
Joe Haddad's avatar
Joe Haddad committed
259

Dan Abramov's avatar
Dan Abramov committed
260
# Test the build
261
262
263
264
265
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  NODE_PATH=src \
  PUBLIC_URL=http://www.example.org/spa/ \
  npm run build

266
# Check for expected output
267
268
exists build/*.html
exists build/static/js/main.*.js
269
270
271
272
273

# Unit tests
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true \
  NODE_PATH=src \
274
  NODE_ENV=test \
275
  npm test -- --no-cache --testPathPattern=src
276
277
278
279
280
281
282

# Test "development" environment
tmp_server_log=`mktemp`
PORT=3002 \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  NODE_PATH=src \
  nohup npm start &>$tmp_server_log &
283
grep -q 'You can now view' <(tail -f $tmp_server_log)
284
285
286
E2E_URL="http://localhost:3002" \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true NODE_PATH=src \
287
  NODE_ENV=development \
Fabrizio Castellarin's avatar
Fabrizio Castellarin committed
288
  node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
289
290
291
292
293
294

# Test "production" environment
E2E_FILE=./build/index.html \
  CI=true \
  NODE_ENV=production \
  NODE_PATH=src \
295
  PUBLIC_URL=http://www.example.org/spa/ \
Fabrizio Castellarin's avatar
Fabrizio Castellarin committed
296
  node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
297

Christopher Chedeau's avatar
Christopher Chedeau committed
298
# Cleanup
299
cleanup