|
| 1 | +module.exports = (api, options) => { |
| 2 | + const chalk = require('chalk') |
| 3 | + |
| 4 | + function run (command, args, rawArgs) { |
| 5 | + if (args.url) { |
| 6 | + const i = rawArgs.findIndex(arg => /^--url/.test(arg)) |
| 7 | + rawArgs.splice(i, /^--url=/.test(rawArgs[i]) ? 1 : 2) |
| 8 | + } |
| 9 | + |
| 10 | + const serverPromise = args.url |
| 11 | + ? Promise.resolve({ url: args.url }) |
| 12 | + : api.service.run('serve', { mode: 'production' }) |
| 13 | + |
| 14 | + return serverPromise.then(({ url, server }) => { |
| 15 | + const { info } = require('@vue/cli-shared-utils') |
| 16 | + info(`Starting e2e tests...`) |
| 17 | + |
| 18 | + const cyArgs = [ |
| 19 | + command, // open or run |
| 20 | + '--env', `VUE_DEV_SERVER_URL=${url}`, |
| 21 | + ...rawArgs |
| 22 | + ] |
| 23 | + |
| 24 | + const execa = require('execa') |
| 25 | + const cypressBinPath = require.resolve('cypress/bin/cypress') |
| 26 | + const runner = execa(cypressBinPath, cyArgs, { stdio: 'inherit' }) |
| 27 | + if (server) { |
| 28 | + runner.on('exit', () => server.close()) |
| 29 | + runner.on('error', () => server.close()) |
| 30 | + } |
| 31 | + return runner |
| 32 | + }) |
| 33 | + } |
| 34 | + |
| 35 | + api.registerCommand('e2e', { |
| 36 | + description: 'run e2e tests headlessly with `cypress run`', |
| 37 | + usage: 'vue-cli-service e2e [options]', |
| 38 | + options: { |
| 39 | + '--url': 'run e2e tests against given url instead of auto-starting dev server', |
| 40 | + '-s, --spec': 'runs a specific spec file. defaults to "all"' |
| 41 | + }, |
| 42 | + details: |
| 43 | + `All Cypress CLI options are also supported:\n` + |
| 44 | + chalk.yellow(`https://docs.cypress.io/guides/guides/command-line.html#cypress-run`) |
| 45 | + }, (args, rawArgs) => run('run', args, rawArgs)) |
| 46 | + |
| 47 | + api.registerCommand('e2e:open', { |
| 48 | + description: 'run e2e tests in interactive mode with `cypress open`', |
| 49 | + usage: 'vue-cli-service e2e:open [options]', |
| 50 | + options: { |
| 51 | + '--url': 'run e2e tests against given url instead of auto-starting dev server' |
| 52 | + }, |
| 53 | + details: |
| 54 | + `All Cypress CLI options are supported:\n` + |
| 55 | + chalk.yellow(`https://docs.cypress.io/guides/guides/command-line.html#cypress-open`) |
| 56 | + }, (args, rawArgs) => run('open', args, rawArgs)) |
| 57 | +} |
0 commit comments