e2e-kitchensink.sh 6.29 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")"

17
18
19
20
21
# CLI and app temporary locations
# 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'`

22
23
function cleanup {
  echo 'Cleaning up.'
Dan Abramov's avatar
Dan Abramov committed
24
  cd $root_path
Dan Abramov's avatar
Dan Abramov committed
25
  rm -rf $temp_cli_path $temp_app_path
26
27
}

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

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

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

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
npm install

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

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

Dan Abramov's avatar
Dan Abramov committed
79
# Pack CLI
Dan Abramov's avatar
Dan Abramov committed
80
cd $root_path/packages/create-react-app
Christopher Chedeau's avatar
Christopher Chedeau committed
81
82
cli_path=$PWD/`npm pack`

Dan Abramov's avatar
Dan Abramov committed
83
84
# Go to react-scripts
cd $root_path/packages/react-scripts
Ville Immonen's avatar
Ville Immonen committed
85

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

Dan Abramov's avatar
Dan Abramov committed
93
# Finally, pack react-scripts
Dan Abramov's avatar
Dan Abramov committed
94
scripts_path=$root_path/packages/react-scripts/`npm pack`
Dan Abramov's avatar
Dan Abramov committed
95

96
97
98
99
# Restore package.json
rm package.json
mv package.json.orig package.json

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

Dan Abramov's avatar
Dan Abramov committed
104
# Install the CLI in a temporary location
Christopher Chedeau's avatar
Christopher Chedeau committed
105
106
107
108
109
cd $temp_cli_path
npm install $cli_path

# Install the app in a temporary location
cd $temp_app_path
110
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
111
112
113
114
115
116
117

# ******************************************************************************
# 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
118
cd test-kitchensink
Christopher Chedeau's avatar
Christopher Chedeau committed
119

120
121
122
# Link to our preset
npm link $root_path/packages/babel-preset-react-app

Christopher Chedeau's avatar
Christopher Chedeau committed
123
# Test the build
124
125
126
127
128
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  NODE_PATH=src \
  PUBLIC_URL=http://www.example.org/spa/ \
  npm run build

129
# Check for expected output
130
131
exists build/*.html
exists build/static/js/main.*.js
132
133
134
135
136

# Unit tests
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true \
  NODE_PATH=src \
137
  NODE_ENV=test \
138
139
140
141
142
143
144
145
146
147
148
149
  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 &
grep -q 'The app is running at:' <(tail -f $tmp_server_log)
E2E_URL="http://localhost:3001" \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true NODE_PATH=src \
150
  NODE_ENV=development \
151
152
153
154
155
156
  node node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js

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

Dan Abramov's avatar
Dan Abramov committed
161
162
163
164
# ******************************************************************************
# Finally, let's check that everything still works after ejecting.
# ******************************************************************************

165
166
167
# Unlink our preset
npm unlink $root_path/packages/babel-preset-react-app

Dan Abramov's avatar
Dan Abramov committed
168
# Eject...
Christopher Chedeau's avatar
Christopher Chedeau committed
169
170
echo yes | npm run eject

Dan Abramov's avatar
Dan Abramov committed
171
172
173
174
175
176
# ...but still link to the local packages
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
177
# Test the build
178
179
180
181
182
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  NODE_PATH=src \
  PUBLIC_URL=http://www.example.org/spa/ \
  npm run build

183
# Check for expected output
184
185
exists build/*.html
exists build/static/js/main.*.js
186
187
188
189
190

# Unit tests
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true \
  NODE_PATH=src \
191
  NODE_ENV=test \
192
193
194
195
196
197
198
199
200
201
202
203
  npm test -- --no-cache --testPathPattern="/src/"

# Test "development" environment
tmp_server_log=`mktemp`
PORT=3002 \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  NODE_PATH=src \
  nohup npm start &>$tmp_server_log &
grep -q 'The app is running at:' <(tail -f $tmp_server_log)
E2E_URL="http://localhost:3002" \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true NODE_PATH=src \
204
  NODE_ENV=development \
Fabrizio Castellarin's avatar
Fabrizio Castellarin committed
205
  node_modules/.bin/mocha --require babel-register --require babel-polyfill integration/*.test.js
206
207
208
209
210
211

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

Christopher Chedeau's avatar
Christopher Chedeau committed
215
# Cleanup
216
cleanup