UNPKG

1.06 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 Mocha = require('mocha')
17
18/** @lends amocha */
19async function amocha(pattern, options = {}) {
20 const {
21 cwd = process.cwd(),
22 reporter = 'spec',
23 timeout = 3000
24 } = options
25 const mocha = new Mocha({
26 reporter,
27 timeout
28 })
29 const filenames = await aglob(pattern, { cwd })
30 for (const filename of filenames) {
31 mocha.addFile(path.resolve(cwd, filename))
32 }
33 const failures = await new Promise((resolve) => {
34 mocha.run((failures) => resolve(failures)
35 )
36 })
37 process.on('exit', () => process.exit(failures))
38 return failures
39}
40
41module.exports = amocha