@suites/unit
Version:
119 lines (118 loc) • 5.53 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.testBedBuilderFactory = exports.SuitesDIAdapters = exports.SuitesDoublesAdapters = exports.AdapterNotFoundError = void 0;
const console = __importStar(require("console"));
const types_common_1 = require("@suites/types.common");
const core_unit_1 = require("@suites/core.unit");
const package_resolver_js_1 = require("./package-resolver.js");
/**
* Thrown when Suites cannot find a compatible adapter package for the DI framework or mocking library.
*
* This occurs when the required adapter package is not installed or cannot be resolved.
* Install the appropriate adapter package to resolve this error.
*
* @since 3.0.0
* @see {@link https://suites.dev/docs/get-started/installation | Installation Guide}
*/
class AdapterNotFoundError extends types_common_1.SuitesError {
constructor(message) {
super(types_common_1.SuitesErrorCode.ADAPTER_NOT_FOUND, 'No compatible adapter found', message);
this.name = 'AdapterNotFoundError';
}
}
exports.AdapterNotFoundError = AdapterNotFoundError;
/**
* Registry of supported mocking library adapter packages.
*
* Maps mocking library names to their corresponding Suites adapter package names.
* Suites automatically detects which library is installed and loads the appropriate adapter.
*
* @since 3.0.0
*/
exports.SuitesDoublesAdapters = {
jest: '@suites/doubles.jest',
sinon: '@suites/doubles.sinon',
vitest: '@suites/doubles.vitest',
bun: '@suites/doubles.bun',
deno: '@suites/doubles.deno',
node: '@suites/doubles.node',
};
/**
* Registry of supported dependency injection framework adapter packages.
*
* Maps DI framework names to their corresponding Suites adapter package names.
* Suites automatically detects which DI framework is installed and loads the appropriate adapter.
*
* @since 3.0.0
*/
exports.SuitesDIAdapters = {
nestjs: '@suites/di.nestjs',
inversify: '@suites/di.inversify',
tsyringe: '@suites/di.tsyringe',
};
/**
* Factory function for creating TestBedBuilder instances with automatic adapter resolution.
*
* This function resolves and configures the appropriate DI and mocking library adapters
* based on what's installed in the project, then creates a builder for the specified target class.
*
* @internal This is used internally by TestBed.solitary() and TestBed.sociable()
* @template TClass The type of the class to be tested
* @param diAdapters - Registry of DI framework adapters
* @param doublesAdapters - Registry of mocking library adapters
* @param targetClass - The class for which to create the test environment
* @returns Factory object with a create method for building TestBedBuilder instances
* @since 3.0.0
*/
function testBedBuilderFactory(diAdapters, doublesAdapters, targetClass) {
return {
create: (testbedBuilderType) => {
const diPackageResolver = (0, package_resolver_js_1.createPackageResolver)(diAdapters);
const diAdapter = diPackageResolver
.resolveCorrespondingAdapter()
.then((adapter) => adapter)
.catch(() => {
throw new AdapterNotFoundError(`It seems that there is an issue with the adapter package needed to integrate Suites
with your dependency injection framework. To resolve this issue, please install the
correct Suites adapter package that is compatible with your dependency injection framework.
For more details, refer to our docs website: https://suites.dev/docs`);
});
const doublesPackageResolver = (0, package_resolver_js_1.createPackageResolver)(doublesAdapters);
const doublesAdapter = doublesPackageResolver
.resolveCorrespondingAdapter()
.then((adapter) => adapter)
.catch(() => {
throw new AdapterNotFoundError(`It seems that there is an issue with the adapter package needed to integrate Suites
with your mocking library. To resolve this issue, please install the
correct Suites adapter package that is compatible with mocking library.
For more details, refer to our docs website: https://suites.dev/docs`);
});
const unitMocker = new core_unit_1.UnitMocker(doublesAdapter.then((adapter) => adapter.mock), diAdapter);
return new testbedBuilderType(doublesAdapter, unitMocker, targetClass, console);
},
};
}
exports.testBedBuilderFactory = testBedBuilderFactory;