Testing Strategy
Relevant source files
The following files were used as context for generating this wiki page:
- packages/frontend/editor-ui/src/components/WhatsNewModal.test.ts
- packages/frontend/editor-ui/src/components/VirtualSchema.test.ts
- packages/frontend/editor-ui/src/tests/mocks.ts
- packages/@n8n/config/test/config.test.ts
- packages/nodes-base/nodes/HtmlExtract/test/workflow.dotNotation.json
- packages/@n8n/benchmark/src/test-execution/test-report.ts
- packages/frontend/editor-ui/src/components/CommunityPackageManageConfirmModal.test.ts
- packages/frontend/editor-ui/src/components/SourceControlPushModal.ee.test.ts
The n8n project employs a multi-layered testing strategy to ensure the quality, stability, and performance of the application. This strategy encompasses frontend component testing, backend configuration validation, full workflow execution tests, and performance benchmarking. By utilizing a combination of unit, integration, and end-to-end testing paradigms, the project maintains a high degree of confidence in its codebase.
The testing ecosystem relies on frameworks like Vitest and Vue Testing Library for the frontend, Jest for backend modules, and a custom benchmarking setup using k6. A significant emphasis is placed on mocking, test utilities, and data-driven tests to create isolated, repeatable, and comprehensive test scenarios.
Frontend Component Testing
Frontend testing focuses on individual Vue components to verify their rendering, behavior, and interaction with the application's state management.
Sources: packages/frontend/editor-ui/src/components/WhatsNewModal.test.ts, packages/frontend/editor-ui/src/components/VirtualSchema.test.ts, packages/frontend/editor-ui/src/components/CommunityPackageManageConfirmModal.test.ts
Frameworks and Libraries
- Vitest: The primary test runner and assertion library. It provides a modern, fast testing experience.
- Vue Testing Library: Used for rendering components and querying the DOM in a way that resembles how a user would interact with them.
user-event: A companion library for Vue Testing Library to simulate realistic user interactions like clicks and typing.- Pinia: The state management library for Vue. The
@pinia/testingpackage is used to mock stores and control application state during tests.
Test Structure and Flow
A typical component test involves setting up a clean environment, rendering the component with the necessary props and state, simulating user actions, and asserting the expected outcomes.
The diagram below illustrates the lifecycle of a frontend component test.
This diagram shows the setup, execution, and teardown phases of a typical Vue component test.
Sources: packages/frontend/editor-ui/src/components/WhatsNewModal.test.ts:68-79,150-152
Key Practices
- Mocking Composables and Modules:
vi.mockis used extensively to isolate components from their dependencies, such as composables for telemetry or page redirection.
Sources: packages/frontend/editor-ui/src/components/WhatsNewModal.test.ts:18-36 - State Management: The
createTestingPiniafunction initializes a mock Pinia store. Individual stores are then mocked to control their state and spy on their actions (e.g.,uiStore.openModal).
Sources: packages/frontend/editor-ui/src/components/WhatsNewModal.test.ts:70,71,192 - Asynchronous Operations: Tests frequently use
async/awaitwithwaitForto handle asynchronous updates to the DOM.
Sources: packages/frontend/editor-ui/src/components/WhatsNewModal.test.ts:155-156 - Snapshot Testing: Used to capture the rendered output of a component and compare it against a stored snapshot, preventing unintended UI regressions.
Sources: packages/frontend/editor-ui/src/components/WhatsNewModal.test.ts:160
Test Utilities and Mocks
A dedicated set of test utilities and mock objects provides a consistent and reusable foundation for writing tests, particularly for the frontend.
Sources: packages/frontend/editor-ui/src/tests/mocks.ts
Mock Generation
Helper functions are available to create mock objects for core entities within the n8n ecosystem, such as nodes, node types, and workflows. This simplifies test setup and ensures that mock data conforms to the required interfaces.
| Function | Description |
|---|---|
mockNode | Creates a mock INodeUi object with specified properties. |
mockNodeTypeDescription | Creates a mock INodeTypeDescription for a given node type. |
createTestWorkflowObject | Instantiates a Workflow class object with mock data. |
createTestWorkflow | Creates a mock IWorkflowDb object, representing a workflow from the DB. |
createMockEnterpriseSettings | Generates a mock FrontendSettings['enterprise'] object. |
Sources: packages/frontend/editor-ui/src/tests/mocks.ts:32,60,136,158,185
Default Test Data
The mocks.ts file also provides default sets of data, such as defaultNodeTypes and defaultNodeDescriptions, which can be imported into tests to provide a baseline environment without needing to mock everything from scratch in every test file.
Sources: packages/frontend/editor-ui/src/tests/mocks.ts:114-122
Backend and Configuration Testing
Backend testing ensures that core functionalities, like configuration management, work as expected.
Sources: packages/@n8n/config/test/config.test.ts
Configuration Loading
Tests for GlobalConfig verify the configuration loading mechanism, which follows a clear order of precedence:
- Default values.
- Values from environment variables.
- Values from files specified by
_FILEsuffixed environment variables.
The following diagram shows the logic for testing the configuration system.
This diagram illustrates the different test cases for verifying the global configuration loading logic.
Sources: packages/@n8n/config/test/config.test.ts:21,240,270,302
Key aspects tested include:
- Correct application of default values when no environment variables are set.
- Proper overriding of default values by environment variables.
- Correct parsing of different data types (boolean, string arrays, numbers).
- Reading configuration secrets from files (e.g.,
DB_POSTGRESDB_PASSWORD_FILE). - Graceful handling of invalid values, with warnings logged to the console.
Sources: packages/@n8n/config/test/config.test.ts:240,270,302,326
Workflow and Node Integration Testing
n8n uses JSON files to define entire workflows, which serve as integration tests for nodes. These tests ensure that nodes work correctly together and that data flows as expected through a series of connected nodes.
Sources: packages/nodes-base/nodes/HtmlExtract/test/workflow.dotNotation.json
Workflow Test Structure
A workflow test file is a JSON object representing a complete workflow, including:
nodes: An array of node objects, each with its parameters, type, and position.connections: An object defining the links between nodes.pinData: Expected output data for specific nodes, which can be used for assertions.
The example workflow below demonstrates extracting a title from HTML, both directly and after the data has been nested by a Set node.
This diagram shows the flow of the test workflow defined in workflow.dotNotation.json.
Sources: packages/nodes-base/nodes/HtmlExtract/test/workflow.dotNotation.json:3,180
This type of test is crucial for verifying:
- Data transformation between nodes.
- Correct handling of data paths (e.g.,
body.html). - The functionality of core nodes like
Code,Set, andHtmlExtract.
Sources: packages/nodes-base/nodes/HtmlExtract/test/workflow.dotNotation.json:110,131
Performance and Benchmark Testing
The project includes a benchmarking suite to monitor and report on performance metrics. This is essential for identifying performance regressions and ensuring the application remains scalable.
Sources: packages/@n8n/benchmark/src/test-execution/test-report.ts
Test Report Structure
Performance tests, likely executed with k6, generate a summary that is then converted into a standardized TestReport. This report captures key metrics about the test run.
The TestReport and its related data structures are defined as follows:
This class diagram shows the data structure of a TestReport used for performance benchmarking.
Sources: packages/@n8n/benchmark/src/test-execution/test-report.ts:29-47
The buildTestReport function is responsible for converting the raw output from a k6 test run into this structured format. This allows for consistent tracking and analysis of performance over time.
Sources: packages/@n8n/benchmark/src/test-execution/test-report.ts:80-99