Files
TSinjex/src/__tests__/ITSinjex.spec.ts
Max P f7c4e609c2 ### Raise Coverage and Enhance Testing
Increased coverage thresholds to 90% across all metrics. Added exclusions for `.spec.ts` and `.test.ts` files to the coverage configuration. Introduced new test files for `Decorators` and `Functions`, incorporating detailed unit tests for decorators and DI functionalities. Removed outdated and redundant test files, consolidating their functionality into the updated tests. Also added new npm script for jest watch mode. Marked helper and main index files to be ignored by the coverage.
2024-08-16 17:31:55 +02:00

79 lines
2.8 KiB
TypeScript

import { ITSinjex_, ITSinjex } from '../interfaces/ITSinjex';
/**
* Test the implementation of the `ITSinjex` interface.
* @param Container The implementation to test.
* Must implement {@link ITSinjex}, {@link ITSinjex_}
*/
export function test_ITSinjex(Container: ITSinjex_): void {
describe('IDIContainer Implementation Tests', () => {
let container: ITSinjex;
beforeEach(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Container as any)['_instance'] = undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Container as any)['_dependencies'] = undefined;
container = Container.getInstance();
});
it('should register and resolve a dependency', () => {
const identifier = 'myDependency';
const dependency = { value: 42 };
container.register(identifier, dependency);
const resolvedDependency =
container.resolve<typeof dependency>(identifier);
expect(resolvedDependency).toBe(dependency);
});
it('should register and resolve a dependency static', () => {
const identifier = 'myDependency';
const dependency = { value: 42 };
Container.register(identifier, dependency);
const resolvedDependency =
Container.resolve<typeof dependency>(identifier);
expect(resolvedDependency).toBe(dependency);
});
it('should throw an error when resolving a non-registered dependency static', () => {
const identifier = 'nonExistentDependency';
expect(() => Container.resolve<unknown>(identifier)).toThrow();
});
it('should return undefined when resolving a non-registered, non-necessary dependency', () => {
const resolvedDependency = Container.resolve<unknown>(
'nonExistentDependency',
false,
);
expect(resolvedDependency).toBe(undefined);
});
it('should warn when resolving a deprecated dependency', () => {
const identifier = 'deprecatedDependency';
const dependency = { value: 42 };
// Spy on console.warn
const warnSpy = jest
.spyOn(console, 'warn')
.mockImplementation(() => {});
Container.register(identifier, dependency, true);
const resolvedDependency =
Container.resolve<typeof dependency>(identifier);
expect(resolvedDependency).toBe(dependency);
// Expect console.warn to be called
expect(warnSpy).toHaveBeenCalled();
// Restore the original console.warn
warnSpy.mockRestore();
});
});
}