turbo-gulp
Version:
Gulp tasks to boost high-quality projects.
53 lines (52 loc) • 1.71 kB
JavaScript
import { Incident } from "incident";
import { Minimatch } from "minimatch";
import { toPosix } from "../project";
import * as matcher from "./matcher";
import { SpawnedProcess } from "./node-async";
const MOCHA_BIN = toPosix(require.resolve("mocha/bin/mocha"));
export async function execMocha(args = [], options) {
return new SpawnedProcess(MOCHA_BIN, args, Object.assign({ stdio: "pipe" }, options)).toPromise();
}
export function getSources(options) {
const baseDir = options.testDir;
const glob = new Minimatch(options.mjs ? "**/*.spec.mjs" : "**/*.spec.js");
const specs = matcher.asString(matcher.join(baseDir, glob));
return {
baseDir,
specs: [specs],
};
}
/**
* Return the command line arguments equivalent to this task
*
* @param options Mocha options
* @return Command line arguments
*/
export function getCommand(options) {
return [MOCHA_BIN, ...getCommandArgs(options)];
}
export function getCommandArgs(options) {
const sources = getSources(options);
const result = [];
result.push("--ui", "bdd");
result.push("--reporter", options.reporter);
if (options.colors) {
result.push("--colors");
}
if (options.mjs) {
result.push("--require", "@std/esm");
}
result.push("--", ...sources.specs);
return result;
}
export async function run(options) {
const args = getCommandArgs(options);
const result = await execMocha(args, { cwd: options.cwd, stdio: "inherit" });
if (result.exit.type === "code") {
if (result.exit.code === 0) {
return;
}
throw Incident("TestError");
}
throw new Incident("UnexpectedExitValue", { exit: result.exit });
}