Skip to content

feat(esm): Add vitest plugins and config to packages/testing (take 2)#358

Closed
Tobbe wants to merge 7 commits into
mainfrom
tobbe-esm-prep-testing-vitest-take-2
Closed

feat(esm): Add vitest plugins and config to packages/testing (take 2)#358
Tobbe wants to merge 7 commits into
mainfrom
tobbe-esm-prep-testing-vitest-take-2

Conversation

@Tobbe
Copy link
Copy Markdown
Member

@Tobbe Tobbe commented Aug 9, 2025

Trying to figure out why #355 is failing CI by taking just a few of the changes from that PR and making a new PR with those

@Tobbe Tobbe added this to the next-release milestone Aug 9, 2025
@netlify
Copy link
Copy Markdown

netlify Bot commented Aug 9, 2025

Deploy Preview for cedarjs canceled.

Name Link
🔨 Latest commit 408df33
🔍 Latest deploy log https://app.netlify.com/projects/cedarjs/deploys/6897b3a1ea4834000893aa5d

Copy link
Copy Markdown
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.

Greptile Summary

This PR adds comprehensive Vitest support to the @cedarjs/testing package as part of CedarJS's ESM preparation effort. The changes introduce a complete parallel testing infrastructure alongside the existing Jest setup, providing developers with an alternative testing framework that better supports modern JavaScript features.

The implementation spans both API and web testing environments. For API testing, the PR adds three main Vite plugins: an auto-imports plugin that globally injects common testing utilities (mockContext, mockHttpEvent, mockSignedWebhook, gql, context), a configuration plugin that sets up Vitest with proper environment variables and database handling, and a database tracking plugin that optimizes test performance by only running cleanup when database operations are actually used. A custom Vitest environment (CedarApiVitestEnv) manages database setup with support for both migration strategies, while a comprehensive setup file (vitest-api.setup.ts) provides scenario testing capabilities, context mocking through Proxy-based isolation, and automated database teardown.

For web testing, the PR introduces three corresponding plugins: a router import transformer that redirects @cedarjs/router imports to use the mock router from @cedarjs/testing, an authentication transformer that replaces createAuth imports with test-friendly mocked versions, and an auto-imports plugin for web-specific testing utilities (mockGraphQLQuery, mockGraphQLMutation, mockCurrentUser). The existing MockRouter.tsx component is enhanced with documentation explaining how it supports both Jest and Vitest through different mocking strategies.

The changes maintain backward compatibility by excluding Vitest-specific code from the CommonJS build in tsconfig.cjs.json, ensuring dual package exports continue to work. All new modules use .js file extensions in their imports, following ESM conventions. The implementation closely mirrors the existing Jest functionality to ensure a seamless migration path for developers who want to adopt Vitest.

Confidence score: 3/5

  • This PR introduces significant complexity but has several implementation issues that could cause problems in edge cases
  • Score lowered due to regex-based string manipulation in transform plugins, inconsistent error handling, and potential runtime errors in proxy implementations
  • Pay close attention to vite-plugin-create-auth-import-transform.ts, mockContext.ts, and CedarApiVitestEnv.ts for potential edge case failures

14 files reviewed, 5 comments

Edit Code Review Bot Settings | Greptile

Comment on lines +14 to +17
code = code.replace(
/(^\s*import\s*{[^}]*?)(\bcreateAuth\b)(,?)([^}]*})/m,
'$1$4',
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

logic: Regex may fail with multi-line imports or when createAuth is the only import, potentially creating malformed code like import { }

Comment thread packages/testing/src/web/vitest/vite-plugin-create-auth-import-transform.ts Outdated
Comment on lines +28 to +29
const ctx = mockContextStore.get('context')
ctx[prop] = value
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

logic: Potential runtime error if mockContextStore.get('context') returns undefined. Add null check before accessing properties.

Comment thread packages/testing/src/api/vitest/vitest-api.setup.ts
Comment thread packages/testing/src/api/mockContext.ts
@Tobbe
Copy link
Copy Markdown
Member Author

Tobbe commented Aug 9, 2025

Got #355 passing, so can close this one now

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