Skip to content

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

Merged
Tobbe merged 14 commits into
mainfrom
tobbe-esm-prep-testing-vitest-take-2
Aug 10, 2025
Merged

feat(esm): Add vitest plugins and config to packages/testing (take 2)#362
Tobbe merged 14 commits into
mainfrom
tobbe-esm-prep-testing-vitest-take-2

Conversation

@Tobbe
Copy link
Copy Markdown
Member

@Tobbe Tobbe commented Aug 9, 2025

This is basically #358 again, making this 9/n in the ESM saga that started with #349

@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 3997562
🔍 Latest deploy log https://app.netlify.com/projects/cedarjs/deploys/6897e4c6846b4f0008aa3a58

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 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.mts and the import transformation plugins which use complex regex patterns

21 files reviewed, 5 comments

Edit Code Review Bot Settings | Greptile

Comment on lines +15 to +18
code = code.replace(
/['"]@cedarjs\/router['"]/,
"'@cedarjs/testing/web/MockRouter.js'",
)
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.

style: Consider using global flag in regex to replace all occurrences in a file, not just the first match

Comment thread packages/vite/src/index.ts
Comment on lines +288 to +296
if (isErrorWithCode(e)) {
if (e instanceof Error) {
throw e
} else {
console.error('unexpected error type', e)
// eslint-disable-next-line
throw e
}
}
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: error handling logic seems inverted - isErrorWithCode(e) should check for module not found errors, not re-throw them

Comment thread packages/testing/src/web/MockRouter.tsx Outdated
mockContextStore.set('context', newContext)
// TODO: See if this should be `newContext` instead
// https://github.com/cedarjs/cedar/pull/355#discussion_r2264851567
return mockContext
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: Returning mockContext instead of newContext creates inconsistency with production behavior where setContext returns the new context proxy

@Tobbe Tobbe enabled auto-merge (squash) August 10, 2025 00:20
@Tobbe Tobbe merged commit 342f555 into main Aug 10, 2025
50 checks passed
@Tobbe Tobbe deleted the tobbe-esm-prep-testing-vitest-take-2 branch August 10, 2025 00:26
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