How to start Aurelia site before Cypress testing?

@shahabganji
Thanks works great :smiley:

But I think you should get the port from aurelia.json.

npm i ps-node -D

npm i find-process -D
 "scripts": {
    "e2e": "au cypress --run --start"
}
// @ts-ignore
import * as cypress from 'cypress';
import * as config from '../../cypress.config';
import { CLIOptions } from 'aurelia-cli';
// @ts-ignore
import * as cfg from '../aurelia.json';

let find = require('find-process');
let 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', cfg.platform.port)
          .then(function (list) {
            if (!list.length) {
              console.log(`port ${cfg.platform.port} is free now`);
            } else {
              console.log(`%s is listening port ${cfg.platform.port}`, JSON.stringify(list[0]));
              var isWin = process.platform === "win32";
              // kill the process
              if (isWin) {
                ps.kill(list[0].pid, { signal: 'SIGHUP' }, () => { });
                process.exit();
              }
              else {
                ps.kill(list[0].pid, () => { });
              }
            }
          })

      }
      )
      .catch(cb);
  } else {
    cypress.open(config);
  }
};
2 Likes

There is also an approach way simpler :sweat_smile:

process.exit(0);
2 Likes

This might be the answer to your question process.pid or process.ppid

1 Like

This probably too late, but I just found out that npm-run-all package has a run-p bin that accepts a --race arguments. It will start all npm targets that follow and kill them all whenever the first one finishes.

So in this case, if I have these npm scripts:

"start": "au run",
"e2e": "cypress run",

then my test script is "test": "run-p --race start e2e".

What that does is starting the web server AND cypress at the same time. But don’t worry, cypress.visit will wait for your web server to load, so you should be fine unless it takes FOREVER for your web server to start (in which case you can increase cypress load timeout or add a cy.wait(5000) before cy.visit() to start cypress 5 seconds later for example). Then, your web server is supposed to stay there forever, but when cypress run finishes and exits, run-p --race will kill the rest of the targets (which is your web server in this case).

2 Likes

aurelia-cli v1.1 has a new feature in protractor and cypress command, thanks for @shahabganji.

au protractor --start
au cypress --start --run

The --start will start dev server before protractor/cypress e2e tests, make sure you are NOT running dev server manually. Furthermore, you can change host/port, au protractor --start --host 127.0.0.1 --port 8888 (cypress is same). BTW, it automatically shuts down dev server after e2e tests.

6 Likes