Class: TestWorkflowEnvironment
An execution environment for running Workflow integration tests.
Runs an external server. By default, the Java test server is used which supports time skipping.
Properties
address
readonlyaddress:string
Address used when constructing connection and nativeConnection
asyncCompletionClient
readonlyasyncCompletionClient:AsyncCompletionClient
An AsyncCompletionClient for interacting with the test server
Deprecated
- use
client.activityinstead
client
readonlyclient:Client
A TimeSkippingClient for interacting with the ephemeral server
connection
readonlyconnection:Connection
Get an established Connection to the ephemeral server
connectionOptions
readonlyconnectionOptions:ExistingServerConnectionOptions
Connection options used when constructing connection and nativeConnection.
namespace?
readonlyoptionalnamespace?:string
Namespace used in this environment (taken from TestWorkflowEnvironmentOptions)
nativeConnection
readonlynativeConnection:NativeConnection
A NativeConnection for interacting with the test server.
Use this connection when creating Workers for testing.
options
readonlyoptions:TestWorkflowEnvironmentOptionsWithDefaults
sleep
sleep: (
durationMs) =>Promise<void>
Wait for durationMs in "server time".
This awaits using regular setTimeout in regular environments or manually skips time in time-skipping environments.
Useful for simulating events far into the future like completion of long running activities.
Time skippping:
The time skippping server toggles between skipped time and normal time depending on what it needs to execute.
This method is likely to resolve in less than durationMs of "real time".
Parameters
durationMs
number of milliseconds or ms-formatted string
Returns
Promise<void>
Example
workflow.ts
const activities = proxyActivities({ startToCloseTimeout: 2_000_000 });
export async function raceActivityAndTimer(): Promise<string> {
return await Promise.race([
wf.sleep(500_000).then(() => 'timer'),
activities.longRunning().then(() => 'activity'),
]);
}
test.ts
const worker = await Worker.create({
connection: testEnv.nativeConnection,
activities: {
async longRunning() {
await testEnv.sleep(1_000_000); // <-- sleep called here
},
},
// ...
});
supportsTimeSkipping
readonlysupportsTimeSkipping:boolean
workflowClient
readonlyworkflowClient:WorkflowClient
A TimeSkippingWorkflowClient for interacting with the test server
Deprecated
- use
client.workflowinstead
Methods
createNexusEndpoint()
createNexusEndpoint(
name,taskQueue):Promise<NexusEndpointIdentifier>
Create a Nexus endpoint targeting a worker task queue.
This is a convenience method that wraps connection.operatorService.createNexusEndpoint for easier
testing of Nexus services.
Parameters
name
string
The name of the Nexus endpoint
taskQueue
string
The task queue that will handle Nexus operations
Returns
Promise<NexusEndpointIdentifier>
The created Nexus endpoint
Example
const endpoint = await testEnv.createNexusEndpoint('my-endpoint', 'my-task-queue');
const endpointId = endpoint.id;
currentTimeMs()
currentTimeMs():
Promise<number>
Get the current time known to this environment.
For non-time-skipping environments this is simply the system time. For time-skipping environments this is whatever time has been skipped to.
Returns
Promise<number>
deleteNexusEndpoint()
deleteNexusEndpoint(
endpoint):Promise<void>
Delete a Nexus endpoint.
This is a convenience method that wraps connection.operatorService.deleteNexusEndpoint for easier
testing of Nexus services.
Parameters
endpoint
Pick<NexusEndpointIdentifier, "id" | "version">
The endpoint to delete (can pass the full endpoint object or just an object with id and version)
Returns
Promise<void>
Example
const endpoint = await testEnv.createNexusEndpoint('my-endpoint', 'my-task-queue');
// ... use the endpoint ...
await testEnv.deleteNexusEndpoint(endpoint);
teardown()
teardown():
Promise<void>
Kill the test server process and close the connection to it
Returns
Promise<void>
createFromExistingServer()
staticcreateFromExistingServer(opts?):Promise<TestWorkflowEnvironment>
Create a new test environment using an existing server. You must already be running a server, which the test environment will connect to.