Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mocha test task #339

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/just-scripts/src/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './cleanTask';
export * from './copyTask';
export * from './sassTask';
export * from './jestTask';
export * from './mochaTask';
export * from './tslintTask';
export * from './webpackTask';
export * from './apiExtractorTask';
Expand Down
84 changes: 84 additions & 0 deletions packages/just-scripts/src/tasks/mochaTask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { resolve, logger, resolveCwd, TaskFunction } from 'just-task';
import { spawn, encodeArgs } from 'just-scripts-utils';
import { existsSync } from 'fs';

export interface MochaTaskOptions {
config?: string;
allowUncaught?: boolean;
asyncOnly?: boolean;
bail?: boolean;
checkLeaks?: boolean;
delay?: boolean;
forbidOnly?: boolean;
forbidPending?: boolean;
retries?: number;
slow?: number;
timeout?: number;
ui?: string;
color?: boolean;
diff?: boolean;
fullTrace?: boolean;
growl?: boolean;
inlineDiffs?: boolean;
watch?: boolean;
_?: string[];

/**
* Arguments to be passed into a spawn call for mocha
*/
nodeArgs?: string[];

/**
* Environment variables to be passed to the mocha runner
*/
env?: NodeJS.ProcessEnv;
}

export function mochaTask(options: MochaTaskOptions = {}): TaskFunction {
const mochaConfigFile: string | undefined =
resolveCwd('./.mocharc.js') ||
resolveCwd('./.mocharc.yaml') ||
resolveCwd('./.mocharc.yml') ||
resolveCwd('./.mocharc.jsonc') ||
resolveCwd('./.mocharc.json') ||
undefined;

return function mocha() {
const mochaCmd = resolve('mocha/bin/mocha');
const configFile = options.config || mochaConfigFile;

if (mochaCmd) {
logger.info(`Running mocha`);
const cmd = process.execPath;
const args = [
...(options.nodeArgs || []),
mochaCmd,
...(configFile && existsSync(configFile) ? ['--config', configFile] : []),
...(options.allowUncaught ? ['--allow-uncaught'] : []),
...(options.bail ? ['--bail'] : []),
...(options.checkLeaks ? ['--check-leaks'] : []),
...(options.delay ? ['--delay'] : []),
...(options.forbidOnly ? ['--forbid-only'] : []),
...(options.forbidPending ? ['--forbid-pending'] : []),
...(options.retries ? ['--retries', options.retries.toString()] : []),
...(options.slow ? ['--slow', options.slow.toString()] : []),
...(options.timeout ? ['--timeout', options.timeout.toString()] : []),
...(options.ui ? ['--ui', options.ui] : []),
...(options.color ? ['--color'] : []),
...(options.diff ? ['--diff'] : []),
...(options.fullTrace ? ['--full-trace'] : []),
...(options.growl ? ['--growl'] : []),
...(options.inlineDiffs ? ['--inline-diffs'] : []),
...(options.watch ? ['--watch'] : []),
...(options._ || [])
].filter(arg => !!arg) as Array<string>;

logger.info(cmd, encodeArgs(args).join(' '));

return spawn(cmd, args, { stdio: 'inherit', env: options.env });
} else {
logger.warn('no mocha command found, skipping mocha');
return Promise.resolve();
}
};
}