UNPKG

@featurevisor/core

Version:

Core package of Featurevisor for Node.js usage

183 lines 6.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.testPlugin = void 0; exports.executeTest = executeTest; exports.testProject = testProject; const fs = require("fs"); const testSegment_1 = require("./testSegment"); const testFeature_1 = require("./testFeature"); const cliFormat_1 = require("./cliFormat"); const prettyDuration_1 = require("./prettyDuration"); const printTestResult_1 = require("./printTestResult"); const builder_1 = require("../builder"); const config_1 = require("../config"); const list_1 = require("../list"); async function executeTest(test, deps, options, datafileContentByEnvironment) { const { datasource, projectConfig, rootDirectoryPath } = deps; const extension = datasource.getExtension(); const relativeTestFilePath = test.key + (extension ? `.${extension}` : ""); const tAsSegment = test; const tAsFeature = test; const key = tAsSegment.segment || tAsFeature.feature; const type = tAsSegment.segment ? "segment" : "feature"; const executionResult = { passed: true, assertionsCount: { passed: 0, failed: 0, }, }; if (!key) { console.error(` => Invalid test: ${JSON.stringify(test)}`); executionResult.passed = false; return executionResult; } let testResult; if (type === "segment") { testResult = await (0, testSegment_1.testSegment)(datasource, tAsSegment, options); } else { testResult = await (0, testFeature_1.testFeature)(datasource, projectConfig, tAsFeature, options, datafileContentByEnvironment); } if (!options.onlyFailures) { // show all (0, printTestResult_1.printTestResult)(testResult, relativeTestFilePath, rootDirectoryPath); } else { // show failed only if (!testResult.passed) { (0, printTestResult_1.printTestResult)(testResult, relativeTestFilePath, rootDirectoryPath); } } if (!testResult.passed) { executionResult.passed = false; executionResult.assertionsCount.failed = testResult.assertions.filter((a) => !a.passed).length; executionResult.assertionsCount.passed += testResult.assertions.length - executionResult.assertionsCount.failed; } else { executionResult.assertionsCount.passed = testResult.assertions.length; } return executionResult; } async function testProject(deps, testOptions = {}) { const { rootDirectoryPath, projectConfig, datasource, options } = deps; let hasError = false; if (!fs.existsSync(projectConfig.testsDirectoryPath)) { console.error(`Tests directory does not exist: ${projectConfig.testsDirectoryPath}`); hasError = true; return hasError; } const startTime = Date.now(); let passedTestsCount = 0; let failedTestsCount = 0; let passedAssertionsCount = 0; let failedAssertionsCount = 0; const datafileContentByEnvironment = new Map(); // with environments if (Array.isArray(projectConfig.environments)) { for (const environment of projectConfig.environments) { const existingState = await datasource.readState(environment); const datafileContent = await (0, builder_1.buildDatafile)(projectConfig, datasource, { schemaVersion: options.schemaVersion || config_1.SCHEMA_VERSION, revision: "include-all-features", environment: environment, inflate: options.inflate, }, existingState); datafileContentByEnvironment.set(environment, datafileContent); } } // no environments if (projectConfig.environments === false) { const existingState = await datasource.readState(false); const datafileContent = await (0, builder_1.buildDatafile)(projectConfig, datasource, { schemaVersion: options.schemaVersion || config_1.SCHEMA_VERSION, revision: "include-all-features", environment: false, inflate: options.inflate, }, existingState); datafileContentByEnvironment.set(false, datafileContent); } const tests = await (0, list_1.listEntities)({ rootDirectoryPath, projectConfig, datasource, options: { ...testOptions, applyMatrix: true, }, }, "test"); for (const test of tests) { const executionResult = await executeTest(test, deps, options, datafileContentByEnvironment); if (!executionResult) { continue; } if (executionResult.passed) { passedTestsCount += 1; } else { hasError = true; failedTestsCount += 1; } passedAssertionsCount += executionResult.assertionsCount.passed; failedAssertionsCount += executionResult.assertionsCount.failed; } const diffInMs = Date.now() - startTime; if (options.onlyFailures !== true || hasError) { console.log("\n---"); } console.log(""); const testSpecsMessage = `Test specs: ${passedTestsCount} passed, ${failedTestsCount} failed`; const testAssertionsMessage = `Assertions: ${passedAssertionsCount} passed, ${failedAssertionsCount} failed`; if (hasError) { console.log(cliFormat_1.CLI_FORMAT_RED, testSpecsMessage); console.log(cliFormat_1.CLI_FORMAT_RED, testAssertionsMessage); } else { console.log(cliFormat_1.CLI_FORMAT_GREEN, testSpecsMessage); console.log(cliFormat_1.CLI_FORMAT_GREEN, testAssertionsMessage); } console.log(cliFormat_1.CLI_FORMAT_BOLD, `Time: ${(0, prettyDuration_1.prettyDuration)(diffInMs)}`); return hasError; } exports.testPlugin = { command: "test", handler: async function ({ rootDirectoryPath, projectConfig, datasource, parsed }) { const hasError = await testProject({ rootDirectoryPath, projectConfig, datasource, options: parsed, }, parsed); if (hasError) { return false; } }, examples: [ { command: "test", description: "run all tests", }, { command: "test --keyPattern=pattern", description: "run tests matching key pattern", }, { command: "test --assertionPattern=pattern", description: "run tests matching assertion pattern", }, { command: "test --onlyFailures", description: "run only failed tests", }, { command: "test --showDatafile", description: "show datafile content for each test", }, { command: "test --verbose", description: "show all test results", }, ], }; //# sourceMappingURL=testProject.js.map