UNPKG

1.21 kBJavaScriptView Raw
1/**
2 * Run mocha tests
3 * @function amocha
4 * @param {string} pattern - Glob file name pattern
5 * @param {Object} [options] - Optional settings
6 * @param {string} reporter
7 * @param {string} [options.cwd=process.cwd()] - Working directory path
8 * @param {string} [options.reporter='spec'] - Mocha reporter
9 * @param {number} [options.timeout=3000] - Timeout duration
10 * @returns {Promise}
11 */
12'use strict'
13
14const aglob = require('aglob')
15const path = require('path')
16const co = require('co')
17const Mocha = require('mocha')
18const coverage = require('./coverage')
19
20/** @lends amocha */
21function amocha (pattern, options = {}) {
22 let {
23 cwd = process.cwd(),
24 reporter = 'spec',
25 timeout = 3000
26 } = options
27 return co(function * () {
28 const mocha = new Mocha({
29 reporter,
30 timeout
31 })
32 let filenames = yield aglob(pattern, { cwd })
33 for (let filename of filenames) {
34 mocha.addFile(path.resolve(cwd, filename))
35 }
36 let failures = yield new Promise((resolve) => {
37 mocha.run((failures) => resolve(failures)
38 )
39 })
40 process.on('exit', () => process.exit(failures))
41 return failures
42 })
43}
44
45Object.assign(amocha, { coverage })
46
47module.exports = amocha