From 929a3eafb1d009d62642f09727b105072005d60e Mon Sep 17 00:00:00 2001 From: Thiago Martins Date: Fri, 8 Jul 2022 11:39:08 -0300 Subject: [PATCH] test(e2e): refactor tests to use constant event payloads instead of manual declaration --- tests/e2e/module-e2e.spec.ts | 28 +++++++++++++--------------- tests/src/constants.ts | 12 ++++++++++++ tests/src/events.producer.ts | 15 ++++++++------- 3 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 tests/src/constants.ts diff --git a/tests/e2e/module-e2e.spec.ts b/tests/e2e/module-e2e.spec.ts index 145fd3ec..caacf58b 100644 --- a/tests/e2e/module-e2e.spec.ts +++ b/tests/e2e/module-e2e.spec.ts @@ -2,6 +2,11 @@ import { INestApplication } from '@nestjs/common'; import { Test } from '@nestjs/testing'; import { EventEmitter2 } from 'eventemitter2'; import { AppModule } from '../src/app.module'; +import { + TEST_EVENT_MULTIPLE_PAYLOAD, + TEST_EVENT_PAYLOAD, + TEST_EVENT_STRING_PAYLOAD, +} from '../src/constants'; import { EventsControllerConsumer } from '../src/events-controller.consumer'; import { EventsProviderPrependConsumer } from '../src/events-provider-prepend.consumer'; import { EventsProviderConsumer } from '../src/events-provider.consumer'; @@ -23,14 +28,14 @@ describe('EventEmitterModule - e2e', () => { const eventsConsumerRef = app.get(EventsProviderConsumer); await app.init(); - expect(eventsConsumerRef.eventPayload).toEqual({ test: 'event' }); + expect(eventsConsumerRef.eventPayload).toEqual(TEST_EVENT_PAYLOAD); }); it(`should emit a "test-event" event to controllers`, async () => { const eventsConsumerRef = app.get(EventsControllerConsumer); await app.init(); - expect(eventsConsumerRef.eventPayload).toEqual({ test: 'event' }); + expect(eventsConsumerRef.eventPayload).toEqual(TEST_EVENT_PAYLOAD); }); it('should be able to specify a consumer be prepended via OnEvent decorator options', async () => { @@ -41,7 +46,7 @@ describe('EventEmitterModule - e2e', () => { ); await app.init(); - expect(eventsConsumerRef.eventPayload).toEqual({ test: 'event' }); + expect(eventsConsumerRef.eventPayload).toEqual(TEST_EVENT_PAYLOAD); expect(prependListenerSpy).toHaveBeenCalled(); }); @@ -65,9 +70,9 @@ describe('EventEmitterModule - e2e', () => { it('should be able to emit a request-scoped event with a single payload', async () => { await app.init(); - expect(EventsProviderRequestScopedConsumer.injectedEventPayload).toEqual({ - test: 'event', - }); + expect(EventsProviderRequestScopedConsumer.injectedEventPayload).toEqual( + TEST_EVENT_PAYLOAD, + ); }); it('should be able to emit a request-scoped event with a string payload', async () => { @@ -75,7 +80,7 @@ describe('EventEmitterModule - e2e', () => { expect( EventsProviderRequestScopedConsumer.injectedEventStringPayload, - ).toEqual('some-string'); + ).toEqual(TEST_EVENT_STRING_PAYLOAD); }); it('should be able to emit a request-scoped event with multiple payloads', async () => { @@ -83,14 +88,7 @@ describe('EventEmitterModule - e2e', () => { expect( EventsProviderRequestScopedConsumer.injectedEventMultiPayload, - ).toEqual([ - { - test: 'event', - }, - { - test2: 'event2', - }, - ]); + ).toEqual(TEST_EVENT_MULTIPLE_PAYLOAD); }); afterEach(async () => { diff --git a/tests/src/constants.ts b/tests/src/constants.ts new file mode 100644 index 00000000..00d95ced --- /dev/null +++ b/tests/src/constants.ts @@ -0,0 +1,12 @@ +export const TEST_EVENT_PAYLOAD = { + test: 'event', +}; + +export const TEST_EVENT_MULTIPLE_PAYLOAD = [ + TEST_EVENT_PAYLOAD, + { + test2: 'event2', + }, +]; + +export const TEST_EVENT_STRING_PAYLOAD = 'some-string'; diff --git a/tests/src/events.producer.ts b/tests/src/events.producer.ts index 672171eb..98423118 100644 --- a/tests/src/events.producer.ts +++ b/tests/src/events.producer.ts @@ -1,17 +1,18 @@ import { Injectable, OnApplicationBootstrap } from '@nestjs/common'; import { EventEmitter2 } from 'eventemitter2'; +import { + TEST_EVENT_MULTIPLE_PAYLOAD, + TEST_EVENT_PAYLOAD, + TEST_EVENT_STRING_PAYLOAD, +} from './constants'; @Injectable() export class EventsProducer implements OnApplicationBootstrap { constructor(private readonly eventEmitter: EventEmitter2) {} onApplicationBootstrap() { - this.eventEmitter.emit('test.event', { test: 'event' }); - this.eventEmitter.emit( - 'multiple.event', - { test: 'event' }, - { test2: 'event2' }, - ); - this.eventEmitter.emit('string.event', 'some-string'); + this.eventEmitter.emit('test.event', TEST_EVENT_PAYLOAD); + this.eventEmitter.emit('multiple.event', TEST_EVENT_MULTIPLE_PAYLOAD); + this.eventEmitter.emit('string.event', TEST_EVENT_STRING_PAYLOAD); } }