UNPKG

@teambit/tester

Version:
168 lines (164 loc) • 5.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TesterTask = void 0; exports.getArtifactDef = getArtifactDef; exports.getJUnitArtifactPath = getJUnitArtifactPath; function _builder() { const data = require("@teambit/builder"); _builder = function () { return data; }; return data; } function _fsExtra() { const data = _interopRequireDefault(require("fs-extra")); _fsExtra = function () { return data; }; return data; } function _path() { const data = require("path"); _path = function () { return data; }; return data; } function _compiler() { const data = require("@teambit/compiler"); _compiler = function () { return data; }; return data; } function _component() { const data = require("@teambit/component"); _component = function () { return data; }; return data; } function _utils() { const data = require("./utils"); _utils = function () { return data; }; return data; } function _junitGenerator() { const data = require("./utils/junit-generator"); _junitGenerator = function () { return data; }; return data; } function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /** * tester build task. Allows to test components during component build. */ class TesterTask { constructor(aspectId, devFiles) { this.aspectId = aspectId; this.devFiles = devFiles; _defineProperty(this, "name", 'TestComponents'); _defineProperty(this, "dependencies", [_compiler().CompilerAspect.id]); } async execute(context) { if (!context.env.getTester) { return { componentsResults: [] }; } const components = context.capsuleNetwork.originalSeedersCapsules.getAllComponents(); const tester = context.env.getTester(); const componentsSpecFiles = _component().ComponentMap.as(components, component => { return (0, _utils().detectTestFiles)(component, this.devFiles); }); const testCount = componentsSpecFiles.toArray().reduce((acc, [, specs]) => acc + specs.length, 0); if (testCount === 0) return { artifacts: [], componentsResults: [] }; const patternsWithCapsule = _component().ComponentMap.as(components, component => { const componentSpecFiles = componentsSpecFiles.get(component); if (!componentSpecFiles) throw new Error('capsule not found'); const [, specs] = componentSpecFiles; const capsule = context.capsuleNetwork.graphCapsules.getCapsule(component.id); if (!capsule) throw new Error('capsule not found'); const compiler = context.env.getCompiler(); if (!compiler) { throw new Error(`compiler not found for ${component.id.toString()}`); } // @ts-ignore. not sure why ts complain that compiler might be undefined, when we check it above. const distFolder = compiler.getDistDir() || compiler.distDir; return { componentDir: (0, _path().join)(capsule.path, distFolder), paths: specs.map(specFile => { const distPath = compiler.getDistPathBySrcPath(specFile.relative); // TODO: fix spec type file need to capsule will return files with type AbstractVinyl return { path: (0, _path().join)(capsule.path, distPath), relative: distPath }; }) }; }); const specFilesWithCapsule = _component().ComponentMap.as(components, component => { const patternEntry = patternsWithCapsule.get(component); // @ts-ignore const [, val] = patternEntry; return val.paths; }); const testerContext = Object.assign(context, { release: true, specFiles: specFilesWithCapsule, rootPath: context.capsuleNetwork.capsulesRootDir, patterns: patternsWithCapsule }); // TODO: remove after fix AbstractVinyl on capsule // @ts-ignore const testsResults = await tester.test(testerContext); // write junit files await Promise.all(testsResults.components.map(async compResult => { const junit = (0, _junitGenerator().testsResultsToJUnitFormat)([compResult]); const capsule = context.capsuleNetwork.graphCapsules.getCapsule(compResult.componentId); if (!capsule) { throw new Error(`unable to find ${compResult.componentId.toString()} in capsules`); } await _fsExtra().default.outputFile((0, _path().join)(capsule.path, getJUnitArtifactPath()), junit); })); return { artifacts: getArtifactDef(), componentsResults: testsResults.components.map(componentTests => { const componentErrors = componentTests.errors; const component = context.capsuleNetwork.graphCapsules.getCapsule(componentTests.componentId)?.component; if (!component) { throw new Error(`unable to find ${componentTests.componentId.toString()} in capsules`); } return { component, metadata: { tests: componentTests.results }, errors: componentErrors }; }) }; } } exports.TesterTask = TesterTask; function getJUnitArtifactPath() { return (0, _path().join)(_builder().CAPSULE_ARTIFACTS_DIR, '__bit_junit.xml'); } function getArtifactDef() { return [{ name: 'junit', globPatterns: [getJUnitArtifactPath()] }]; } //# sourceMappingURL=tester.task.js.map