e2e-kitchensink.sh 8.1 KB
Newer Older
1
#!/bin/bash
Christopher Chedeau's avatar
Christopher Chedeau committed
2
3
4
5
6
7
8
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.

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

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

Joe Haddad's avatar
Joe Haddad committed
17
# CLI, app, and test module temporary locations
18
19
20
# 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
21
temp_module_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_module_path'`
22

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

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

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

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

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

56
57
58
59
60
# 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
61
62
63
64

# Echo every command being executed
set -x

Dan Abramov's avatar
Dan Abramov committed
65
# Go to root
Christopher Chedeau's avatar
Christopher Chedeau committed
66
cd ..
Dan Abramov's avatar
Dan Abramov committed
67
root_path=$PWD
68

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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`
  # Issues: 
  #    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
Dan Abramov's avatar
Dan Abramov committed
90
91
92
93
94
  # npm 5 is too buggy right now
  if [ $(npm -v | head -c 1) -eq 5 ]; then
    npm i -g npm@^4.x
  fi;
  npm cache clean || npm cache verify
95
96
fi

Joe Haddad's avatar
Joe Haddad committed
97
98
99
# Prevent lerna bootstrap, we only want top-level dependencies
cp package.json package.json.bak
grep -v "lerna bootstrap" package.json > temp && mv temp package.json
100
npm install
Joe Haddad's avatar
Joe Haddad committed
101
mv package.json.bak package.json
102

Ville Immonen's avatar
Ville Immonen committed
103
104
105
if [ "$USE_YARN" = "yes" ]
then
  # Install Yarn so that the test can use it to install packages.
Dan Abramov's avatar
Dan Abramov committed
106
  npm install -g yarn
107
  yarn cache clean
Ville Immonen's avatar
Ville Immonen committed
108
109
fi

Joe Haddad's avatar
Joe Haddad committed
110
111
112
# We removed the postinstall, so do it manually
./node_modules/.bin/lerna bootstrap --concurrency=1

113
114
115
116
cd packages/react-error-overlay/
npm run build:prod
cd ../..

Dan Abramov's avatar
Dan Abramov committed
117
# ******************************************************************************
118
# First, pack react-scripts and create-react-app so we can use them.
Dan Abramov's avatar
Dan Abramov committed
119
120
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
121
# Pack CLI
122
cd "$root_path"/packages/create-react-app
Christopher Chedeau's avatar
Christopher Chedeau committed
123
124
cli_path=$PWD/`npm pack`

Dan Abramov's avatar
Dan Abramov committed
125
# Go to react-scripts
126
cd "$root_path"/packages/react-scripts
Ville Immonen's avatar
Ville Immonen committed
127

128
129
130
131
132
# 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.
133
node "$root_path"/tasks/replace-own-deps.js
Ville Immonen's avatar
Ville Immonen committed
134

Dan Abramov's avatar
Dan Abramov committed
135
# Finally, pack react-scripts
136
scripts_path="$root_path"/packages/react-scripts/`npm pack`
Dan Abramov's avatar
Dan Abramov committed
137

138
139
140
141
# Restore package.json
rm package.json
mv package.json.orig package.json

Dan Abramov's avatar
Dan Abramov committed
142
143
144
145
# ******************************************************************************
# Now that we have packed them, create a clean app folder and install them.
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
146
# Install the CLI in a temporary location
147
148
cd "$temp_cli_path"
npm install "$cli_path"
Christopher Chedeau's avatar
Christopher Chedeau committed
149
150
151

# Install the app in a temporary location
cd $temp_app_path
152
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
153

Joe Haddad's avatar
Joe Haddad committed
154
155
156
157
# Install the test module
cd "$temp_module_path"
npm install test-integrity@^2.0.1

Dan Abramov's avatar
Dan Abramov committed
158
159
160
161
162
163
# ******************************************************************************
# 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
164
cd "$temp_app_path/test-kitchensink"
Christopher Chedeau's avatar
Christopher Chedeau committed
165

166
# Link to our preset
167
npm link "$root_path"/packages/babel-preset-react-app
168

Joe Haddad's avatar
Joe Haddad committed
169
170
171
# Link to test module
npm link "$temp_module_path/node_modules/test-integrity"

Christopher Chedeau's avatar
Christopher Chedeau committed
172
# Test the build
173
174
175
176
177
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  NODE_PATH=src \
  PUBLIC_URL=http://www.example.org/spa/ \
  npm run build

178
# Check for expected output
179
180
exists build/*.html
exists build/static/js/main.*.js
181
182
183
184
185

# Unit tests
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true \
  NODE_PATH=src \
186
  NODE_ENV=test \
187
  npm test -- --no-cache --testPathPattern=src
188
189
190
191
192
193
194

# Test "development" environment
tmp_server_log=`mktemp`
PORT=3001 \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  NODE_PATH=src \
  nohup npm start &>$tmp_server_log &
Joe Haddad's avatar
Joe Haddad committed
195
196
while true
do
Joe Haddad's avatar
Fix CI    
Joe Haddad committed
197
  if grep -q 'You can now view' $tmp_server_log; then
Joe Haddad's avatar
Joe Haddad committed
198
199
200
201
202
    break
  else
    sleep 1
  fi
done
203
204
205
E2E_URL="http://localhost:3001" \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true NODE_PATH=src \
206
  NODE_ENV=development \
Joe Haddad's avatar
Joe Haddad committed
207
  node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
208
209
210
211
212

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

Dan Abramov's avatar
Dan Abramov committed
217
218
219
220
# ******************************************************************************
# Finally, let's check that everything still works after ejecting.
# ******************************************************************************

221
# Unlink our preset
222
npm unlink "$root_path"/packages/babel-preset-react-app
223

Dan Abramov's avatar
Dan Abramov committed
224
# Eject...
Christopher Chedeau's avatar
Christopher Chedeau committed
225
226
echo yes | npm run eject

Dan Abramov's avatar
Dan Abramov committed
227
# ...but still link to the local packages
228
229
230
231
npm link "$root_path"/packages/babel-preset-react-app
npm link "$root_path"/packages/eslint-config-react-app
npm link "$root_path"/packages/react-dev-utils
npm link "$root_path"/packages/react-scripts
Dan Abramov's avatar
Dan Abramov committed
232

Joe Haddad's avatar
Joe Haddad committed
233
234
235
# Link to test module
npm link "$temp_module_path/node_modules/test-integrity"

Dan Abramov's avatar
Dan Abramov committed
236
# Test the build
237
238
239
240
241
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  NODE_PATH=src \
  PUBLIC_URL=http://www.example.org/spa/ \
  npm run build

242
# Check for expected output
243
244
exists build/*.html
exists build/static/js/main.*.js
245
246
247
248
249

# Unit tests
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true \
  NODE_PATH=src \
250
  NODE_ENV=test \
251
  npm test -- --no-cache --testPathPattern=src
252
253
254
255
256
257
258

# Test "development" environment
tmp_server_log=`mktemp`
PORT=3002 \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  NODE_PATH=src \
  nohup npm start &>$tmp_server_log &
Joe Haddad's avatar
Joe Haddad committed
259
260
while true
do
Joe Haddad's avatar
Fix CI    
Joe Haddad committed
261
  if grep -q 'You can now view' $tmp_server_log; then
Joe Haddad's avatar
Joe Haddad committed
262
263
264
265
266
    break
  else
    sleep 1
  fi
done
267
268
269
E2E_URL="http://localhost:3002" \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true NODE_PATH=src \
270
  NODE_ENV=development \
Fabrizio Castellarin's avatar
Fabrizio Castellarin committed
271
  node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
272
273
274
275
276
277

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

Christopher Chedeau's avatar
Christopher Chedeau committed
281
# Cleanup
282
cleanup