Ensure tests do not log warning or errors

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`.
This commit is contained in:
undergroundwires
2024-07-23 16:08:04 +02:00
parent a6505587bf
commit ae0165f1fe
26 changed files with 359 additions and 205 deletions

View File

@@ -1,43 +1,38 @@
import { describe, it, expect } from 'vitest';
import { shallowMount } from '@vue/test-utils';
import { useAutoUnsubscribedEvents } from '@/presentation/components/Shared/Hooks/UseAutoUnsubscribedEvents';
import { EventSubscriptionCollectionStub } from '@tests/unit/shared/Stubs/EventSubscriptionCollectionStub';
import { EventSubscriptionStub } from '@tests/unit/shared/Stubs/EventSubscriptionStub';
import { EventSubscriptionCollection } from '@/infrastructure/Events/EventSubscriptionCollection';
import type { FunctionKeys } from '@/TypeHelpers';
import type { LifecycleHook } from '@/presentation/components/Shared/Hooks/Common/LifecycleHook';
import { LifecycleHookStub } from '@tests/unit/shared/Stubs/LifecycleHookStub';
import type { IEventSubscriptionCollection } from '@/infrastructure/Events/IEventSubscriptionCollection';
describe('UseAutoUnsubscribedEvents', () => {
describe('event collection handling', () => {
it('returns the provided event collection when initialized', () => {
// arrange
const expectedEvents = new EventSubscriptionCollectionStub();
const context = new TestContext()
.withEvents(expectedEvents);
// act
const { events: actualEvents } = useAutoUnsubscribedEvents(expectedEvents);
const { events: actualEvents } = context.use();
// assert
expect(actualEvents).to.equal(expectedEvents);
});
it('uses a default event collection when none is provided during initialization', () => {
// arrange
const expectedType = EventSubscriptionCollection;
// act
const { events: actualEvents } = useAutoUnsubscribedEvents();
// assert
expect(actualEvents).to.be.instanceOf(expectedType);
});
it('throws error when there are existing subscriptions', () => {
// arrange
const expectedError = 'there are existing subscriptions, this may lead to side-effects';
const events = new EventSubscriptionCollectionStub();
events.register([new EventSubscriptionStub(), new EventSubscriptionStub()]);
const context = new TestContext()
.withEvents(events);
// act
const act = () => useAutoUnsubscribedEvents(events);
const act = () => context.use();
// assert
expect(act).to.throw(expectedError);
@@ -48,21 +43,44 @@ describe('UseAutoUnsubscribedEvents', () => {
// arrange
const events = new EventSubscriptionCollectionStub();
const expectedCall: FunctionKeys<EventSubscriptionCollection> = 'unsubscribeAll';
const stubComponent = shallowMount({
setup() {
useAutoUnsubscribedEvents(events);
events.register([new EventSubscriptionStub(), new EventSubscriptionStub()]);
},
template: '<div></div>',
});
events.callHistory.length = 0;
const onTeardown = new LifecycleHookStub();
const context = new TestContext()
.withEvents(events)
.withOnTeardown(onTeardown.getHook());
// act
stubComponent.unmount();
context.use();
events.register([new EventSubscriptionStub(), new EventSubscriptionStub()]);
events.callHistory.length = 0;
onTeardown.executeAllCallbacks();
// assert
expect(onTeardown.totalRegisteredCallbacks).to.be.greaterThan(0);
expect(events.callHistory).to.have.lengthOf(1);
expect(events.callHistory[0].methodName).to.equal(expectedCall);
});
});
});
class TestContext {
private onTeardown: LifecycleHook = new LifecycleHookStub().getHook();
private events: IEventSubscriptionCollection = new EventSubscriptionCollectionStub();
public withOnTeardown(onTeardown: LifecycleHook): this {
this.onTeardown = onTeardown;
return this;
}
public withEvents(events: IEventSubscriptionCollection): this {
this.events = events;
return this;
}
public use(): ReturnType<typeof useAutoUnsubscribedEvents> {
return useAutoUnsubscribedEvents(
this.events,
this.onTeardown,
);
}
}