Files
privacy.sexy/tests/unit/infrastructure/EnvironmentVariables/EnvironmentVariablesFactory.ts
undergroundwires 4212c7b9e0 Improve context for errors thrown by compiler
This commit introduces a custom error object to provide additional
context for errors throwing during parsing and compiling operations,
improving troubleshooting.

By integrating error context handling, the error messages become more
informative and user-friendly, providing sequence of trace with context
to aid in troubleshooting.

Changes include:

- Introduce custom error object that extends errors with contextual
  information. This replaces previous usages of `AggregateError` which
  is not displayed well by browsers when logged.
- Improve parsing functions to encapsulate error context with more
  details.
- Increase unit test coverage and refactor the related code to be more
  testable.
2024-05-25 13:55:30 +02:00

56 lines
2.0 KiB
TypeScript

import {
describe,
} from 'vitest';
import { itIsSingletonFactory } from '@tests/unit/shared/TestCases/SingletonFactoryTests';
import { EnvironmentVariablesFactory, type EnvironmentVariablesValidator } from '@/infrastructure/EnvironmentVariables/EnvironmentVariablesFactory';
import { ViteEnvironmentVariables } from '@/infrastructure/EnvironmentVariables/Vite/ViteEnvironmentVariables';
import type { IEnvironmentVariables } from '@/infrastructure/EnvironmentVariables/IEnvironmentVariables';
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
describe('EnvironmentVariablesFactory', () => {
describe('instance', () => {
itIsSingletonFactory({
getter: () => EnvironmentVariablesFactory.Current.instance,
expectedType: ViteEnvironmentVariables,
});
});
it('creates the correct type', () => {
// arrange
const sut = new TestableEnvironmentVariablesFactory();
// act
const metadata = sut.instance;
// assert
expect(metadata).to.be.instanceOf(ViteEnvironmentVariables);
});
it('validates its instance', () => {
// arrange
let validatedInstance: IEnvironmentVariables | undefined;
const validatorMock = (instanceToValidate: IEnvironmentVariables) => {
validatedInstance = instanceToValidate;
};
// act
const sut = new TestableEnvironmentVariablesFactory(validatorMock);
const actualInstance = sut.instance;
// assert
expectExists(validatedInstance);
expect(actualInstance).to.equal(validatedInstance);
});
it('throws error if validator fails', () => {
// arrange
const expectedError = 'validator failed';
const failingValidator = () => {
throw new Error(expectedError);
};
// act
const act = () => new TestableEnvironmentVariablesFactory(failingValidator);
// assert
expect(act).to.throw(expectedError);
});
});
class TestableEnvironmentVariablesFactory extends EnvironmentVariablesFactory {
public constructor(validator: EnvironmentVariablesValidator = () => { /* NO OP */ }) {
super(validator);
}
}