Skip to content

improvement(tests): make vitest suites state-safe with auto-unstub of env/global stubs#5853

Merged
waleedlatif1 merged 3 commits into
stagingfrom
test/isolation-hygiene
Jul 22, 2026
Merged

improvement(tests): make vitest suites state-safe with auto-unstub of env/global stubs#5853
waleedlatif1 merged 3 commits into
stagingfrom
test/isolation-hygiene

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • Add unstubEnvs: true / unstubGlobals: true to the sim vitest config so env/global stubs are auto-restored after every test
  • Move module-scope fetch/crypto/window stubs into beforeEach across executor, data-drains, and knowledge suites — several suites were silently making live network calls (observed real 401s from Datadog/BigQuery/Snowflake/OpenAI/Anthropic) when their module-scope stubs got cleared
  • Converge billing/workspaces/tools/hooks suites onto shared @sim/testing mock instances and namespace spies instead of rival file-local vi.mock factories, with explicit beforeEach setup and afterAll restore
  • Give lib/core suites private module instances via vite query-suffix imports where module-level state bakes env at import time
  • Stub auth-client/NEXT_PUBLIC_APP_URL in suites that crashed at collection without a local .env
  • No CI behavior change: isolation stays on; this is groundwork toward an eventual isolate: false flip (2.2x faster suite, blocked today by cross-file mock-binding conflicts)

Type of Change

  • Improvement

Testing

Full apps/sim suite green locally under current isolated mode (1055/1056; sole failure is a pre-existing local-env ripgrep dependency that passes in CI). Also validated each fixed file standalone and in shared-worker orderings.

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)

… env/global stubs

- add unstubEnvs/unstubGlobals to vitest config so vi.stubEnv/vi.stubGlobal
  are restored after every test
- move module-scope fetch/crypto/window stubs into beforeEach across executor,
  data-drains, and knowledge suites so they hold under auto-unstub (several
  suites were silently hitting live Datadog/BigQuery/Snowflake/OpenAI/Anthropic
  endpoints when their module-scope stubs were cleared)
- converge billing/workspaces/tools/hooks suites onto the shared @sim/testing
  mock instances and namespace spies instead of rival file-local vi.mock
  factories, with explicit beforeEach setup and afterAll restore
- give lib/core suites private module instances via vite query-suffix imports
  where module-level state bakes env at import time
- stub auth-client/NEXT_PUBLIC_APP_URL in suites that crashed at collection
  without a local .env

Groundwork for eventually running the suite with isolate: false; no CI
behavior change (isolation stays on).
@vercel

vercel Bot commented Jul 22, 2026

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Jul 22, 2026 8:10pm

Request Review

@cursor

cursor Bot commented Jul 22, 2026

Copy link
Copy Markdown

PR Summary

Low Risk
Changes are limited to test configuration and test files; production code is untouched aside from a shared testing mock helper.

Overview
Vitest config now sets unstubEnvs: true and unstubGlobals: true, so env and global stubs are restored after every test instead of leaking across cases.

Suites that relied on module-scope fetch, crypto, or window stubs re-apply them in beforeEach (executor handlers, workflow deploy, data-drain destinations, knowledge/API tests). That fixes cases where the first test cleared stubs and later tests could hit real HTTP endpoints.

Mock wiring shifts away from rival per-file vi.mock factories toward patterns that work when the module graph is shared: vi.spyOn on real namespaces (billing, embeddings, tools registry, query client, schedule utils, uploads), mutating/restoring the real env object with snapshots, shared @sim/testing dbChainMock/databaseMock delegation, and query-suffixed imports for libs that freeze env at load time (feature flags, rate limiter, execution timeouts, SSRF/pinned fetch, table limits).

Collection/runtime guards add auth-client stubs or NEXT_PUBLIC_APP_URL where Better Auth or getBaseUrl() would throw without a .env, trim risky it.concurrent usage in knowledge utils, and extend the shared DB mock with selectDistinctOn. isolate remains true; this is prep for a future isolate: false speedup without changing CI behavior today.

Reviewed by Cursor Bugbot for commit 11085e0. Configure here.

@greptile-apps

greptile-apps Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR makes the Sim Vitest suites safer around shared state. The main changes are:

  • Automatically restore stubbed environment variables and globals after each test.
  • Recreate fetch, crypto, and window stubs in per-test setup.
  • Use shared database mocks and namespace spies across affected suites.
  • Isolate modules whose state depends on environment values at import time.
  • Add local auth and application URL stubs where collection requires them.

Confidence Score: 5/5

This looks safe to merge.

  • The shared database helpers now restore defined implementations and reset originally undefined implementations.
  • The correction is applied consistently across the five affected suites.
  • No blocking issue remains from the previously reported shared-mock leak.

Important Files Changed

Filename Overview
apps/sim/lib/billing/core/plan.test.ts Moves database setup to shared mocks and fully restores captured implementations after the suite.
apps/sim/lib/billing/core/usage-log.test.ts Uses shared database mocks with complete teardown for insert and transaction behavior.
apps/sim/lib/billing/core/usage.test.ts Delegates shared database entry points during tests and restores their original state afterward.
apps/sim/lib/workspaces/utils.test.ts Applies the corrected shared database restoration pattern to workspace utility tests.
apps/sim/lib/admin/dashboard-organizations.test.ts Uses shared query mocks and restores all captured database methods, including selectDistinctOn.
apps/sim/vitest.config.ts Enables automatic restoration of environment and global stubs between tests.
packages/testing/src/mocks/database.mock.ts Updates shared database mock support used by the state-safe test setup.

Reviews (3): Last reviewed commit: "fix(tests): run knowledge utils cases se..." | Re-trigger Greptile

Comment thread apps/sim/lib/billing/core/plan.test.ts
Comment thread apps/sim/lib/billing/core/plan.test.ts
Comment thread apps/sim/lib/admin/dashboard-organizations.test.ts
- restoreGlobalDb now mockReset()s entries whose original implementation was
  undefined instead of leaving the chain-mock delegation installed
- add selectDistinctOn to the setup-level createMockDb and to the dashboard
  suite's delegated key set so the distinct-on path routes under either binding
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/app/api/knowledge/utils.test.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 11085e0. Configure here.

@waleedlatif1
waleedlatif1 merged commit fad9728 into staging Jul 22, 2026
20 checks passed
@waleedlatif1
waleedlatif1 deleted the test/isolation-hygiene branch July 22, 2026 21:22
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