Files
vue-cli/packages/@vue/cli-plugin-e2e-nightwatch/index.js
T
Evan You 69ebd800a2 refactor: rename test commands
BREAKING CHANGE: `cli-plugin-unit-jest` and `cli-plugin-unit-mocha` now register
"test:unit" command and script instead of "test"; `cli-plugin-e2e-cypress` now
register "test:e2e" with optional `--headless` flag instead of "e2e" and
"e2e:open"; `cli-plugin-e2e-nightwatch` now register "test:e2e" instead of "e2e".

close #876, close #878
2018-05-01 18:15:25 -04:00

75 lines
2.5 KiB
JavaScript

function removeArg (rawArgs, arg) {
const matchRE = new RegExp(`^--${arg}`)
const equalRE = new RegExp(`^--${arg}=`)
const i = rawArgs.findIndex(arg => matchRE.test(arg))
if (i > -1) {
rawArgs.splice(i, equalRE.test(rawArgs[i]) ? 1 : 2)
}
}
module.exports = (api, options) => {
api.registerCommand('test:e2e', {
description: 'run e2e tests with nightwatch',
usage: 'vue-cli-service test:e2e [options]',
options: {
'--url': 'run e2e tests against given url instead of auto-starting dev server',
'--config': 'use custom nightwatch config file (overrides internals)',
'-e, --env': 'specify comma-delimited browser envs to run in (default: chrome)',
'-t, --test': 'sepcify a test to run by name',
'-f, --filter': 'glob to filter tests by filename'
},
details:
`All Nightwatch CLI options are also supported.\n` +
`https://github.com/nightwatchjs/nightwatch/blob/master/lib/runner/cli/cli.js`
}, (args, rawArgs) => {
removeArg(rawArgs, 'url')
removeArg(rawArgs, 'mode')
const serverPromise = args.url
? Promise.resolve({ url: args.url })
: api.service.run('serve')
return serverPromise.then(({ server, url }) => {
// expose dev server url to tests
process.env.VUE_DEV_SERVER_URL = url
if (rawArgs.indexOf('--config') === -1) {
// expose user options to config file
const fs = require('fs')
let userOptionsPath, userOptions
if (fs.existsSync(userOptionsPath = api.resolve('nightwatch.config.js'))) {
userOptions = require(userOptionsPath)
} else if (fs.existsSync(userOptionsPath = api.resolve('nightwatch.json'))) {
userOptions = require(userOptionsPath)
}
process.env.VUE_NIGHTWATCH_USER_OPTIONS = JSON.stringify(userOptions || {})
rawArgs.push('--config', require.resolve('./nightwatch.config.js'))
}
if (rawArgs.indexOf('--env') === -1) {
rawArgs.push('--env', 'chrome')
}
const execa = require('execa')
const nightWatchBinPath = require.resolve('nightwatch/bin/nightwatch')
const runner = execa(nightWatchBinPath, rawArgs, { stdio: 'inherit' })
if (server) {
runner.on('exit', () => server.close())
runner.on('error', () => server.close())
}
if (process.env.VUE_CLI_TEST) {
runner.on('exit', code => {
process.exit(code)
})
}
return runner
})
})
}
module.exports.defaultModes = {
'test:e2e': 'production'
}