Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
@temporalio/testing
Advanced tools
@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.
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));
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 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 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.
FAQs
Temporal.io SDK Testing sub-package
We found that @temporalio/testing demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.