From 772c3f668282247148ce97ad915f34019f33cc10 Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Mon, 26 Aug 2019 11:14:08 +0700 Subject: [PATCH] Use real cls-hooked in tests instead of NamespaceMock https://github.com/Jeff-Lewis/cls-hooked/issues/17#issuecomment-510168720 Note: uses undocumented methods ns.enter(context) and ns.exit(context) --- packages/server/src/test-utils/TestUtils.ts | 7 ++++++- packages/server/src/test.ts | 5 +++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/server/src/test-utils/TestUtils.ts b/packages/server/src/test-utils/TestUtils.ts index 350d5f3..c0689e3 100644 --- a/packages/server/src/test-utils/TestUtils.ts +++ b/packages/server/src/test-utils/TestUtils.ts @@ -39,7 +39,11 @@ export class TestUtils { let connection!: Connection let queryRunner: QueryRunner + let context: any + beforeEach(async () => { + context = namespace.createContext(); + (namespace as any).enter(context) connection = await database.connect() queryRunner = connection.createQueryRunner() await queryRunner.connect() @@ -56,7 +60,8 @@ export class TestUtils { await queryRunner.release() namespace.set(CORRELATION_ID, undefined) namespace.set(ENTITY_MANAGER, undefined) - await connection.close() + await connection.close(); + (namespace as any).exit(context) }) } diff --git a/packages/server/src/test.ts b/packages/server/src/test.ts index ac65b1c..3b1070c 100644 --- a/packages/server/src/test.ts +++ b/packages/server/src/test.ts @@ -1,13 +1,14 @@ import {Bootstrap} from './application/Bootstrap' import {IAPIDef} from '@rondo.dev/common' -import {NamespaceMock, TestUtils} from './test-utils' +import {TestUtils} from './test-utils' import {config} from './config' +import {createNamespace} from 'cls-hooked' export const exit = jest.fn() const bootstrap = new Bootstrap( config, - new NamespaceMock(), + createNamespace('test'), exit, )