Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
Add support for some mocha cli options
Browse files Browse the repository at this point in the history
  • Loading branch information
ctdio committed Sep 9, 2017
1 parent 3868b61 commit 848d605
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ const _loadConfig = require('./src/utils/loadConfig')

const mochaPuppeteerPkgVersion = require('./package.json').version

// pick specific values from config
function _pickConfig (config) {
/* eslint-disable */
// TODO: Add more fields to pick (ex. puppeteerOptions)
return ({ lassoConfig } = config)
/* eslint-enable */
}

const parser = argly
.createParser({
'--help -h': {
Expand All @@ -25,6 +17,18 @@ const parser = argly
'--pattern -p *': {
type: 'string',
description: 'Pattern to run tests. Either a single file or glob pattern.'
},
'--reporter': {
type: 'string',
description: 'The mocha test reporter to use. (Defaults to "spec")'
},
'--useColors': {
type: 'boolean',
description: 'Whether use colors for test output. (Defaults to true)'
},
'--ui': {
type: 'string',
description: 'The mocha ui to use. (Defaults to "bdd")'
}
})
.example('Test a single file: "mocha-puppeteer /foo/bar-test.js"')
Expand All @@ -47,25 +51,44 @@ module.exports = async function runCli () {
const {
pattern,
help,
version
version,

// mocha options (TODO: add more)
reporter,
useColors,
ui
} = parser.parse()

// Gracefully exit if either the "help" or "version" arguments are supplied
// of if the "pattern" argument is missing because it's required.
if (help || version || !pattern) return

try {
let mochaOptions = {}
let lassoConfig = {}

const config = await _loadConfig({
startingDirectory: process.cwd()
})

const options = config ? _pickConfig(config) : {}
if (config) {
config.mochaOptions && (mochaOptions = config.mochaOptions)
config.lassoConfig && (lassoConfig = config.lassoConfig)
}

// apply cli overrides
reporter !== undefined && (mochaOptions.reporter = reporter)
useColors !== undefined && (mochaOptions.useColors = useColors)
ui !== undefined && (mochaOptions.ui = ui)

Object.assign(options, { testFiles: [ pattern ] })
const options = Object.assign({
mochaOptions,
lassoConfig
}, { testFiles: [ pattern ] })

await mochaPuppeteer.runTests(options)
} catch (err) {
console.error(err.message)
console.error(err)
process.exit(1)
}
}

0 comments on commit 848d605

Please sign in to comment.