From eea58df0a755458d29b604058ab50e77466a8523 Mon Sep 17 00:00:00 2001 From: Graham Rogers Date: Thu, 8 May 2025 18:00:39 +0100 Subject: [PATCH 1/3] Simplify the type of `MockActivityEnvironment.run` This improves its type inference, see #1710 --- packages/testing/src/mocking-activity-environment.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/testing/src/mocking-activity-environment.ts b/packages/testing/src/mocking-activity-environment.ts index 8b1cec47b..ccb78f81f 100644 --- a/packages/testing/src/mocking-activity-environment.ts +++ b/packages/testing/src/mocking-activity-environment.ts @@ -54,7 +54,7 @@ export class MockActivityEnvironment extends events.EventEmitter { /** * Run a function in Activity Context */ - public async run

>(fn: F, ...args: P): Promise { + public async run(fn: F, ...args: Parameters): ReturnType { return this.activity.runNoEncoding(fn as ActivityFunction, { args, headers: {} }) as Promise; } } From 1701e50d8dbc18004ede6244c50004517a066332 Mon Sep 17 00:00:00 2001 From: Graham Rogers Date: Fri, 9 May 2025 11:00:29 +0100 Subject: [PATCH 2/3] Fix build --- packages/testing/src/mocking-activity-environment.ts | 4 ++-- packages/worker/src/activity.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/testing/src/mocking-activity-environment.ts b/packages/testing/src/mocking-activity-environment.ts index ccb78f81f..4d88adc22 100644 --- a/packages/testing/src/mocking-activity-environment.ts +++ b/packages/testing/src/mocking-activity-environment.ts @@ -54,8 +54,8 @@ export class MockActivityEnvironment extends events.EventEmitter { /** * Run a function in Activity Context */ - public async run(fn: F, ...args: Parameters): ReturnType { - return this.activity.runNoEncoding(fn as ActivityFunction, { args, headers: {} }) as Promise; + public run(fn: F, ...args: Parameters): ReturnType { + return this.activity.runNoEncoding(fn, { args, headers: {} }) as ReturnType; } } diff --git a/packages/worker/src/activity.ts b/packages/worker/src/activity.ts index 8774dd3aa..3d7720a05 100644 --- a/packages/worker/src/activity.ts +++ b/packages/worker/src/activity.ts @@ -164,9 +164,9 @@ export class Activity { }); } - public runNoEncoding(fn: ActivityFunction, input: ActivityExecuteInput): Promise { + public runNoEncoding(fn: F, input: ActivityExecuteInput): ReturnType { if (this.fn !== undefined) throw new IllegalStateError('Activity function is defined'); - return asyncLocalStorage.run(this.context, () => this.execute(fn, input)); + return asyncLocalStorage.run(this.context, () => this.execute(fn, input)) as ReturnType; } } From ba5976c47e0e4aca12445a4922043bcba6fe640e Mon Sep 17 00:00:00 2001 From: Graham Rogers Date: Fri, 9 May 2025 11:07:15 +0100 Subject: [PATCH 3/3] Add return type test --- packages/test/src/test-mockactivityenv.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/test/src/test-mockactivityenv.ts b/packages/test/src/test-mockactivityenv.ts index 6811cf06b..cfdc2eb35 100644 --- a/packages/test/src/test-mockactivityenv.ts +++ b/packages/test/src/test-mockactivityenv.ts @@ -47,3 +47,13 @@ test('MockActivityEnvironment injects provided info', async (t) => { }, 1); t.is(res, 4); }); + +test('MockActivityEnvironment return type is correctly inferred', async (t) => { + async function foo(): Promise { + return 'foo'; + } + const result = await new MockActivityEnvironment().run(foo); + // There should be no compile time error on this line + result.startsWith('foo'); + t.pass(); +});