-
-
Notifications
You must be signed in to change notification settings - Fork 25
feat(esm): Add vitest plugins and config to packages/testing (take 2) #362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
025a6aa
feat(esm): Add vitest plugins and config to packages/testing (take 2)
Tobbe 5199f72
vite-plugin-create-auth-import-transform id check
Tobbe b714b1a
bring more changes over from #355
Tobbe 0c17224
bring more changes over from #355 - II
Tobbe 91b28ba
bring more changes over from #355 - III
Tobbe 044743c
bring more changes over from #355 - IV
Tobbe 65f861d
fix link to import tranform plugin
Tobbe 852e917
add globRoutesImporter
Tobbe cae4abd
debug testing build
Tobbe bdec4c2
debug testing build - log after write
Tobbe cf0f346
remove unused import
Tobbe c9c9f56
Import globRoutesImporter in MockProvider
Tobbe c052ec5
log MockProviders
Tobbe 3997562
remove debug logs
Tobbe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // TODO: See if we can use `GlobalContext` instead of `any` here to more | ||
| // closely match the production context. | ||
| // https://github.com/cedarjs/cedar/pull/355#discussion_r2264851576 | ||
| const mockContextStore = new Map<string, any>() | ||
| const mockContext = new Proxy( | ||
| {}, | ||
| { | ||
| get: (_target, prop) => { | ||
| // Handle toJSON() calls, i.e. JSON.stringify(context) | ||
| if (prop === 'toJSON') { | ||
| return () => mockContextStore.get('context') | ||
| } | ||
|
|
||
| const ctx = mockContextStore.get('context') | ||
|
|
||
| if (!ctx) { | ||
| return undefined | ||
| } | ||
|
|
||
| return ctx[prop] | ||
| }, | ||
| set: (_target, prop, value) => { | ||
| const ctx = mockContextStore.get('context') | ||
|
|
||
| if (!ctx) { | ||
| return false | ||
| } | ||
|
|
||
| ctx[prop] = value | ||
|
|
||
| return true | ||
| }, | ||
| }, | ||
| ) | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-empty-object-type | ||
| export interface GlobalContext extends Record<string, unknown> {} | ||
|
|
||
| export const context = mockContext | ||
|
|
||
| export const setContext = (newContext: GlobalContext): GlobalContext => { | ||
| mockContextStore.set('context', newContext) | ||
| // TODO: See if this should be `newContext` instead | ||
| // https://github.com/cedarjs/cedar/pull/355#discussion_r2264851567 | ||
| return mockContext | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { getSchema } from '@prisma/internals' | ||
| import 'dotenv-defaults/config.js' | ||
| import execa from 'execa' | ||
| import type { Environment } from 'vitest/environments' | ||
|
|
||
| import { getPaths } from '@cedarjs/project-config' | ||
|
|
||
| import { getDefaultDb, checkAndReplaceDirectUrl } from '../directUrlHelpers.js' | ||
|
|
||
| const CedarApiVitestEnvironment: Environment = { | ||
| name: 'cedar-api', | ||
| transformMode: 'ssr', | ||
|
|
||
| async setup() { | ||
| if (process.env.SKIP_DB_PUSH === '1') { | ||
| return { | ||
| teardown() {}, | ||
| } | ||
| } | ||
|
|
||
| const cedarPaths = getPaths() | ||
| const defaultDb = getDefaultDb(cedarPaths.base) | ||
|
|
||
| process.env.DATABASE_URL = process.env.TEST_DATABASE_URL || defaultDb | ||
|
|
||
| // NOTE: This is a workaround to get the directUrl from the schema | ||
| // Instead of using the schema, we can use the config file | ||
| // const prismaConfig = await getConfig(rwjsPaths.api.dbSchema) | ||
| // and then check for the prismaConfig.datasources[0].directUrl | ||
| const prismaSchema = (await getSchema(cedarPaths.api.dbSchema)).toString() | ||
|
|
||
| const directUrlEnvVar = checkAndReplaceDirectUrl(prismaSchema, defaultDb) | ||
|
|
||
| const command = | ||
| process.env.TEST_DATABASE_STRATEGY === 'reset' | ||
| ? ['prisma', 'migrate', 'reset', '--force', '--skip-seed'] | ||
| : ['prisma', 'db', 'push', '--force-reset', '--accept-data-loss'] | ||
|
|
||
| const directUrlDefinition = directUrlEnvVar | ||
| ? { [directUrlEnvVar]: process.env[directUrlEnvVar] } | ||
| : {} | ||
|
|
||
| execa.sync(`yarn rw`, command, { | ||
| cwd: cedarPaths.api.base, | ||
| stdio: 'inherit', | ||
| shell: true, | ||
| env: { | ||
| DATABASE_URL: process.env.DATABASE_URL, | ||
| ...directUrlDefinition, | ||
| }, | ||
| }) | ||
|
|
||
| return { | ||
| teardown() {}, | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| export default CedarApiVitestEnvironment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export { autoImportsPlugin } from './vite-plugin-auto-import.js' | ||
| export { cedarVitestApiConfigPlugin } from './vite-plugin-cedar-vitest-api-config.js' | ||
| export { trackDbImportsPlugin } from './vite-plugin-track-db-imports.js' |
36 changes: 36 additions & 0 deletions
36
packages/testing/src/api/vitest/vite-plugin-auto-import.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import autoImport from 'unplugin-auto-import/vite' | ||
|
|
||
| export function autoImportsPlugin() { | ||
| return autoImport({ | ||
| // targets to transform | ||
| include: [ | ||
| /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx | ||
| ], | ||
|
|
||
| // global imports to register | ||
| imports: [ | ||
| // import { mockContext, mockHttpEvent, mockSignedWebhook } from '@cedarjs/testing/api'; | ||
| { | ||
| '@cedarjs/testing/api': [ | ||
| 'mockContext', | ||
| 'mockHttpEvent', | ||
| 'mockSignedWebhook', | ||
| ], | ||
| }, | ||
| // import { gql } from 'graphql-tag' | ||
| { | ||
| 'graphql-tag': ['gql'], | ||
| }, | ||
| // import { context } from '@cedarjs/context' | ||
| { | ||
| '@cedarjs/context': ['context'], | ||
| }, | ||
| ], | ||
|
|
||
| // We provide our mocking types elsewhere and so don't need this plugin to | ||
| // generate them. | ||
| // TODO: Maybe we should have it at least generate the types for the gql | ||
| // import? (Or do we already provide that some other way?) | ||
| dts: false, | ||
| }) | ||
| } |
35 changes: 35 additions & 0 deletions
35
packages/testing/src/api/vitest/vite-plugin-cedar-vitest-api-config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import path from 'node:path' | ||
|
|
||
| import type { Plugin } from 'vite' | ||
|
|
||
| import { getEnvVarDefinitions, getPaths } from '@cedarjs/project-config' | ||
|
|
||
| export function cedarVitestApiConfigPlugin(): Plugin { | ||
| return { | ||
| name: 'cedar-vitest-plugin', | ||
| config: () => { | ||
| return { | ||
| define: getEnvVarDefinitions(), | ||
| ssr: { | ||
| noExternal: ['@cedarjs/testing'], | ||
| }, | ||
| resolve: { | ||
| alias: { | ||
| src: getPaths().api.src, | ||
| }, | ||
| }, | ||
| test: { | ||
| environment: path.join(import.meta.dirname, 'CedarApiVitestEnv.js'), | ||
| // fileParallelism: false, | ||
| // fileParallelism doesn't work with vitest projects (which is what | ||
| // we're using in the root vitest.config.ts). As a workaround we set | ||
| // poolOptions instead, which also shouldn't work, but was suggested | ||
| // by Vitest team member AriPerkkio (Hiroshi's answer didn't work). | ||
| // https://github.com/vitest-dev/vitest/discussions/7416 | ||
| poolOptions: { forks: { singleFork: true } }, | ||
| setupFiles: [path.join(import.meta.dirname, 'vitest-api.setup.js')], | ||
| }, | ||
| } | ||
| }, | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
packages/testing/src/api/vitest/vite-plugin-track-db-imports.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { Plugin } from 'vite' | ||
|
|
||
| export function trackDbImportsPlugin(): Plugin { | ||
| return { | ||
| name: 'db-import-tracker', | ||
| transform(code, id) { | ||
| // This regex and code content check could potentially match other files. | ||
| // It's very unlikely, but it is possible. For now this is good enough | ||
| if ( | ||
| id.match(/\/api\/src\/lib\/db\.(js|ts)$/) && | ||
| code.includes('PrismaClient') | ||
| ) { | ||
| // Inserting the code last (instead of at the top) works nicer with | ||
| // sourcemaps | ||
| return ( | ||
| code + | ||
| ` | ||
| ;if (typeof globalThis !== "undefined") { | ||
| globalThis.__cedarjs_db_imported__ = true; | ||
| } else { | ||
| throw new Error( | ||
| "vite-plugin-track-db-imports: globalThis is undefined. " + | ||
| "This is an error with CedarJS" | ||
| ); | ||
| } | ||
| ` | ||
| ) | ||
| } | ||
|
|
||
| return code | ||
| }, | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Returning
mockContextinstead ofnewContextcreates inconsistency with production behavior wheresetContextreturns the new context proxy