Skip to content

Refactor integration tests to use mocked APIs instead of requiring real n8n instance #362

@czlonkowski

Description

@czlonkowski

Problem

Currently, 46 integration tests in tests/integration/n8n-api/workflows/smart-parameters.test.ts are failing in CI with "No response from n8n server" because they require:

  • A running n8n instance
  • Valid API credentials (N8N_API_URL, N8N_API_KEY)
  • Test webhook URLs

This blocks contributor PRs from passing CI checks, as contributors don't have access to these credentials.

Root Cause

These tests are labeled "integration tests" but are actually end-to-end tests requiring external infrastructure. True integration tests should:

  1. Use mocked HTTP responses (MSW - Mock Service Worker)
  2. Test code integration, not external API availability
  3. Run successfully in any CI environment

Affected files:

  • tests/integration/n8n-api/workflows/smart-parameters.test.ts (46 failing tests)
  • tests/integration/n8n-api/workflows/autofix-workflow.test.ts
  • All tests using getTestN8nClient()

Current Impact

  • 46 tests fail on every PR unless n8n credentials are available
  • Contributors cannot run these tests locally without a real n8n instance
  • PR checks block merging even for unrelated code changes
  • CI workflow has empty environment variables for n8n credentials

Proposed Solution

1. Immediate Fix (Short-term)

Add skip logic when credentials aren't available:

// In test setup files
beforeAll(() => {
  if (process.env.CI && \!process.env.N8N_API_URL) {
    console.warn('⚠️ Skipping n8n API integration tests - credentials not available');
    console.warn('These tests require N8N_API_URL and N8N_API_KEY to be set');
    process.exit(0); // Exit with success to not block CI
  }
});

2. Long-term Solution (Recommended)

Restructure test architecture into three distinct tiers:

tests/
├── unit/              # Pure logic, no external dependencies (✅ already working)
├── integration/       # Mocked HTTP responses using MSW (⚠️ needs refactoring)
└── e2e/              # Real n8n instance required (❌ should be optional)
    └── README.md     # Documentation: "Run with: N8N_API_URL=... npm run test:e2e"

Benefits:

  • Integration tests use MSW to mock n8n API responses
  • Tests validate request/response handling without real infrastructure
  • All tests can run in any CI environment
  • E2E tests become optional for contributors

3. Implementation Steps

  1. Install MSW (Mock Service Worker)

    npm install -D msw
  2. Create mock handlers for n8n API

    // tests/mocks/n8n-api-handlers.ts
    import { http, HttpResponse } from 'msw';
    
    export const n8nHandlers = [
      http.post('*/workflows', () => {
        return HttpResponse.json({ id: 'test-workflow-id', ... });
      }),
      http.patch('*/workflows/:id', () => {
        return HttpResponse.json({ success: true });
      }),
      // ... more handlers
    ];
  3. Refactor integration tests to use mocks

    import { setupServer } from 'msw/node';
    import { n8nHandlers } from '../../mocks/n8n-api-handlers';
    
    const server = setupServer(...n8nHandlers);
    
    beforeAll(() => server.listen());
    afterEach(() => server.resetHandlers());
    afterAll(() => server.close());
  4. Move real API tests to e2e directory

    mv tests/integration/n8n-api/workflows/smart-parameters.test.ts tests/e2e/
  5. Update CI workflows

    # .github/workflows/test.yml - Always runs
    - name: Run unit and integration tests
      run: |
        npm run test:unit
        npm run test:integration  # Now uses MSW mocks
    
    # .github/workflows/test-e2e.yml - Manual trigger only
    - name: Run E2E tests
      if: github.event_name == 'workflow_dispatch'
      run: npm run test:e2e

Acceptance Criteria

  • Integration tests run successfully without real n8n credentials
  • All contributor PRs can pass CI checks
  • E2E tests are separated and marked as optional
  • Documentation updated with instructions for running E2E tests locally
  • MSW configured for mocking n8n API responses
  • Test coverage maintained or improved

Related

Priority

High - This blocks all contributor PRs from merging successfully.


Conceived by Romuald Członkowski - www.aiadvisors.pl/en

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions