From c4bed2d7be839da01cbe73253c2ab0dd75e97bb2 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Fri, 11 Dec 2020 11:12:15 +0100 Subject: [PATCH] feat(testing): Create TestingLogger Useful for testing logging output from e2e tests or unit tests --- packages/testing/src/index.ts | 1 + packages/testing/src/testing-logger.ts | 89 ++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 packages/testing/src/testing-logger.ts diff --git a/packages/testing/src/index.ts b/packages/testing/src/index.ts index 1003f1bba3..cad2796243 100644 --- a/packages/testing/src/index.ts +++ b/packages/testing/src/index.ts @@ -10,4 +10,5 @@ export * from './initializers/test-db-initializer'; export * from './initializers/mysql-initializer'; export * from './initializers/postgres-initializer'; export * from './initializers/sqljs-initializer'; +export * from './testing-logger'; export * from './types'; diff --git a/packages/testing/src/testing-logger.ts b/packages/testing/src/testing-logger.ts new file mode 100644 index 0000000000..094426ae94 --- /dev/null +++ b/packages/testing/src/testing-logger.ts @@ -0,0 +1,89 @@ +import { VendureLogger } from '@vendure/core'; + +/** + * @description + * The TestingLogger can be used in unit tests or e2e tests to make assertions on whether the various + * Logger methods have been called, and which which arguments. + * + * Here's some examples of how to use it in e2e tests and unit tests. In both cases we are using + * the Jest testing framework, but the TestingLogger should work with other similar frameworks + * (e.g. replacing `jest.fn()` with `jasmine.createSpy()`). + * + * @example + * ```TypeScript + * // e2e test example + * import { createTestEnvironment, TestingLogger } from '\@vendure/testing'; + * + * const testingLogger = new TestingLogger(() => jest.fn()); + * + * const { server, adminClient, shopClient } = createTestEnvironment({ + * ...testConfig, + * logger: testingLogger, + * }); + * + * // e2e testing setup omitted + * + * it('should log an error', async () => { + * // The `errorSpy` property exposes the Jest mock function + * testingLogger.errorSpy.mockClear(); + * + * await doSomethingThatErrors(); + * + * expect(testingLogger.errorSpy).toHaveBeenCalled(); + * }); + * ``` + * + * @example + * ```TypeScript + * // unit test example + * import { Test } from '\@nestjs/testing'; + * import { Logger } from '\@vendure/core'; + * import { TestingLogger } from '\@vendure/testing'; + * + * beforeEach(async () => { + * const moduleRef = await Test.createTestingModule({ + * // Nest testing setup omitted + * }).compile(); + * + * Logger.useLogger(testingLogger); + * moduleRef.useLogger(new Logger()); + * } + * ``` + * + * @docsCategory testing + */ +export class TestingLogger any> implements VendureLogger { + constructor(private createSpyFn: () => Spy) { + this.debugSpy = createSpyFn(); + this.errorSpy = createSpyFn(); + this.infoSpy = createSpyFn(); + this.verboseSpy = createSpyFn(); + this.warnSpy = createSpyFn(); + } + + debugSpy: Spy; + errorSpy: Spy; + infoSpy: Spy; + verboseSpy: Spy; + warnSpy: Spy; + + debug(message: string, context?: string): void { + this.debugSpy(message, context); + } + + error(message: string, context?: string, trace?: string): void { + this.errorSpy(message, context, trace); + } + + info(message: string, context?: string): void { + this.infoSpy(message, context); + } + + verbose(message: string, context?: string): void { + this.verboseSpy(message, context); + } + + warn(message: string, context?: string): void { + this.warnSpy(message, context); + } +}