What is @temporalio/testing?
@temporalio/testing is a package designed to facilitate the testing of Temporal workflows and activities. It provides tools to create test environments, mock activities, and validate workflow executions, ensuring that your Temporal-based applications behave as expected.
What are @temporalio/testing's main functionalities?
Test Environment Setup
This feature allows you to set up a test environment for running Temporal workflows. The `TestWorkflowEnvironment.create()` method initializes the environment, making it possible to run and test workflows in isolation.
const { TestWorkflowEnvironment } = require('@temporalio/testing');
async function setupTestEnv() {
const testEnv = await TestWorkflowEnvironment.create();
return testEnv;
}
setupTestEnv().then(env => console.log('Test environment setup complete.')).catch(err => console.error(err));
Mock Activities
This feature allows you to mock activities within your workflows. By providing mock implementations of activities, you can test workflows without relying on the actual activity implementations, making tests faster and more reliable.
const { TestWorkflowEnvironment } = require('@temporalio/testing');
const { Worker } = require('@temporalio/worker');
async function mockActivities() {
const testEnv = await TestWorkflowEnvironment.create();
const worker = await Worker.create({
activities: {
myActivity: async () => 'mocked result'
},
taskQueue: 'test-task-queue'
});
await worker.run();
}
mockActivities().then(() => console.log('Activities mocked.')).catch(err => console.error(err));
Workflow Execution Validation
This feature allows you to validate the execution of workflows. By using the `WorkflowClient` to execute workflows within the test environment, you can ensure that workflows produce the expected results.
const { TestWorkflowEnvironment } = require('@temporalio/testing');
const { Worker, WorkflowClient } = require('@temporalio/client');
async function validateWorkflowExecution() {
const testEnv = await TestWorkflowEnvironment.create();
const client = new WorkflowClient();
const result = await client.execute('myWorkflow', { taskQueue: 'test-task-queue' });
console.log('Workflow result:', result);
}
validateWorkflowExecution().then(() => console.log('Workflow execution validated.')).catch(err => console.error(err));
Other packages similar to @temporalio/testing
jest
Jest is a popular testing framework for JavaScript applications. It provides a comprehensive set of tools for writing and running tests, including mocking, assertions, and snapshot testing. While Jest is not specific to Temporal workflows, it can be used in conjunction with @temporalio/testing to provide a complete testing solution.
mocha
Mocha is a flexible JavaScript test framework that runs on Node.js and in the browser. It provides a simple way to structure tests and includes features like asynchronous testing, test coverage reports, and customizable reporters. Mocha can be used to test Temporal workflows by integrating with @temporalio/testing.
sinon
Sinon is a standalone test spy, stub, and mock library for JavaScript. It works with any testing framework and provides powerful tools for mocking and spying on functions. Sinon can be used to mock activities and other dependencies in Temporal workflows, complementing the features provided by @temporalio/testing.