We had this issue with one of our projects. We ended up using gulp to orchestrate the running of the app as well as running the Cypress tests. That way, we are able to tear down the app after the Cypress tests.
Let me see if I’m able to post some snippets for you.
We added your snippet code as au command in this repository.
The command to use is au cy.
The project is based on the great @huochunpeng plugin system in here.
But we have a problem, seems the site does not start well on port 8080.
I have found the difference as to why it worked with our project.
server = spawn(process.argv[0], ["server.js"], {
detached: true,
stdio: "ignore"
});
The snippet above is what we have within the startServer method. It turns out process.argv[0] is node and in turn that runs server.js serving our compiled application.
I tried replacing the params to "npm.cmd", ["start"]...and that seems to have done it, but it wasn’t able to teardown the server after the test has run.
There were multiple ways which the project could be ran, one uses PORT and the other on 8080, I was checking with both and to o avail. anyway, thanks for mentioning
spawn should include a bunch of properties. You might be able get the process id there, the issue however is that using npm.cmd spawns other processes… those you won’t be able to get the process id.
I will have a look at our server.js file when I get time and get back to you.
As for running Cypress headless, you can look inside gulp-cypress.ts and replace the browser with electron. Currently, that’s the only way to run a headless browser.
au cypress or au cypress --run, alteratively I could do au cypress --start if I want to start the project before I ran the tests, or au cypress --run --start if I want to run cypress in non-interactive mode(headless I guess) and start the project before that.
I’d imagine that run.default() should return a process handle, which will then be used with cross-platform killtree inside cypress.run().then(…) before cb().
I think @khuongduybui is right, however, I don’t know how to expose that process, since I just imported the run method used in au run command and that does not return any process handler since there is no need in that scenario, the web server should be running on till we say not so. That being said, I came up with a workaround, based on the port of the application( e.g. 8080 in this case ) I found the process(pid) and killed it
I used find-process npm package to find the process information of the application running on port 8080, and used ps-node npm package to kill it.
so the cypress.ts file would look like the following:
// @ts-ignore
import * as cypress from 'cypress';
import * as config from '../../cypress.config';
import { CLIOptions } from 'aurelia-cli';
var find = require('find-process');
var ps = require( 'ps-node' );
import * as run from './run';
export default (cb) => {
let hasRunProject = process.argv.find(x => x === "--start") || false;
if (hasRunProject) {
run.default();
}
if (CLIOptions.hasFlag('run')) {
cypress
.run(config)
.then(results => {
(results.totalFailed === 0 ? cb() : cb('Run failed!'));
find('port', 8080)
.then(function (list) {
if (!list.length) {
console.log('port 8080 is free now');
} else {
console.log('%s is listening port 8080', JSON.stringify(list[0]));
// kill the process
ps.kill(list[0].pid , () => {});
}
})
}
)
.catch(cb);
} else {
cypress.open(config);
}
};
and you can jus run the following command: au cypress --run --start
the --start is sth I invented to run the real application under the test.
Hope this helps.