Skip to content

Commit

Permalink
feat(mocha): allow plugin to pick up .mocharc files
Browse files Browse the repository at this point in the history
We want to delegate as much configuration to tools themselves rather
than exposing options via Tool Kit. Unfortunately, the mocha plugin was
invoking mocha with a low-level API and skipping the code which reads
configuration files, meaning that users could not set options like they
can with plugins such as Jest and Webpack.

To solve this let's just invoke the mocha CLI directly rather than via a
lower-level API so that it calls all the setup code it would for a
normal invocation, including looking for configuration files. The user
can also set the path for the config file explicitly in the Tool Kit
options.

The files that mocha reads can now be set in its own configuration file
so we don't really need to have them be set by Tool Kit. The option to
do so might be removed in a future major release of the plugin.
  • Loading branch information
ivomurrell committed Jun 17, 2022
1 parent d8eacaa commit 1ef9fd5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 56 deletions.
3 changes: 2 additions & 1 deletion lib/types/src/schema/mocha.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SchemaOutput } from '../schema'

export const MochaSchema = {
files: 'string'
files: 'string',
configPath: 'string?'
} as const
export type MochaOptions = SchemaOutput<typeof MochaSchema>

Expand Down
50 changes: 25 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 8 additions & 30 deletions plugins/mocha/src/tasks/mocha.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { hookConsole, styles } from '@dotcom-tool-kit/logger'
import { hookFork, waitOnExit } from '@dotcom-tool-kit/logger'
import { Task } from '@dotcom-tool-kit/types'
import MochaCore from 'mocha'
import { glob } from 'glob'
import { ToolKitError } from '@dotcom-tool-kit/error'
import { MochaOptions, MochaSchema } from '@dotcom-tool-kit/types/lib/schema/mocha'
import { fork } from 'child_process'
const mochaCLIPath = require.resolve('mocha/bin/mocha')

export default class Mocha extends Task<typeof MochaSchema> {
static description = ''
Expand All @@ -13,34 +13,12 @@ export default class Mocha extends Task<typeof MochaSchema> {
}

async run(): Promise<void> {
const mocha = new MochaCore()

const files = glob.sync(this.options.files)
if (files.length === 0) {
const error = new ToolKitError('No test files found')
error.details = `We looked for files matching ${styles.filepath(
this.options.files
)}, but there weren't any. Set ${styles.title(
'options."@dotcom-tool-kit/mocha".files'
)} in your Tool Kit configuration to change this file pattern.`
throw error
}

files.forEach((file) => {
mocha.addFile(file)
})

const unhook = hookConsole(this.logger, 'mocha')
await new Promise<void>((resolve, reject) => {
mocha.run((failures) => {
if (failures > 0) {
const error = new ToolKitError('mocha tests failed')
error.details = 'please fix the test failures and retry'
reject(error)
} else {
resolve()
}
})
}).finally(() => unhook())
const args = [this.options.configPath ? `--config=${this.options.configPath}` : '', ...files]
this.logger.info(`running mocha ${args.join(' ')}`)
const child = fork(mochaCLIPath, args, { silent: true })
hookFork(this.logger, 'mocha', child)
return waitOnExit('mocha', child)
}
}

0 comments on commit 1ef9fd5

Please sign in to comment.