ts-bdd
Version:
A TypeScript BDD testing framework with typed shared examples and state management
103 lines • 3.94 kB
JavaScript
;
/* eslint-disable @typescript-eslint/no-explicit-any */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SharedExamplesBuilder = void 0;
// Builder for creating typed shared examples
class SharedExamplesBuilder {
#examples;
constructor(examples = {}) {
this.#examples = examples;
}
add(name, callback) {
// For inline builder, mutate the current instance
this.#examples[name] = callback;
return this;
}
// Add build method to the builder class itself
build;
// Internal method to execute a shared example (used by framework)
#executeExample(name, params) {
const example = this.#examples[name];
if (example) {
const exampleParams = {
subject: params.subject,
itBehavesLike: params.itBehavesLike,
get: params.get,
};
// If no arguments provided, call with just params (no-arg shared example)
// Otherwise, call with arg first, then params (shared example with args)
if (params.args.length === 0) {
example(exampleParams);
}
else {
example(params.args[0], exampleParams);
}
return true;
}
return false;
}
// Internal method - framework access only
/** @internal */
_executeExample(name, params) {
return this.#executeExample(name, params);
}
// Internal method - framework access only
/** @internal */
_getExamples() {
return this.#examples;
}
// Create a clean build() method that captures context automatically
#build(params) {
const builtFunction = (name, ...args) => {
// Create a typed itBehavesLike function for nested calls
const typedItBehavesLike = (nestedName, ...nestedArgs) => {
const _nestedExecuted = this.#executeExample(nestedName, {
subject: params.subject,
itBehavesLike: typedItBehavesLike, // Type assertion for recursion
args: nestedArgs,
get: params.get,
});
// Silently ignore non-existent nested examples
};
const _executed = this.#executeExample(name, {
subject: params.subject,
itBehavesLike: typedItBehavesLike, // Type assertion for parameter compatibility
args,
get: params.get,
});
// Silently ignore non-existent examples
};
return builtFunction;
}
// Internal method - framework access only
/** @internal */
_build(params) {
return this.#build(params);
}
// Internal method - framework access only
/** @internal */
_createItBehavesLike(params) {
return (name, ...args) => {
const _executed = this.#executeExample(name, {
subject: params.subject,
itBehavesLike: (nestedName, ...nestedArgs) => {
const _nestedExecuted = this.#executeExample(nestedName, {
subject: params.subject,
itBehavesLike: () => {
// Empty placeholder - prevents infinite recursion in nested examples
}, // Prevent infinite recursion
args: nestedArgs,
get: params.get,
});
// Silently ignore non-existent nested examples
},
args,
get: params.get,
});
// Silently ignore non-existent examples instead of throwing
// This allows for more graceful handling in test scenarios
};
}
}
exports.SharedExamplesBuilder = SharedExamplesBuilder;
//# sourceMappingURL=index.js.map