Skip to content

Commit 324db87

Browse files
authored
Refactor integration tests (#1299)
Pure refactoring of the integration tests, done in preparation for implementation of Workflow Update in #1277.
1 parent 81f0b14 commit 324db87

File tree

7 files changed

+142
-110
lines changed

7 files changed

+142
-110
lines changed

packages/test/src/integration-tests.ts renamed to packages/test/src/integration-tests-old.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
/* eslint @typescript-eslint/no-non-null-assertion: 0 */
2+
3+
/**
4+
* Our most recent style of integration tests are those in the
5+
* integration-tests/ directory. This file has been given the suffix -old to
6+
* distinguish the different variants.
7+
*/
28
import path from 'node:path';
39
import v8 from 'node:v8';
410
import { readFileSync } from 'node:fs';
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { randomUUID } from 'crypto';
2+
import { ExecutionContext, TestFn } from 'ava';
3+
import { WorkflowHandle, WorkflowStartOptions } from '@temporalio/client';
4+
import {
5+
LocalTestWorkflowEnvironmentOptions,
6+
TestWorkflowEnvironment,
7+
workflowInterceptorModules as defaultWorkflowInterceptorModules,
8+
} from '@temporalio/testing';
9+
import {
10+
DefaultLogger,
11+
LogLevel,
12+
Runtime,
13+
WorkerOptions,
14+
WorkflowBundle,
15+
bundleWorkflowCode,
16+
} from '@temporalio/worker';
17+
import * as workflow from '@temporalio/workflow';
18+
import { ConnectionInjectorInterceptor } from '../activities/interceptors';
19+
import { Worker, test as anyTest, bundlerOptions } from '../helpers';
20+
21+
export interface Context {
22+
env: TestWorkflowEnvironment;
23+
workflowBundle: WorkflowBundle;
24+
}
25+
26+
export function makeTestFunction(opts: {
27+
workflowsPath: string;
28+
workflowEnvironmentOpts?: LocalTestWorkflowEnvironmentOptions;
29+
workflowInterceptorModules?: string[];
30+
}): TestFn<Context> {
31+
const test = anyTest as TestFn<Context>;
32+
test.before(async (t) => {
33+
// Ignore invalid log levels
34+
Runtime.install({ logger: new DefaultLogger((process.env.TEST_LOG_LEVEL || 'DEBUG').toUpperCase() as LogLevel) });
35+
const env = await TestWorkflowEnvironment.createLocal(opts.workflowEnvironmentOpts);
36+
const workflowBundle = await bundleWorkflowCode({
37+
...bundlerOptions,
38+
workflowInterceptorModules: [...defaultWorkflowInterceptorModules, ...(opts.workflowInterceptorModules ?? [])],
39+
workflowsPath: opts.workflowsPath,
40+
});
41+
t.context = {
42+
env,
43+
workflowBundle,
44+
};
45+
});
46+
test.after.always(async (t) => {
47+
await t.context.env.teardown();
48+
});
49+
return test;
50+
}
51+
52+
export interface Helpers {
53+
taskQueue: string;
54+
createWorker(opts?: Partial<WorkerOptions>): Promise<Worker>;
55+
executeWorkflow<T extends () => Promise<any>>(workflowType: T): Promise<workflow.WorkflowResultType<T>>;
56+
executeWorkflow<T extends workflow.Workflow>(
57+
fn: T,
58+
opts: Omit<WorkflowStartOptions<T>, 'taskQueue' | 'workflowId'>
59+
): Promise<workflow.WorkflowResultType<T>>;
60+
startWorkflow<T extends () => Promise<any>>(workflowType: T): Promise<WorkflowHandle<T>>;
61+
startWorkflow<T extends workflow.Workflow>(
62+
fn: T,
63+
opts: Omit<WorkflowStartOptions<T>, 'taskQueue' | 'workflowId'>
64+
): Promise<WorkflowHandle<T>>;
65+
}
66+
67+
export function helpers(t: ExecutionContext<Context>): Helpers {
68+
const taskQueue = t.title.replace(/ /g, '_');
69+
70+
return {
71+
taskQueue,
72+
async createWorker(opts?: Partial<WorkerOptions>): Promise<Worker> {
73+
return await Worker.create({
74+
connection: t.context.env.nativeConnection,
75+
workflowBundle: t.context.workflowBundle,
76+
taskQueue,
77+
interceptors: {
78+
activity: [() => ({ inbound: new ConnectionInjectorInterceptor(t.context.env.connection) })],
79+
},
80+
showStackTraceSources: true,
81+
...opts,
82+
});
83+
},
84+
async executeWorkflow(
85+
fn: workflow.Workflow,
86+
opts?: Omit<WorkflowStartOptions, 'taskQueue' | 'workflowId'>
87+
): Promise<any> {
88+
return await t.context.env.client.workflow.execute(fn, {
89+
taskQueue,
90+
workflowId: randomUUID(),
91+
...opts,
92+
});
93+
},
94+
async startWorkflow(
95+
fn: workflow.Workflow,
96+
opts?: Omit<WorkflowStartOptions, 'taskQueue' | 'workflowId'>
97+
): Promise<WorkflowHandle<workflow.Workflow>> {
98+
return await t.context.env.client.workflow.start(fn, {
99+
taskQueue,
100+
workflowId: randomUUID(),
101+
...opts,
102+
});
103+
},
104+
};
105+
}

packages/test/src/test-integration-workflows.ts renamed to packages/test/src/integration-tests/test-misc.ts

Lines changed: 7 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,14 @@
1-
import { randomUUID } from 'crypto';
2-
import { ExecutionContext, TestFn } from 'ava';
31
import { firstValueFrom, Subject } from 'rxjs';
4-
import { WorkflowFailedError, WorkflowHandle, WorkflowStartOptions } from '@temporalio/client';
5-
import { TestWorkflowEnvironment, workflowInterceptorModules } from '@temporalio/testing';
6-
import {
7-
bundleWorkflowCode,
8-
DefaultLogger,
9-
LogLevel,
10-
Runtime,
11-
WorkerOptions,
12-
WorkflowBundle,
13-
} from '@temporalio/worker';
2+
import { WorkflowFailedError } from '@temporalio/client';
143
import * as activity from '@temporalio/activity';
15-
import * as workflow from '@temporalio/workflow';
16-
import { CancelReason } from '@temporalio/worker/lib/activity';
174
import { tsToMs } from '@temporalio/common/lib/time';
18-
import { test as anyTest, bundlerOptions, Worker } from './helpers';
19-
import { activityStartedSignal } from './workflows/definitions';
20-
import { signalSchedulingWorkflow } from './activities/helpers';
21-
import { ConnectionInjectorInterceptor } from './activities/interceptors';
22-
23-
interface Context {
24-
env: TestWorkflowEnvironment;
25-
workflowBundle: WorkflowBundle;
26-
}
27-
28-
const test = anyTest as TestFn<Context>;
29-
30-
interface Helpers {
31-
taskQueue: string;
32-
createWorker(opts?: Partial<WorkerOptions>): Promise<Worker>;
33-
executeWorkflow<T extends () => Promise<any>>(workflowType: T): Promise<workflow.WorkflowResultType<T>>;
34-
executeWorkflow<T extends workflow.Workflow>(
35-
fn: T,
36-
opts: Omit<WorkflowStartOptions<T>, 'taskQueue' | 'workflowId'>
37-
): Promise<workflow.WorkflowResultType<T>>;
38-
startWorkflow<T extends () => Promise<any>>(workflowType: T): Promise<WorkflowHandle<T>>;
39-
startWorkflow<T extends workflow.Workflow>(
40-
fn: T,
41-
opts: Omit<WorkflowStartOptions<T>, 'taskQueue' | 'workflowId'>
42-
): Promise<WorkflowHandle<T>>;
43-
}
44-
45-
function helpers(t: ExecutionContext<Context>): Helpers {
46-
const taskQueue = t.title.replace(/ /g, '_');
47-
48-
return {
49-
taskQueue,
50-
async createWorker(opts?: Partial<WorkerOptions>): Promise<Worker> {
51-
return await Worker.create({
52-
connection: t.context.env.nativeConnection,
53-
workflowBundle: t.context.workflowBundle,
54-
taskQueue,
55-
interceptors: {
56-
activity: [() => ({ inbound: new ConnectionInjectorInterceptor(t.context.env.connection) })],
57-
},
58-
showStackTraceSources: true,
59-
...opts,
60-
});
61-
},
62-
async executeWorkflow(
63-
fn: workflow.Workflow,
64-
opts?: Omit<WorkflowStartOptions, 'taskQueue' | 'workflowId'>
65-
): Promise<any> {
66-
return await t.context.env.client.workflow.execute(fn, {
67-
taskQueue,
68-
workflowId: randomUUID(),
69-
...opts,
70-
});
71-
},
72-
async startWorkflow(
73-
fn: workflow.Workflow,
74-
opts?: Omit<WorkflowStartOptions, 'taskQueue' | 'workflowId'>
75-
): Promise<WorkflowHandle<workflow.Workflow>> {
76-
return await t.context.env.client.workflow.start(fn, {
77-
taskQueue,
78-
workflowId: randomUUID(),
79-
...opts,
80-
});
81-
},
82-
};
83-
}
84-
85-
test.before(async (t) => {
86-
// Ignore invalid log levels
87-
Runtime.install({ logger: new DefaultLogger((process.env.TEST_LOG_LEVEL || 'DEBUG').toUpperCase() as LogLevel) });
88-
const env = await TestWorkflowEnvironment.createLocal();
89-
const workflowBundle = await bundleWorkflowCode({
90-
...bundlerOptions,
91-
workflowInterceptorModules,
92-
workflowsPath: __filename,
93-
});
94-
t.context = {
95-
env,
96-
workflowBundle,
97-
};
98-
});
5+
import { CancelReason } from '@temporalio/worker/lib/activity';
6+
import * as workflow from '@temporalio/workflow';
7+
import { signalSchedulingWorkflow } from '../activities/helpers';
8+
import { activityStartedSignal } from '../workflows/definitions';
9+
import { helpers, makeTestFunction } from './helpers';
9910

100-
test.after.always(async (t) => {
101-
await t.context.env.teardown();
102-
});
11+
const test = makeTestFunction({ workflowsPath: __filename });
10312

10413
export async function parent(): Promise<void> {
10514
await workflow.startChild(child, { workflowId: 'child' });
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Our most recent style of integration tests are those in the
3+
* integration-tests/ directory. This file has been given the suffix -old to
4+
* distinguish the different variants.
5+
*/
6+
7+
import { RUN_INTEGRATION_TESTS, ByteSkewerPayloadCodec } from './helpers';
8+
import { runIntegrationTests } from './integration-tests-old';
9+
10+
if (RUN_INTEGRATION_TESTS) {
11+
runIntegrationTests(new ByteSkewerPayloadCodec());
12+
}

packages/test/src/test-integration-codec.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Our most recent style of integration tests are those in the
3+
* integration-tests/ directory. This file has been given the suffix -old to
4+
* distinguish the different variants.
5+
*/
6+
7+
import { RUN_INTEGRATION_TESTS } from './helpers';
8+
import { runIntegrationTests } from './integration-tests-old';
9+
10+
if (RUN_INTEGRATION_TESTS) {
11+
runIntegrationTests();
12+
}

packages/test/src/test-integration.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)