4
votes

Running e2e tests against prod bundle with webpack-dev-server

In Angular one can run protractor tests against the prod build using

"e2e:prod": "ng e2e --prod" 

This will build the prod bundle, start the webpack-dev-server and run protractor. Once this is done, the webpack-dev-server will be shutdown. Anyhow this is not what we are looking for... see below

How to shutdown node server once protractor is finished ?

For our continous integration we want the e2e tests to run against a node server and not the webpack-dev-server. So we tried starting the node server and then running the protractor tests.

For this purpose we introduced a new script in our package.json that our ci server executes:

npm run e2e:ci

Solution 1 - use concurrently (not working):

"e2e:ci": "concurrently -k \"npm:e2e:ci:serve\" \"npm:e2e:ci:protractor\"",
"e2e:ci:serve": "node server.js",
"e2e:ci:protractor": "ng e2e --base-url=http://localhost:8080/ --dev-server-target="

This solution almost works. Having --dev-server-target= without target will stop angular to build the bundle (we are on angular 7). e2e:ci:serve starts the node server, assuming the prod bundle was build before so it can be served. BUT - once protractor is finished it will kill the node server so it exists with code 1:

--> Sending SIGTERM to other processes..
[serve:server] npm run serve:server exited with code 1
npm ERR! code ELIFECYCLE

Since I used concurrenlty with -k (kill) the node server stops once protractor is finished. But with exit code 1, so our ci assumes the e2e test failed.

Solution 2 - use npm-run-all (not working)

we also tried npm-run-all instead of concurrently with the race flag -r:

"e2e:ci": "npm-run-all -p -r e2e:ci:serve e2e:ci:protractor",

But this will leave the server running once protractor is finished. So the e2e test never stops.

Anybody has a clue how to do this correctly? Either shutdown the node server gracefully or maybe another approach to archieve running e2e tests against node server?

1

1 Answers

5
votes

Ok, we finally figured it out to make it run with npm-run-all -r, at least under linux. Our express server needed to listen to SIGTERM signal. Here all together:

package.json

"e2e:ci": "npm-run-all -p -r e2e:ci:serve e2e:ci:protractor",
"e2e:ci:serve": "node server.js",
"e2e:ci:protractor": "ng e2e --base-url=http://localhost:8080/ --dev-server-target="

server.js

var app = express();
var server = require('http').createServer(app);
server.listen(8080);

process.on('SIGTERM', function () {
    server.close(function () {
        process.exit(0);
    });
});