Skip to content

test: Fix flaky unit tests across three packages (no-changelog)#28336

Merged
shortstacked merged 1 commit intomasterfrom
fix-flaky-unit-tests
Apr 13, 2026
Merged

test: Fix flaky unit tests across three packages (no-changelog)#28336
shortstacked merged 1 commit intomasterfrom
fix-flaky-unit-tests

Conversation

@shortstacked
Copy link
Copy Markdown
Collaborator

Summary

Fix three flaky unit tests that accounted for ~200 CI failures across 40+ branches in the last 7 days (data from Codecov test analytics).

1. ExecutionPersistence — timestamp off-by-one (15 failures / 15 branches)

The test captured Date.now() after calling the function under test, then asserted the backdated deletedAt was <= that value minus the buffer. When the clock ticked by 1ms between the implementation's and the test's Date.now() calls, the assertion failed. Fixed by using Jest fake timers so Date.now() is deterministic.

2. TypeScript Worker — CDN fetch timeout (8 failures / 8 branches)

The test didn't mock indexedDbCache, so it opened real IndexedDB via fake-indexeddb. When the luxon type cache missed, the worker tried to fetch types from cdn.jsdelivr.net, which timed out under CI network constraints. Fixed by mocking the cache module with a cache hit, eliminating both IndexedDB and network overhead (tests now run in 9ms vs timing out at 5000ms).

3. GoogleSheetsTrigger — missing nock isolation (23 failures each × 5 tests / 8 branches)

The test file was missing nock.disableNetConnect(). While globalSetup.ts calls it, Jest runs global setup in a separate context from test workers. Without it, unmatched requests could leak to the real Google Sheets API. Fixed by adding proper nock lifecycle hooks (disableNetConnect, cleanAll, enableNetConnect).

Related Linear tickets, Github issues, and Community forum posts

N/A — identified via Codecov test analytics API flaky test analysis.

Review / Merge checklist

  • I have seen this code, I have run this code, and I take responsibility for this code.
  • PR title and summary are descriptive. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 10, 2026

Bundle Report

Bundle size has no change ✅

Affected Assets, Files, and Routes:

view changes for bundle: editor-ui-esm

Assets Changed:

Asset Name Size Change Total Size Change (%)
assets/worker-*.js -3.14MB 17.9kB -99.43%
assets/worker-*.js 3.14MB 3.16MB 17560.39% ⚠️

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@shortstacked shortstacked marked this pull request as ready for review April 10, 2026 14:17
@shortstacked shortstacked requested review from a team and schrothbn April 10, 2026 14:18
@github-actions
Copy link
Copy Markdown
Contributor

Performance Comparison

Comparing currentlatest master14-day baseline

docker-stats

Metric Current Latest Master Baseline (avg) vs Master vs Baseline Status
docker-image-size-runners 386.00 MB 386.00 MB 387.50 MB (σ 3.00) +0.0% -0.4%
docker-image-size-n8n 1269.76 MB 1269.76 MB 1269.76 MB (σ 0.00) +0.0% +0.0%

Idle baseline with Instance AI module loaded

Metric Current Latest Master Baseline (avg) vs Master vs Baseline Status
instance-ai-heap-used-baseline 186.30 MB 186.51 MB 186.46 MB (< 3 samples) -0.1% -0.1%
instance-ai-rss-baseline 342.09 MB 394.55 MB 369.15 MB (< 3 samples) -13.3% -7.3%

Memory consumption baseline with starter plan resources

Metric Current Latest Master Baseline (avg) vs Master vs Baseline Status
memory-heap-used-baseline 113.85 MB 114.53 MB 113.09 MB (σ 1.15) -0.6% +0.7%
memory-rss-baseline 285.81 MB 287.07 MB 281.78 MB (σ 34.50) -0.4% +1.4%
How to read this table
  • Current: This PR's value (or latest master if PR perf tests haven't run)
  • Latest Master: Most recent nightly master measurement
  • Baseline: Rolling 14-day average from master
  • vs Master: PR impact (current vs latest master)
  • vs Baseline: Drift from baseline (current vs rolling avg)
  • Status: ✅ within 1σ | ⚠️ 1-2σ | 🔴 >2σ regression

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

Architecture diagram
sequenceDiagram
    participant Test as Test Runner (Jest/Vitest)
    participant SUT as System Under Test
    participant Mocks as Mocking Layer (Jest/Nock/Vi)
    participant Infra as External Infra (DB/CDN/API)

    Note over Test,Infra: Flow 1: ExecutionPersistence (Deterministic Timestamps)
    Test->>Mocks: NEW: jest.useFakeTimers()
    Test->>SUT: deleteInFlightExecution()
    SUT->>SUT: Calculate deletedAt (now - buffer)
    SUT->>Infra: update(executionId, { deletedAt })
    Test->>Test: NEW: Assert deletedAt matches fixed mock time
    Test->>Mocks: jest.useRealTimers()

    Note over Test,Infra: Flow 2: TypeScript Worker (Cache Isolation)
    Test->>Mocks: NEW: vi.mock('@/app/plugins/cache')
    Test->>SUT: Initialize Worker
    SUT->>Mocks: Get types from cache
    alt NEW: Cache Mocked
        Mocks-->>SUT: Return empty object/mocked types
    else OLD: Cache Miss (Flaky Path)
        SUT->>Infra: Open IndexedDB (fake-indexeddb)
        SUT->>Infra: Fetch from cdn.jsdelivr.net (Timeout Risk)
    end
    SUT-->>Test: Worker ready (9ms)

    Note over Test,Infra: Flow 3: GoogleSheetsTrigger (Network Isolation)
    Test->>Mocks: NEW: nock.disableNetConnect()
    Test->>SUT: Execute Trigger Node
    SUT->>Mocks: HTTP GET /spreadsheets/...
    alt Mock Match
        Mocks-->>SUT: Return 200 OK (Defined fixture)
    else NEW: Unmatched Request
        Mocks-->>SUT: Throw NetConnectNotAllowedError
    else OLD: Request Leak (Flaky Path)
        Mocks->>Infra: Call real Google Sheets API
    end
    SUT-->>Test: Return node output
    Test->>Mocks: NEW: nock.cleanAll() / enableNetConnect()
Loading

@n8n-assistant n8n-assistant Bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team node/improvement New feature or request labels Apr 10, 2026
@shortstacked shortstacked enabled auto-merge April 10, 2026 14:38
@shortstacked shortstacked added this pull request to the merge queue Apr 13, 2026
Merged via the queue into master with commit 738d42c Apr 13, 2026
61 checks passed
@shortstacked shortstacked deleted the fix-flaky-unit-tests branch April 13, 2026 11:12
@n8n-assistant
Copy link
Copy Markdown
Contributor

n8n-assistant Bot commented Apr 14, 2026

Got released with n8n@

Aijeyomah pushed a commit to Aijeyomah/n8n that referenced this pull request Apr 15, 2026
…io#28336)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team node/improvement New feature or request Released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants