Skip to content

improvement(tests): speed up unit tests by eliminating vi.resetModules anti-pattern#3357

Merged
waleedlatif1 merged 4 commits intostagingfrom
waleedlatif1/speedup-unit-tests
Feb 26, 2026
Merged

improvement(tests): speed up unit tests by eliminating vi.resetModules anti-pattern#3357
waleedlatif1 merged 4 commits intostagingfrom
waleedlatif1/speedup-unit-tests

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • Convert 51 test files from slow vi.resetModules/vi.doMock/await import() pattern to fast vi.hoisted/vi.mock/static import pattern
  • Add global @sim/db mock to vitest.setup.ts so tests don't need per-file database mocking
  • Switch 4 test files from jsdom to node environment (no DOM usage)
  • Remove all vi.importActual calls that were loading heavy modules unnecessarily (200+ block definition files)
  • Remove slow mockConsoleLogger/mockAuth/setupCommonApiMocks helpers that internally used vi.doMock
  • Reduce real setTimeout delays in engine tests
  • Mock heavy transitive deps in diff-engine test to avoid loading block/tool/trigger registries

Results: test execution time 34s → 9s (3.9x), environment time 2.5s → 0.6s (4x)

Type of Change

  • Enhancement / improvement

Testing

All 4086 tests pass. Only 4 pre-existing failures remain (unrelated to this change).

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Feb 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Feb 26, 2026 11:45pm

Request Review

…s anti-pattern

- convert 51 test files from vi.resetModules/vi.doMock/dynamic import to vi.hoisted/vi.mock/static import
- add global @sim/db mock to vitest.setup.ts
- switch 4 test files from jsdom to node environment
- remove all vi.importActual calls that loaded heavy modules (200+ block files)
- remove slow mockConsoleLogger/mockAuth/setupCommonApiMocks helpers
- reduce real setTimeout delays in engine tests
- mock heavy transitive deps in diff-engine test

test execution time: 34s -> 9s (3.9x faster)
environment time: 2.5s -> 0.6s (4x faster)
- document vi.hoisted + vi.mock + static import as the standard pattern
- explicitly ban vi.resetModules, vi.doMock, vi.importActual, mockAuth, setupCommonApiMocks
- document global mocks from vitest.setup.ts
- add mock pattern reference for auth, hybrid auth, and database chains
- add performance rules section covering heavy deps, jsdom vs node, real timers
@waleedlatif1 waleedlatif1 force-pushed the waleedlatif1/speedup-unit-tests branch from dfbb1b7 to d40219f Compare February 26, 2026 23:34
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 26, 2026

Greptile Summary

This PR successfully refactors 65 test files to use faster Vitest patterns, achieving a 3.9x speedup in test execution time (34s → 9s) and 4x improvement in environment setup time (2.5s → 0.6s).

Key improvements:

  • Converted 51 test files from slow vi.resetModules/vi.doMock/await import() pattern to fast vi.hoisted/vi.mock/static import pattern
  • Added global @sim/db mock to vitest.setup.ts to eliminate per-file database mocking boilerplate
  • Removed all vi.importActual calls that were loading 200+ heavy block definition files unnecessarily
  • Removed slow helper functions (mockConsoleLogger, mockAuth, setupCommonApiMocks) that internally used vi.doMock
  • Reduced setTimeout delays in engine tests from 500ms/150ms/50ms to 1-2ms
  • Mocked heavy transitive dependencies in diff-engine test to avoid loading block/tool/trigger registries
  • Switched 4 test files from jsdom to node environment where DOM is not used

Test results:
All 4086 tests pass with only. The refactoring is well-executed with consistent patterns applied across all files.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • Score reflects consistent refactoring pattern applied across 65 test files, significant performance improvements (3.9x speedup), all 4086 tests passing, and well-documented changes with clear benefits and no functional modifications to production code
  • No files require special attention

Important Files Changed

Filename Overview
apps/sim/vitest.setup.ts Added global @sim/db mock to eliminate per-file database mocking boilerplate
apps/sim/executor/execution/engine.test.ts Reduced setTimeout delays from 500ms/150ms/50ms to 1-2ms for faster test execution
apps/sim/lib/workflows/diff/diff-engine.test.ts Added explicit mocks for heavy dependencies (@/blocks, @/tools/utils, @/triggers) to avoid loading full registries
apps/sim/executor/variables/resolvers/block.test.ts Replaced vi.importActual with minimal MOCK_BLOCKS to avoid loading 200+ block definition files
apps/sim/app/api/chat/route.test.ts Converted from slow vi.resetModules/vi.doMock/await import() to fast vi.hoisted/vi.mock/static import pattern
apps/sim/app/api/files/parse/route.test.ts Removed mockAuth/setupCommonApiMocks helpers, switched to explicit vi.hoisted mock declarations
apps/sim/app/api/schedules/execute/route.test.ts Refactored to use vi.hoisted and static imports, eliminated redundant vi.doMock in each test
apps/sim/lib/file-parsers/index.test.ts Converted to fast pattern with vi.hoisted, removed vi.resetModules from beforeEach
apps/sim/app/api/tools/custom/route.test.ts Replaced mockHybridAuth helper with explicit mocks, moved sample data outside describe block
apps/sim/app/api/copilot/checkpoints/revert/route.test.ts Converted to fast pattern with static imports and vi.hoisted mock declarations
apps/sim/app/api/folders/[id]/route.test.ts Refactored from slow vi.doMock pattern to fast vi.mock with hoisted declarations
apps/sim/app/api/knowledge/[id]/documents/[documentId]/route.test.ts Switched to fast test pattern, removed per-test module resets
apps/sim/lib/mcp/connection-manager.test.ts Converted to use vi.hoisted and static imports for better performance
apps/sim/lib/uploads/providers/s3/client.test.ts Refactored from dynamic imports to static imports with hoisted mocks

Last reviewed commit: d40219f

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

65 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

- socket/middleware/permissions: add vi.mock for @/lib/auth to prevent transitive getBaseUrl() call
- workflow-handler: add vi.mock for @/executor/utils/http matching executor mock pattern
- evaluator-handler: add db.query.account mock structure before vi.spyOn
- router-handler: same db.query.account fix as evaluator
@waleedlatif1 waleedlatif1 merged commit 4ccb573 into staging Feb 26, 2026
3 checks passed
@waleedlatif1 waleedlatif1 deleted the waleedlatif1/speedup-unit-tests branch February 26, 2026 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant