Skip to content

Commit

Permalink
feat(runner): Use node-cleanup for better cleanup. Use wait-on for mo…
Browse files Browse the repository at this point in the history
…re reliable test runs
  • Loading branch information
christopherthielen committed Apr 30, 2018
1 parent e678d0d commit ec04982
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 16 deletions.
62 changes: 53 additions & 9 deletions cypress-runner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const fs = require('fs');
const { resolve } = require('path');
const waitOn = require('wait-on');
const nodeCleanup = require('node-cleanup');

function findBinary(binaryName, path) {
const binary = resolve(path, 'node_modules', '.bin', binaryName);
Expand All @@ -21,21 +23,63 @@ function launchCypress(cypressCmd, path, port) {
process.exit();
}

let serve, cypress;

const cleanupServe = () => {
if (serve) {
console.log('Terminating serve...');
serve.kill();
}
serve = null;
};

const cleanupCypress = () => {
if (cypress) {
console.log('Terminating cypress...')
cypress.kill();
}
cypress = null;
};

const cleanup = () => {
cleanupServe();
cleanupCypress();
}

const { fork } = require('child_process');
const serveBinary = findBinary('serve', '.');
let serveArgs = [ '-n', '-s', '-p', port, path ];

const cypressBinary = findBinary('cypress', '.');
const browserSync = fork(serveBinary, [ '-n', '-s', '-p', port, path ]);
const cypress = fork(cypressBinary, [ cypressCmd ]);

process.on('SIGINT', function () {
browserSync.kill();
cypress.kill();
console.log(`Launching ${[serveBinary].concat(serveArgs).join(' ')}`);
serve = fork(serveBinary, serveArgs);

waitOn({ resources: [`http://localhost:${port}`], window: 500, timeout: 30000}, () => {
console.log(`detected http service ready on port ${port}`)
console.log(`Launching ${[cypressBinary].concat(cypressCmd).join(' ')}`);
cypress = fork(cypressBinary, [ cypressCmd ]);
cypress.on('exit', (code) => {
console.log(`Cypress completed with exit code ${code}`);
cypress = null;
cleanupServe();
process.exit(code)
});
});

nodeCleanup((exitCode, signal) => {
if (signal) {
console.log(`cypress-runner exiting via signal ${signal}`);
} else if (exitCode) {
console.log(`cypress-runner exiting with exit code ${exitCode}`);
}
cleanup();
});

browserSync.on('exit', () => cypress.kill());
cypress.on('exit', (code) => {
browserSync.kill();
process.exit(code)
serve.on('exit', (code) => {
console.log(`Serve completed with exit code ${code}`);
serve = null;
cleanupCypress();
});
}

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
},
"dependencies": {
"cypress": "^2.1.0",
"node-cleanup": "^2.1.2",
"serve": "^6.5.5",
"wait-on": "^2.1.0",
"yargs": "^11.0.0"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit ec04982

Please sign in to comment.