From ef844c3839ae421346cfe110910c5145da3a5d9d Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Wed, 1 Nov 2023 19:21:49 +0100 Subject: [PATCH 1/3] failing tests --- .../tests/execution-context.spec.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/packages/graphql-modules/tests/execution-context.spec.ts b/packages/graphql-modules/tests/execution-context.spec.ts index 5804d22db1..d1d0d6ebea 100644 --- a/packages/graphql-modules/tests/execution-context.spec.ts +++ b/packages/graphql-modules/tests/execution-context.spec.ts @@ -10,6 +10,7 @@ import { testkit, } from '../src'; +import { executionContext } from '../src/application/execution-context'; import { getExecutionContextDependencyStore, getExecutionContextStore, @@ -510,3 +511,42 @@ test('accessing a singleton provider with execution context in another singleton getDependencyName: expectedName, }); }); + +it('should provide context when created in separate async execution within same stack', async () => { + const create = async () => + executionContext.create({ + getApplicationContext() { + return 'app' as any; + }, + getModuleContext() { + return 'mod' as any; + }, + }); + const destroy = await create(); + + expect(executionContext.getApplicationContext()).toBe('app'); + expect(executionContext.getModuleContext('')).toBe('mod'); + + destroy(); +}); + +it('should provide nested contexts', async () => { + const createPicker = (i: number) => ({ + getApplicationContext() { + return ('app ' + i) as any; + }, + getModuleContext() { + return ('mod ' + i) as any; + }, + }); + + const destroy0 = executionContext.create(createPicker(0)); + expect(executionContext.getApplicationContext()).toBe('app 0'); + + const destroy1 = executionContext.create(createPicker(1)); + expect(executionContext.getApplicationContext()).toBe('app 1'); + destroy1(); + + expect(executionContext.getApplicationContext()).toBe('app 0'); + destroy0(); +}); From 88bc5c78833cab113192d98f531fb21ec099831e Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Thu, 2 Nov 2023 11:43:52 +0100 Subject: [PATCH 2/3] nested async execution --- .../tests/execution-context.spec.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/graphql-modules/tests/execution-context.spec.ts b/packages/graphql-modules/tests/execution-context.spec.ts index d1d0d6ebea..454957144e 100644 --- a/packages/graphql-modules/tests/execution-context.spec.ts +++ b/packages/graphql-modules/tests/execution-context.spec.ts @@ -530,6 +530,27 @@ it('should provide context when created in separate async execution within same destroy(); }); +it('should provide context in nested async execution', async () => { + let destroy = () => { + // noop + }; + await new Promise((resolve) => { + destroy = executionContext.create({ + getApplicationContext() { + return 'app' as any; + }, + getModuleContext() { + return 'mod' as any; + }, + }); + resolve(); + }).then(() => { + expect(executionContext.getApplicationContext()).toBe('app'); + expect(executionContext.getModuleContext('')).toBe('mod'); + }); + destroy(); +}); + it('should provide nested contexts', async () => { const createPicker = (i: number) => ({ getApplicationContext() { From 9dee25bc22d2f9d098abd9fb48e280b5f323919f Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Thu, 2 Nov 2023 12:06:51 +0100 Subject: [PATCH 3/3] use finally --- .../graphql-modules/tests/execution-context.spec.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/graphql-modules/tests/execution-context.spec.ts b/packages/graphql-modules/tests/execution-context.spec.ts index 454957144e..b5349ef127 100644 --- a/packages/graphql-modules/tests/execution-context.spec.ts +++ b/packages/graphql-modules/tests/execution-context.spec.ts @@ -544,11 +544,14 @@ it('should provide context in nested async execution', async () => { }, }); resolve(); - }).then(() => { - expect(executionContext.getApplicationContext()).toBe('app'); - expect(executionContext.getModuleContext('')).toBe('mod'); - }); - destroy(); + }) + .then(() => { + expect(executionContext.getApplicationContext()).toBe('app'); + expect(executionContext.getModuleContext('')).toBe('mod'); + }) + .finally(() => { + destroy(); + }); }); it('should provide nested contexts', async () => {