UNPKG

@suites/unit

Version:
92 lines (91 loc) 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TestBed = void 0; const testbed_builder_js_1 = require("./testbed-builder.js"); const core_unit_1 = require("@suites/core.unit"); /** * The main entry point for creating isolated test environments with Suites. * * TestBed provides two testing modes: * - **Solitary**: Complete isolation with all dependencies mocked * - **Sociable**: Selective real implementations with other dependencies mocked * * TestBed uses a Virtual DI Container to analyze the target class metadata, automatically * generate type-safe mocks, and wire everything together without boilerplate. * * @since 3.0.0 * @see {@link https://suites.dev/docs/api-reference/ | TestBed Documentation} * @see {@link TestBed.solitary} for isolated testing * @see {@link TestBed.sociable} for integration testing */ class TestBed { /** * Creates a test environment where all dependencies are automatically mocked for complete isolation. * * In solitary mode, every dependency is replaced with an auto-generated mock, this enables: * - Test units in complete isolation * - Control all inputs and outputs precisely * - Focus on specific behavior and edge cases * - Achieve fast, deterministic tests * * @template TClass The type of the class under test * @param targetClass - The class constructor to test in isolation * @returns A builder for configuring mocks before compilation * * @remarks * Solitary tests follow the London/Mockist TDD style where all collaborators are mocked. * This provides maximum control and speed at the cost of not catching integration issues. * For testing real interactions between components, use {@link TestBed.sociable}. * * @example * ```ts * // Basic usage - all dependencies are automatically mocked * const { unit, unitRef } = await TestBed.solitary(MyService).compile(); * * // Configure mock behavior after compilation * const mockLogger = unitRef.get(Logger); * mockLogger.log.mockReturnValue('test'); * ``` * * @since 3.0.0 * @see {@link https://suites.dev/docs/api-reference/testbed-solitary | Solitary Testing Guide} * @see {@link TestBed.sociable} for integration testing */ static solitary(targetClass) { return (0, testbed_builder_js_1.testBedBuilderFactory)(testbed_builder_js_1.SuitesDIAdapters, testbed_builder_js_1.SuitesDoublesAdapters, targetClass).create((core_unit_1.SolitaryTestBedBuilder)); } /** * Creates a test environment that selectively uses real implementations alongside mocks. * * In sociable mode, you explicitly choose which dependencies run real code * while everything else remains mocked. This allows to: * - Test interactions between multiple real components * - Catch integration bugs while maintaining test speed * - Validate business logic flows across boundaries * - Keep external I/O dependencies mocked * * @template TClass The type of the class under test * @param targetClass - The class constructor to test with selective real dependencies * @returns A builder requiring `.expose()` to specify real implementations * * @example * ```ts * // Test with real dependencies * const { unit, unitRef } = await TestBed.sociable(OrderService) * .expose(PriceCalculator) // PriceCalculator runs real code * .compile(); * * // Configure mocked dependencies * const inventory = unitRef.get(InventoryService); * inventory.checkStock.mockResolvedValue(true); * ``` * * @since 3.0.0 * @see {@link https://suites.dev/docs/api-reference/testbed-sociable | Sociable API Reference} * @see {@link https://suites.dev/docs/guides/sociable | Sociable Guide} */ static sociable(targetClass) { return (0, testbed_builder_js_1.testBedBuilderFactory)(testbed_builder_js_1.SuitesDIAdapters, testbed_builder_js_1.SuitesDoublesAdapters, targetClass).create((core_unit_1.SociableTestBedBuilder)); } } exports.TestBed = TestBed;