fava
Version:
A wannabe tiny largely-drop-in replacement for ava that works in the browser too.
53 lines (52 loc) • 1.87 kB
JavaScript
/* IMPORT */
import { NOOP } from './constants.js';
import Flags from './flags.js';
import Hooks from './hooks.js';
import Tester from './tester.js';
/* MAIN */
class Describer {
/* CONSTRUCTOR */
constructor(title, flags) {
/* VARIABLES */
this.describers = [];
this.executed = false;
this.hooks = new Hooks();
this.testers = [];
/* EXECUTION API */
this.execute = async () => {
//TODO: Make sure that hooks are called reliably, even in case of exceptions
this.executed = true;
const global = new Tester('global', NOOP, new Flags().flags).api();
await this.hooks.hooks.before(global);
await this.visit({
recursive: false,
onDescriber: describer => {
return describer.execute();
},
onTester: async (tester) => {
const local = new Tester('local', NOOP, new Flags().flags).api();
local.ctx = { ...global.ctx };
await this.hooks.hooks.beforeEach(local);
await tester.execute();
await this.hooks.hooks.afterEach(local);
}
});
await this.hooks.hooks.after(global);
};
this.visit = async ({ onDescriber, onTester, recursive = true }) => {
for (const describer of this.describers) {
await onDescriber?.(describer);
if (!recursive)
continue;
await describer.visit({ onDescriber, onTester, recursive });
}
for (const tester of this.testers) {
await onTester?.(tester);
}
};
this.flags = flags;
this.title = title;
}
}
/* EXPORT */
export default Describer;