harmonyc
Version:
Harmony Code - model-driven BDD for Vitest
70 lines (69 loc) • 2.37 kB
JavaScript
import { watchFiles } from "../cli/watch.js";
import c from 'tinyrainbow';
import { compileFiles } from "../compiler/compiler.js";
export default function harmonyPlugin({ watchDir, }) {
return {
name: 'harmony',
config(config) {
var _a, _b;
var _c;
(_a = config.test) !== null && _a !== void 0 ? _a : (config.test = {});
(_b = (_c = config.test).reporters) !== null && _b !== void 0 ? _b : (_c.reporters = ['default']);
if (!Array.isArray(config.test.reporters)) {
config.test.reporters = [config.test.reporters];
}
config.test.reporters.splice(0, 0, new HarmonyReporter());
},
async configureServer(server) {
const isWatchMode = server.config.server.watch !== null;
const patterns = [`${watchDir}/**/*.harmony`];
if (isWatchMode) {
await watchFiles(patterns);
}
else {
await compileFiles(patterns);
}
},
};
}
class HarmonyReporter {
onInit(ctx) {
this.ctx = ctx;
}
onCollected(files) {
this.files = files;
}
onTaskUpdate(packs) {
if (this.files)
for (const file of this.files)
addPhrases(file);
}
onFinished(files, errors) {
for (const file of files)
addPhrases(file);
}
}
function addPhrases(task, depth = 2) {
var _a, _b;
if ('tasks' in task) {
for (const child of task.tasks)
addPhrases(child, depth + 1);
}
else if (task.type === 'test' &&
((_a = task.result) === null || _a === void 0 ? void 0 : _a.state) === 'fail' &&
((_b = task.meta) === null || _b === void 0 ? void 0 : _b.hasOwnProperty('phrases')) &&
task.meta.phrases.length > 0) {
const x = task;
x.name +=
'\n' +
task.meta
.phrases.map((step, i, a) => {
const indent = ' '.repeat(depth);
const failed = i === a.length - 1;
const figure = failed ? c.red('×') : c.green('✓');
return `${indent}${figure} ${step}`;
})
.join('\n');
delete task.meta.phrases; // to make sure not to add them again
}
}