Skip to content

Simplify the type of MockActivityEnvironment.run #1711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/test/src/test-mockactivityenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
return 'foo';
}
const result = await new MockActivityEnvironment().run(foo);
// There should be no compile time error on this line
result.startsWith('foo');
t.pass();
});
4 changes: 2 additions & 2 deletions packages/testing/src/mocking-activity-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export class MockActivityEnvironment extends events.EventEmitter {
/**
* Run a function in Activity Context
*/
public async run<P extends any[], R, F extends ActivityFunction<P, R>>(fn: F, ...args: P): Promise<R> {
return this.activity.runNoEncoding(fn as ActivityFunction<any, any>, { args, headers: {} }) as Promise<R>;
public run<F extends ActivityFunction>(fn: F, ...args: Parameters<F>): ReturnType<F> {
return this.activity.runNoEncoding(fn, { args, headers: {} }) as ReturnType<F>;
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/worker/src/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ export class Activity {
});
}

public runNoEncoding(fn: ActivityFunction<any[], any>, input: ActivityExecuteInput): Promise<unknown> {
public runNoEncoding<F extends ActivityFunction>(fn: F, input: ActivityExecuteInput): ReturnType<F> {
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<F>;
}
}

Expand Down
Loading