UNPKG

ts-bdd

Version:

A TypeScript BDD testing framework with typed shared examples and state management

91 lines 4.05 kB
"use strict"; /* eslint-disable @typescript-eslint/no-explicit-any */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RunnableSuite = void 0; const index_js_1 = require("../shared-examples-builder/index.js"); class RunnableSuite { definitions; runner; constructor(definitions, runner) { this.definitions = definitions; this.runner = runner; } describe(description, callback) { // Create a completely separate scope to avoid variable conflicts const executeInlineBuilder = () => { let builderCache = new Map(); let builderSubjectFactory = null; const builder = new index_js_1.SharedExamplesBuilder(); this.runner.beforeEach(() => { builderCache = new Map(); }); const builderGet = (...keys) => { const getSingleValue = (key) => { if (builderCache.has(key)) { return builderCache.get(key); } // For definition-based values const definition = this.definitions[key]; const value = typeof definition === 'function' ? definition(builderGet) : definition; builderCache.set(key, value); return value; }; if (keys.length === 1 && keys[0]) { return getSingleValue(keys[0]); } return keys.map((key) => getSingleValue(key)); }; const builderSet = (key, valueFactory) => { // Always treat as a factory function that should be evaluated lazily // Convert to a get-receiving factory for internal consistency const lazyFactory = (_get) => valueFactory(); // Update immediately for current context (needed for shared examples) builderCache.delete(key); this.definitions[key] = lazyFactory; // Also schedule for test runs (for proper test isolation) this.runner.beforeEach(() => { builderCache.delete(key); this.definitions[key] = lazyFactory; }); }; const builderSubject = ((factoryOrNothing) => { if (factoryOrNothing !== undefined) { builderSubjectFactory = factoryOrNothing; return; } if (builderSubjectFactory) { return builderSubjectFactory(); } return undefined; }); const builderContext = (contextDescription, contextCallback) => { this.runner.describe(contextDescription, () => { // Call context callback - set() calls inside will automatically // create beforeEach hooks at this context level due to Jest's scoping contextCallback(); // Don't restore parent factory - let child contexts keep their subject // This allows shared examples to access the subject factory set in child contexts }); }; // Assign the build method to the builder instance builder.build = () => builder._build({ subject: builderSubject, get: builderGet, }); const sharedExamplesWithBuild = builder; // Execute the callback to let user define shared examples and tests callback({ get: builderGet, set: builderSet, context: builderContext, subject: builderSubject, sharedExamplesBuilder: sharedExamplesWithBuild, }); }; this.runner.describe(description, executeInlineBuilder); } } exports.RunnableSuite = RunnableSuite; //# sourceMappingURL=index.js.map