UNPKG

turbo-gulp

Version:

Gulp tasks to boost high-quality projects.

115 lines (114 loc) 3.98 kB
/** * This module defines the _mocha_ target type used to build and run Mocha tests. * * In the following list of tasks, `{target}` represents the name of the target as defined by the `name` property * of the target options. * The _lib_ target provides the following tasks: * * ## {target} * * Build and run the tests, use the console reporter. * * ## {target}:build * * Performs a full test build of the library and test files. * * ## {target}:run * * Only run the tests (does not build the tests). * * ## {target}:coverage * * Run tests with coverage (does not build the tests). * * ## {target}:typedoc:deploy * * Deploy the _Typedoc_ documentation using _git_. This can be used to easily deploy the documentation to the * `gh-pages` branch. * * ## {target}:clean * * Remove the build and directory corresponding to this target. * * ## {target}:tsconfig.json * * Emit a `tsconfig.json` file corresponding to the configuration for this target. This allows to compile it using * the command line `tsc` program. This is also useful for IDE to auto-detect the configuration of the project. * * @module targets/mocha */ import { posix as posixPath } from "path"; import { OutModules } from "../"; import * as mocha from "../task-generators/mocha"; import * as nyc from "../task-generators/nyc"; import { nameTask, registerBaseTasks, resolveTargetBase } from "./_base"; /** * Resolve absolute paths and dependencies for the provided target. * * @param target Non-resolved target. * @return Resolved target. */ function resolveMochaTarget(target) { return resolveTargetBase(target); } /** * Generates gulp tasks for the provided Mocha target. * * @param gulp Gulp instance used to generate tasks manipulating files. * @param targetOptions Target configuration. */ export function generateMochaTasks(gulp, targetOptions) { const target = resolveMochaTarget(targetOptions); const result = registerBaseTasks(gulp, targetOptions); const testOptions = { rootDir: target.project.absRoot, testDir: target.buildDir, }; const runTasks = []; if (target.outModules === OutModules.Js || target.outModules === OutModules.Both) { const runCjs = nameTask(`${target.name}:run:cjs`, mocha.generateTask(gulp, testOptions)); result.runCjs = runCjs; runTasks.push(runCjs); } if (target.outModules === OutModules.Mjs || target.outModules === OutModules.Both) { const runEsm = nameTask(`${target.name}:run:esm`, mocha.generateTask(gulp, Object.assign({}, testOptions, { mjs: true }))); result.runEsm = runEsm; runTasks.push(runEsm); } // run result.run = nameTask(`${target.name}:run`, gulp.series(runTasks)); const coverageOptions = { test: Object.assign({}, testOptions, { mjs: target.outModules !== OutModules.Js }), rootDir: target.project.absRoot, reportDir: posixPath.join(target.project.absRoot, "coverage"), tempDir: posixPath.join(target.project.absRoot, ".nyc_output"), reporters: ["text", "lcovonly", "html"], }; // coverage result.coverage = nameTask(`${target.name}:coverage`, nyc.generateTask(gulp, coverageOptions)); // start const startTasks = []; if (result.clean !== undefined) { startTasks.push(result.clean); } startTasks.push(result.build); startTasks.push(result.coverage); result.start = nameTask(target.name, gulp.series(startTasks)); return result; } /** * Generates and registers gulp tasks for the provided Mocha target. * * @param gulp Gulp instance where the tasks will be registered. * @param targetOptions Target configuration. */ export function registerMochaTasks(gulp, targetOptions) { const tasks = generateMochaTasks(gulp, targetOptions); for (const key in tasks) { const task = tasks[key]; if (task !== undefined) { gulp.task(task); } } return tasks; }