Skip to main content

Node Testing

Testing is crucial for ensuring your nodes work correctly and continue to work as the codebase evolves. n8n uses Jest for unit testing and provides testing utilities for nodes.

Testing Setup

All node tests are located in the packages/nodes-base/nodes/ directory alongside the node files.

File Naming Convention

Running Tests

Testing Dependencies

Common testing libraries used in n8n:

Basic Test Structure

Here’s a complete test file structure:

Mocking Execution Context

The IExecuteFunctions interface provides the context for node execution. Mock it appropriately:

HTTP Mocking with Nock

Use nock to mock external API calls:

Real-World Example: AMQP Tests

Here’s the complete AMQP node test from the source code:

Testing Patterns

Test Operations

1

Test Happy Path

Test the expected successful execution:
2

Test Error Handling

Test how the node handles errors:
3

Test Edge Cases

Test boundary conditions:
4

Test Continue on Fail

Test error handling with continueOnFail:

Test Binary Data

Test Pagination

Testing LoadOptions

Testing Credential Tests

Best Practices

Follow these testing best practices for maintainable tests.

DO:

  • ✅ Mock all external dependencies
  • ✅ Test happy paths, error cases, and edge cases
  • ✅ Use jest.clearAllMocks() in beforeEach()
  • ✅ Use nock.cleanAll() when testing HTTP requests
  • ✅ Test credential validation
  • ✅ Test continue-on-fail behavior
  • ✅ Use descriptive test names
  • ✅ Test with multiple items
  • ✅ Test binary data handling

DON’T:

  • ❌ Make real API calls in tests
  • ❌ Use any type in test code
  • ❌ Share state between tests
  • ❌ Test implementation details
  • ❌ Skip error case testing
  • ❌ Hardcode test data in multiple places

Coverage Requirements

Aim for high test coverage:
  • Statements: > 80%
  • Branches: > 75%
  • Functions: > 80%
  • Lines: > 80%
Check coverage:

Next Steps

Versioning

Learn how to version your nodes

Node Structure

Review node structure and implementation