e2e-kitchensink.sh 6.94 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.'
Joe Haddad's avatar
Joe Haddad committed
25
  ps -ef | grep 'react-scripts' | grep -v grep | awk '{print $2}' | xargs kill -s 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
npm install

Ville Immonen's avatar
Ville Immonen committed
71
72
73
if [ "$USE_YARN" = "yes" ]
then
  # Install Yarn so that the test can use it to install packages.
Dan Abramov's avatar
Dan Abramov committed
74
  npm install -g yarn
75
  yarn cache clean
Ville Immonen's avatar
Ville Immonen committed
76
77
fi

Dan Abramov's avatar
Dan Abramov committed
78
# ******************************************************************************
79
# First, pack react-scripts and create-react-app so we can use them.
Dan Abramov's avatar
Dan Abramov committed
80
81
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
82
# Pack CLI
83
cd "$root_path"/packages/create-react-app
Christopher Chedeau's avatar
Christopher Chedeau committed
84
85
cli_path=$PWD/`npm pack`

Dan Abramov's avatar
Dan Abramov committed
86
# Go to react-scripts
87
cd "$root_path"/packages/react-scripts
Ville Immonen's avatar
Ville Immonen committed
88

89
90
91
92
93
# 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.
94
node "$root_path"/tasks/replace-own-deps.js
Ville Immonen's avatar
Ville Immonen committed
95

Dan Abramov's avatar
Dan Abramov committed
96
# Finally, pack react-scripts
97
scripts_path="$root_path"/packages/react-scripts/`npm pack`
Dan Abramov's avatar
Dan Abramov committed
98

99
100
101
102
# Restore package.json
rm package.json
mv package.json.orig package.json

Dan Abramov's avatar
Dan Abramov committed
103
104
105
106
# ******************************************************************************
# Now that we have packed them, create a clean app folder and install them.
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
107
# Install the CLI in a temporary location
108
109
cd "$temp_cli_path"
npm install "$cli_path"
Christopher Chedeau's avatar
Christopher Chedeau committed
110
111
112

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

Joe Haddad's avatar
Joe Haddad committed
115
116
117
118
# Install the test module
cd "$temp_module_path"
npm install test-integrity@^2.0.1

Dan Abramov's avatar
Dan Abramov committed
119
120
121
122
123
124
# ******************************************************************************
# 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
125
cd "$temp_app_path/test-kitchensink"
Christopher Chedeau's avatar
Christopher Chedeau committed
126

127
# Link to our preset
128
npm link "$root_path"/packages/babel-preset-react-app
129

Joe Haddad's avatar
Joe Haddad committed
130
131
132
# Link to test module
npm link "$temp_module_path/node_modules/test-integrity"

Christopher Chedeau's avatar
Christopher Chedeau committed
133
# Test the build
134
135
136
137
138
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  NODE_PATH=src \
  PUBLIC_URL=http://www.example.org/spa/ \
  npm run build

139
# Check for expected output
140
141
exists build/*.html
exists build/static/js/main.*.js
142
143
144
145
146

# Unit tests
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true \
  NODE_PATH=src \
147
  NODE_ENV=test \
148
149
150
151
152
153
154
155
  npm test -- --no-cache --testPathPattern="/src/"

# 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
156
157
158
159
160
161
162
163
while true
do
  if grep -q 'The app is running at:' $tmp_server_log; then
    break
  else
    sleep 1
  fi
done
164
165
166
E2E_URL="http://localhost:3001" \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true NODE_PATH=src \
167
  NODE_ENV=development \
Joe Haddad's avatar
Joe Haddad committed
168
  node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
169
170
171
172
173

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

Dan Abramov's avatar
Dan Abramov committed
178
179
180
181
# ******************************************************************************
# Finally, let's check that everything still works after ejecting.
# ******************************************************************************

182
# Unlink our preset
183
npm unlink "$root_path"/packages/babel-preset-react-app
184

Dan Abramov's avatar
Dan Abramov committed
185
# Eject...
Christopher Chedeau's avatar
Christopher Chedeau committed
186
187
echo yes | npm run eject

Dan Abramov's avatar
Dan Abramov committed
188
# ...but still link to the local packages
189
190
191
192
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
193

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

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

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

# Unit tests
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true \
  NODE_PATH=src \
211
  NODE_ENV=test \
Joe Haddad's avatar
Joe Haddad committed
212
  npm test -- --no-cache --testPathPattern='/src/'
213
214
215
216
217
218
219

# 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
220
221
222
223
224
225
226
227
while true
do
  if grep -q 'The app is running at:' $tmp_server_log; then
    break
  else
    sleep 1
  fi
done
228
229
230
E2E_URL="http://localhost:3002" \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true NODE_PATH=src \
231
  NODE_ENV=development \
Fabrizio Castellarin's avatar
Fabrizio Castellarin committed
232
  node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
233
234
235
236
237
238

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

Christopher Chedeau's avatar
Christopher Chedeau committed
242
# Cleanup
243
cleanup