@teambit/tester
Version:
169 lines (167 loc) • 6.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TesterService = exports.OnTestsChanged = void 0;
function _path() {
const data = require("path");
_path = function () {
return data;
};
return data;
}
function _component() {
const data = require("@teambit/component");
_component = function () {
return data;
};
return data;
}
function _cliHighlight() {
const data = _interopRequireDefault(require("cli-highlight"));
_cliHighlight = function () {
return data;
};
return data;
}
function _tester() {
const data = require("./tester");
_tester = function () {
return data;
};
return data;
}
function _tester2() {
const data = require("./tester.aspect");
_tester2 = function () {
return data;
};
return data;
}
function _utils() {
const data = require("./utils");
_utils = 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); }
const chalk = require('chalk');
const OnTestsChanged = exports.OnTestsChanged = 'OnTestsChanged';
class TesterService {
constructor(workspace, logger, pubsub, devFiles) {
this.workspace = workspace;
this.logger = logger;
this.pubsub = pubsub;
this.devFiles = devFiles;
_defineProperty(this, "name", 'tester');
_defineProperty(this, "_callback", void 0);
}
render(env) {
const descriptor = this.getDescriptor(env);
const name = `${chalk.green('configured tester:')} ${descriptor?.id} (${descriptor?.displayName} @ ${descriptor?.version})`;
const configLabel = chalk.green('tester config:');
const configObj = descriptor?.config ? (0, _cliHighlight().default)(descriptor?.config, {
language: 'javascript',
ignoreIllegals: true
}) : '';
return `${name}\n${configLabel}\n${configObj}`;
}
getDescriptor(environment) {
if (!environment.env.getTester) return undefined;
const tester = environment.env.getTester();
return {
id: tester.id || '',
displayName: tester.displayName || '',
icon: tester.icon || '',
config: tester.displayConfig ? tester.displayConfig() : '',
version: tester.version ? tester.version() : '?'
};
}
transform(env, context) {
// Old env
if (!env?.tester) return undefined;
return {
getTester: () => env.tester()(context)
};
}
onTestRunComplete(callback) {
this._callback = callback;
}
async run(context, options) {
if (!context.env.getTester) {
return new (_tester().Tests)([]);
}
const tester = context.env.getTester();
const specFiles = _component().ComponentMap.as(context.components, component => {
return (0, _utils().detectTestFiles)(component, this.devFiles);
});
const testCount = specFiles.toArray().reduce((acc, [, specs]) => acc + specs.length, 0);
const componentWithTests = specFiles.toArray().reduce((acc, [, specs]) => {
if (specs.length > 0) acc += 1;
return acc;
}, 0);
if (testCount === 0 && !options.ui) {
this.logger.consoleWarning(`no tests found for components using environment ${chalk.cyan(context.id)}\n`);
return new (_tester().Tests)([]);
}
if (!options.ui) this.logger.console(`testing ${componentWithTests} components with environment ${chalk.cyan(context.id)}\n`);
const patterns = await _component().ComponentMap.asAsync(context.components, async component => {
const componentDir = this.workspace.componentDir(component.id);
const componentPatterns = this.devFiles.getDevPatterns(component, _tester2().TesterAspect.id);
const packageRootDir = await this.workspace.getComponentPackagePath(component);
return {
componentDir,
packageRootDir,
paths: componentPatterns.map(pattern => ({
path: (0, _path().resolve)(componentDir, pattern),
relative: pattern
})) || []
};
});
let additionalHostDependencies = [];
if (context.env.getAdditionalTestHostDependencies && typeof context.env.getAdditionalTestHostDependencies === 'function') {
additionalHostDependencies = await context.env.getAdditionalTestHostDependencies();
}
const testerContext = Object.assign(context, {
release: false,
specFiles,
patterns,
sourcePatterns: patterns,
rootPath: this.workspace.path,
workspace: this.workspace,
debug: options.debug,
watch: options.watch,
ui: options.ui,
coverage: options.coverage,
updateSnapshot: options.updateSnapshot,
additionalHostDependencies
});
if (options.watch && options.ui && tester.watch) {
if (tester.onTestRunComplete) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
tester.onTestRunComplete(results => {
if (this._callback) this._callback(results);
results.components.forEach(component => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.pubsub.publish(OnTestsChanged, {
testsChanged: {
id: component.componentId.toString(),
testsResults: component.results,
loading: component.loading
}
});
});
});
}
return tester.watch(testerContext);
}
const results = await tester.test(testerContext);
return results;
}
}
exports.TesterService = TesterService;
//# sourceMappingURL=tester.service.js.map