e2e-kitchensink.sh 5.12 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
# http://unix.stackexchange.com/a/84980
temp_app_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_app_path'`
Joe Haddad's avatar
Joe Haddad committed
18
temp_module_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_module_path'`
19
20
21
custom_registry_url=http://localhost:4873
original_npm_registry_url=`npm get registry`
original_yarn_registry_url=`yarn config get registry`
22

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

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

function handle_exit {
  cleanup
44
  echo 'Exiting without error.' 1>&2;
45
46
47
  exit
}

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

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

# Echo every command being executed
set -x

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

68
69
70
71
72
if hash npm 2>/dev/null
then
  npm i -g npm@latest
fi

73
# Bootstrap monorepo
74
yarn
75

Dan Abramov's avatar
Dan Abramov committed
76
# ******************************************************************************
77
# First, publish the monorepo.
Dan Abramov's avatar
Dan Abramov committed
78
79
# ******************************************************************************

80
81
# Start local registry
tmp_registry_log=`mktemp`
82
(cd && nohup npx verdaccio@3.8.2 -c "$root_path"/tasks/verdaccio.yaml &>$tmp_registry_log &)
83
84
# Wait for `verdaccio` to boot
grep -q 'http address' <(tail -f $tmp_registry_log)
85

86
# Set registry to local registry
87
88
npm set registry "$custom_registry_url"
yarn config set registry "$custom_registry_url"
Ville Immonen's avatar
Ville Immonen committed
89

90
# Login so we can publish packages
91
(cd && npx npm-auth-to-token@1.0.0 -u user -p password -e user@example.com -r "$custom_registry_url")
Dan Abramov's avatar
Dan Abramov committed
92

93
# Publish the monorepo
94
95
git clean -df
./tasks/publish.sh --yes --force-publish=* --skip-git --cd-version=prerelease --exact --npm-tag=latest
96

Dan Abramov's avatar
Dan Abramov committed
97
# ******************************************************************************
98
# Now that we have published them, create a clean app folder and install them.
Dan Abramov's avatar
Dan Abramov committed
99
100
# ******************************************************************************

Christopher Chedeau's avatar
Christopher Chedeau committed
101
102
# Install the app in a temporary location
cd $temp_app_path
103
npx create-react-app --internal-testing-template="$root_path"/packages/react-scripts/fixtures/kitchensink test-kitchensink
Dan Abramov's avatar
Dan Abramov committed
104

Joe Haddad's avatar
Joe Haddad committed
105
106
# Install the test module
cd "$temp_module_path"
107
yarn add test-integrity@^2.0.1
Joe Haddad's avatar
Joe Haddad committed
108

Dan Abramov's avatar
Dan Abramov committed
109
110
111
112
113
114
# ******************************************************************************
# 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
115
cd "$temp_app_path/test-kitchensink"
Christopher Chedeau's avatar
Christopher Chedeau committed
116

117
118
119
# In kitchensink, we want to test all transforms
export BROWSERSLIST='ie 9'

Joe Haddad's avatar
Joe Haddad committed
120
# Link to test module
121
npm link "$temp_module_path/node_modules/test-integrity"
Joe Haddad's avatar
Joe Haddad committed
122

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

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

# Unit tests
134
# https://facebook.github.io/jest/docs/en/troubleshooting.html#tests-are-extremely-slow-on-docker-and-or-continuous-integration-ci-server
135
136
137
REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true \
  NODE_PATH=src \
138
  NODE_ENV=test \
139
  yarn test --no-cache --runInBand --testPathPattern=src
140

Clement Hoang's avatar
Clement Hoang committed
141
# Prepare "development" environment
142
143
144
145
tmp_server_log=`mktemp`
PORT=3001 \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  NODE_PATH=src \
146
  nohup yarn start &>$tmp_server_log &
147
grep -q 'You can now view' <(tail -f $tmp_server_log)
Clement Hoang's avatar
Clement Hoang committed
148
149

# Test "development" environment
150
151
152
E2E_URL="http://localhost:3001" \
  REACT_APP_SHELL_ENV_MESSAGE=fromtheshell \
  CI=true NODE_PATH=src \
153
  NODE_ENV=development \
Clement Hoang's avatar
Clement Hoang committed
154
  BABEL_ENV=test \
155
  node_modules/.bin/jest --no-cache --runInBand --config='jest.integration.config.js'
156
157
158
159
# Test "production" environment
E2E_FILE=./build/index.html \
  CI=true \
  NODE_PATH=src \
160
  NODE_ENV=production \
Clement Hoang's avatar
Clement Hoang committed
161
  BABEL_ENV=test \
162
  PUBLIC_URL=http://www.example.org/spa/ \
163
  node_modules/.bin/jest --no-cache --runInBand --config='jest.integration.config.js'
164

Christopher Chedeau's avatar
Christopher Chedeau committed
165
# Cleanup
166
cleanup