UNPKG

arrange-act-assert

Version:

The lightweight "Act-Arrange-Assert" oriented testing framework

133 lines (132 loc) 5.02 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TestSuite = void 0; const OS = __importStar(require("os")); const parallelize_1 = require("../parallelize/parallelize"); const MainContext_1 = require("./MainContext"); const testRunner_1 = require("../testRunner/testRunner"); const default_1 = require("../formatters/default"); const singleton_1 = __importDefault(require("../coverage/singleton")); const Utils = __importStar(require("../utils/utils")); const DEFAULT_OPTIONS = { parallel: OS.cpus().length, folder: process.cwd(), include: [/(\\|\/|.*(\.|-|_))(test)(\.|(\.|-|\\|\/).*.)(cjs|mjs|js)$/i], exclude: [/\/node_modules\//i], prefix: [], clearModuleCache: false, formatter: new default_1.DefaultFormatter() }; class TestSuite { constructor(options, context = new MainContext_1.MainContext()) { this.context = context; this.options = { ...DEFAULT_OPTIONS, ...Utils.getTestSuiteOptions(), ...options }; this._root = (0, testRunner_1.newRoot)(this.options); if (!Number.isFinite(this.options.parallel) || this.options.parallel < 0) { throw new Error("Invalid parallel option. Must be >= 0"); } this._root.setFormatter(this.options.formatter); } async run() { if (this.options.coverage) { await singleton_1.default.start(); } const result = await this._run(); await this._root.end(); if (this.options.coverage) { if (this.options.parallel === 0) { this._root.processMessage("", { type: 5, coverage: await singleton_1.default.takeCoverage() }); } await singleton_1.default.stop(); } this.options.formatter.formatSummary && await this.options.formatter.formatSummary(this._root.summary, { excludeFiles: result.files, exclude: this.options.exclude, branches: !this.options.coverageNoBranches, sourceMaps: !this.options.coverageNoSourceMaps }); for (const error of result.errors) { console.error(error); } return { files: result.files, runErrors: result.errors, ok: result.errors.length === 0 && this._root.summary.total.error === 0, summary: this._root.summary }; } async _run() { const files = await this.context.getFiles(this.options.folder, this.options); if (this.options.parallel === 0) { const errors = []; for (const file of files) { try { this._root.runTestFile(file, this.options); } catch (e) { errors.push(e); } } return { files: files, errors: errors }; } else { const results = await (0, parallelize_1.parallelize)(this.options.parallel, this._spawnTestFiles(files)); return { files: files, errors: results.filter(result => result.status === "rejected").map(result => result.reason) }; } } *_spawnTestFiles(testFiles) { for (const testFile of testFiles) { yield this._root.spawnTestFile(testFile, this.options); } } } exports.TestSuite = TestSuite;