feat(esm): Add vitest plugins and config to packages/testing (take 2)#362
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 migration strategy. The changes introduce a complete parallel testing infrastructure alongside the existing Jest setup, enabling projects to use Vitest as an alternative testing framework.
The core additions include:
Vitest Plugin Architecture: New barrel export files (packages/testing/src/api/vitest/index.ts and packages/testing/src/web/vitest/index.ts) expose specialized Vite plugins for both API and web testing scenarios. These plugins handle import transformations, auto-imports, and database tracking specific to Vitest's execution model.
Import Transformation Plugins: Several transformation plugins convert CedarJS-specific imports during testing - cedarJsRouterImportTransformPlugin replaces router imports with mock implementations, createAuthImportTransformPlugin mocks authentication imports, and these transformations only apply in test environments to maintain production functionality.
Database Testing Infrastructure: A custom Vitest environment (CedarApiVitestEnv.ts) handles database setup and migrations, while a database tracking plugin (vite-plugin-track-db-imports.ts) optimizes performance by only running database teardown when tests actually use the database.
Test Utilities and Setup: The vitest-api.setup.ts file provides comprehensive scenario management, context mocking, and database teardown capabilities specifically adapted for Vitest's execution model, recreating Jest's testing utilities with Vitest-compatible implementations.
Integration Points: The @cedarjs/vite package is updated to conditionally include Vitest plugins when in test mode, and the MockRouter component is enhanced to work with both Jest and Vitest mocking strategies. Package exports are expanded to expose the new Vitest functionality while maintaining backward compatibility.
Build System Updates: TypeScript project references are added to establish proper build dependencies, and the testing package's build process is customized to handle dual ESM/CJS output with specific handling for import.meta warnings and Jest compatibility requirements.
This implementation provides feature parity with the existing Jest testing infrastructure while leveraging Vitest's better ESM support and performance characteristics.
Confidence score: 3/5
- This PR introduces significant complexity with multiple transformation plugins and build system changes that could cause integration issues
- Score reflects concerns about the brittle string manipulation in build processes and potential edge cases in import transformation plugins
- Pay close attention to the build configuration in
packages/testing/build.mtsand the import transformation plugins which use complex regex patterns
21 files reviewed, 5 comments
| code = code.replace( | ||
| /['"]@cedarjs\/router['"]/, | ||
| "'@cedarjs/testing/web/MockRouter.js'", | ||
| ) |
There was a problem hiding this comment.
style: Consider using global flag in regex to replace all occurrences in a file, not just the first match
| if (isErrorWithCode(e)) { | ||
| if (e instanceof Error) { | ||
| throw e | ||
| } else { | ||
| console.error('unexpected error type', e) | ||
| // eslint-disable-next-line | ||
| throw e | ||
| } | ||
| } |
There was a problem hiding this comment.
logic: error handling logic seems inverted - isErrorWithCode(e) should check for module not found errors, not re-throw them
| mockContextStore.set('context', newContext) | ||
| // TODO: See if this should be `newContext` instead | ||
| // https://github.com/cedarjs/cedar/pull/355#discussion_r2264851567 | ||
| return mockContext |
There was a problem hiding this comment.
logic: Returning mockContext instead of newContext creates inconsistency with production behavior where setContext returns the new context proxy
This is basically #358 again, making this 9/n in the ESM saga that started with #349