feat(esm): Add vitest plugins and config to packages/testing (take 2)#358
feat(esm): Add vitest plugins and config to packages/testing (take 2)#358Tobbe wants to merge 7 commits into
Conversation
✅ Deploy Preview for cedarjs canceled.
|
There was a problem hiding this comment.
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, andCedarApiVitestEnv.tsfor potential edge case failures
14 files reviewed, 5 comments
| code = code.replace( | ||
| /(^\s*import\s*{[^}]*?)(\bcreateAuth\b)(,?)([^}]*})/m, | ||
| '$1$4', | ||
| ) |
There was a problem hiding this comment.
logic: Regex may fail with multi-line imports or when createAuth is the only import, potentially creating malformed code like import { }
| const ctx = mockContextStore.get('context') | ||
| ctx[prop] = value |
There was a problem hiding this comment.
logic: Potential runtime error if mockContextStore.get('context') returns undefined. Add null check before accessing properties.
|
Got #355 passing, so can close this one now |
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