This commit increases strictnes of tests by failing on tests (even though they pass) if `console.warn` or `console.error` is used. This is used to fix warning outputs from Vue, cleaning up test output and preventing potential issues with tests. This commit fixes all of the failing tests, including refactoring in code to make them more testable through injecting Vue lifecycle hook function stubs. This removes `shallowMount`ing done on places, improving the speed of executing unit tests. It also reduces complexity and increases maintainability by removing `@vue/test-utils` dependency for these tests. Changes: - Register global hook for all tests to fail if console.error or console.warn is being used. - Fix all issues with failing tests. - Create test helper function for running code in a wrapper component to run code in reliable/unified way to surpress Vue warnings about code not running inside `setup`.
126 lines
4.4 KiB
TypeScript
126 lines
4.4 KiB
TypeScript
import { describe } from 'vitest';
|
|
import { VueDependencyInjectionApiStub } from '@tests/unit/shared/Stubs/VueDependencyInjectionApiStub';
|
|
import { InjectionKeys } from '@/presentation/injectionSymbols';
|
|
import { provideDependencies, type VueDependencyInjectionApi } from '@/presentation/bootstrapping/DependencyProvider';
|
|
import { ApplicationContextStub } from '@tests/unit/shared/Stubs/ApplicationContextStub';
|
|
import { itIsSingletonFactory } from '@tests/unit/shared/TestCases/SingletonFactoryTests';
|
|
import type { IApplicationContext } from '@/application/Context/IApplicationContext';
|
|
import { itIsTransientFactory } from '@tests/unit/shared/TestCases/TransientFactoryTests';
|
|
import { executeInComponentSetupContext } from '@tests/shared/Vue/ExecuteInComponentSetupContext';
|
|
|
|
describe('DependencyProvider', () => {
|
|
describe('provideDependencies', () => {
|
|
const testCases: {
|
|
readonly [K in keyof typeof InjectionKeys]: (injectionKey: symbol) => void;
|
|
} = {
|
|
useCollectionState: createTransientTests(),
|
|
useApplication: createSingletonTests(),
|
|
useRuntimeEnvironment: createSingletonTests(),
|
|
useAutoUnsubscribedEvents: createTransientTests(),
|
|
useClipboard: createTransientTests(),
|
|
useCurrentCode: createTransientTests(),
|
|
useUserSelectionState: createTransientTests(),
|
|
useLogger: createTransientTests(),
|
|
useCodeRunner: createTransientTests(),
|
|
useDialog: createTransientTests(),
|
|
useScriptDiagnosticsCollector: createTransientTests(),
|
|
useAutoUnsubscribedEventListener: createTransientTests(),
|
|
};
|
|
Object.entries(testCases).forEach(([key, runTests]) => {
|
|
const registeredKey = InjectionKeys[key].key;
|
|
describe(`Key: "${registeredKey.toString()}"`, () => {
|
|
runTests(registeredKey);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
function createTransientTests() {
|
|
return (injectionKey: symbol) => {
|
|
it('should register a function when transient dependency is resolved', () => {
|
|
// arrange
|
|
const api = new VueDependencyInjectionApiStub();
|
|
// act
|
|
new ProvideDependenciesBuilder()
|
|
.withApi(api)
|
|
.provideDependencies();
|
|
// expect
|
|
const registeredObject = api.inject(injectionKey);
|
|
expect(registeredObject).to.be.instanceOf(Function);
|
|
});
|
|
describe('should return different instances for transient dependency', () => {
|
|
// arrange
|
|
const api = new VueDependencyInjectionApiStub();
|
|
new ProvideDependenciesBuilder()
|
|
.withApi(api)
|
|
.provideDependencies();
|
|
// act
|
|
const getFactoryResult = () => {
|
|
const registeredFactory = api.inject(injectionKey) as () => unknown;
|
|
let factoryResult: unknown;
|
|
executeInComponentSetupContext({
|
|
setupCallback: () => {
|
|
factoryResult = registeredFactory();
|
|
},
|
|
});
|
|
return factoryResult;
|
|
};
|
|
// assert
|
|
itIsTransientFactory({
|
|
getter: getFactoryResult,
|
|
});
|
|
});
|
|
};
|
|
}
|
|
|
|
function createSingletonTests() {
|
|
return (injectionKey: symbol) => {
|
|
it('should register an object when singleton dependency is resolved', () => {
|
|
// arrange
|
|
const api = new VueDependencyInjectionApiStub();
|
|
// act
|
|
new ProvideDependenciesBuilder()
|
|
.withApi(api)
|
|
.provideDependencies();
|
|
// expect
|
|
const registeredObject = api.inject(injectionKey);
|
|
expect(registeredObject).to.be.instanceOf(Object);
|
|
});
|
|
describe('should return the same instance for singleton dependency', () => {
|
|
// arrange
|
|
const singletonContext = new ApplicationContextStub();
|
|
const api = new VueDependencyInjectionApiStub();
|
|
new ProvideDependenciesBuilder()
|
|
.withContext(singletonContext)
|
|
.withApi(api)
|
|
.provideDependencies();
|
|
// act
|
|
const getRegisteredInstance = () => api.inject(injectionKey);
|
|
// assert
|
|
itIsSingletonFactory({
|
|
getter: getRegisteredInstance,
|
|
});
|
|
});
|
|
};
|
|
}
|
|
|
|
class ProvideDependenciesBuilder {
|
|
private context: IApplicationContext = new ApplicationContextStub();
|
|
|
|
private api: VueDependencyInjectionApi = new VueDependencyInjectionApiStub();
|
|
|
|
public withApi(api: VueDependencyInjectionApi): this {
|
|
this.api = api;
|
|
return this;
|
|
}
|
|
|
|
public withContext(context: IApplicationContext): this {
|
|
this.context = context;
|
|
return this;
|
|
}
|
|
|
|
public provideDependencies() {
|
|
return provideDependencies(this.context, this.api);
|
|
}
|
|
}
|