-
Notifications
You must be signed in to change notification settings - Fork 16
refactor: common vitest configurations #1107
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
base: main
Are you sure you want to change the base?
Conversation
View your CI Pipeline Execution ↗ for commit 9433ca5
☁️ Nx Cloud last updated this comment at |
@code-pushup/ci
@code-pushup/cli
@code-pushup/create-cli
@code-pushup/core
@code-pushup/models
@code-pushup/nx-plugin
@code-pushup/coverage-plugin
@code-pushup/eslint-plugin
@code-pushup/js-packages-plugin
@code-pushup/jsdocs-plugin
@code-pushup/lighthouse-plugin
@code-pushup/typescript-plugin
@code-pushup/utils
@code-pushup/models-transformers
commit: |
Code PushUp🤨 Code PushUp report has both improvements and regressions – compared current commit 0640602 with previous commit df16191. 🕵️ See full comparison in Code PushUp portal 🔍 🏷️ Categories👎 2 groups regressed, 👍 4 audits improved, 👎 5 audits regressed, 12 audits changed without impacting score🗃️ Groups
19 other groups are unchanged. 🛡️ Audits
589 other audits are unchanged. |
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.
Thanks for cleaning up our test config mess :)
I left some comments.
- please add a proper PR description that contains the scope and planned changes
- move the config code under a Nx project in
testing/vitest-setup
. I updated the issue accordingly.
environment: 'node', | ||
include: ['tests/**/*.e2e.test.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], | ||
globalSetup: './global-setup.ts', | ||
setupFiles: ['../../testing/test-setup/src/lib/reset.mocks.ts'], |
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.
Did you forgot to keep this?
packages/core/vitest.unit.config.ts
Outdated
projectRoot: new URL('../../', import.meta.url), | ||
setupFiles: [ | ||
'testing/test-setup/src/lib/cliui.mock.ts', | ||
'testing/test-setup/src/lib/fs.mock.ts', |
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.
This can be default
packages/core/vitest.unit.config.ts
Outdated
'testing/test-setup/src/lib/portal-client.mock.ts', | ||
'testing/test-setup/src/lib/extend/ui-logger.matcher.ts', | ||
'testing/test-setup/src/lib/extend/markdown-table.matcher.ts', | ||
'testing/test-setup/src/lib/extend/jest-extended.matcher.ts', |
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.
this could also be in defaults
packages/utils/vitest.unit.config.ts
Outdated
'testing/test-setup/src/lib/extend/path.matcher.ts', | ||
'testing/test-setup/src/lib/extend/jest-extended.matcher.ts', |
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.
Those could be defaults
tools/README.md
Outdated
@@ -0,0 +1,154 @@ | |||
## Vitest config factory and setup presets | |||
|
|||
This folder contains utilities to centralize and standardize Vitest configuration across the monorepo. |
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.
This folder contains utilities to centralize and standardize Vitest configuration across the monorepo. | |
This project contains utilities to centralize and standardize Vitest configuration across a monorepo. |
tools/README.md
Outdated
- `createIntConfig(projectKey, { projectRoot, ...options })` | ||
- `createE2eConfig(projectKey, { projectRoot, ...options })` | ||
|
||
`projectKey` is used for cache and coverage directories. |
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.
You could give an example
tools/README.md
Outdated
### Defaults | ||
|
||
Common to all kinds: | ||
|
||
- `reporters: ['basic']`, `globals: true`, `environment: 'node'` | ||
- `alias: tsconfigPathAliases()` | ||
- `pool: 'threads'` with `singleThread: true` | ||
- Cache directories resolved from `projectRoot` (absolute paths) | ||
|
||
Coverage: | ||
|
||
- Unit/Int: enabled by default, reports to `<projectRoot>/coverage/<project>/<kind>-tests` | ||
- E2E: disabled by default | ||
- Default exclude: `['mocks/**', '**/types.ts']` | ||
|
||
Global setup: | ||
|
||
- Unit/Int: `['<projectRoot>/global-setup.ts']` by default | ||
- E2E: none by default (set per-suite if needed) | ||
|
||
Include patterns: | ||
|
||
- Unit: `src/**/*.unit.test.*` | ||
- Int: `src/**/*.int.test.*` | ||
- E2E: `tests/**/*.e2e.test.*` |
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.
This information is tightly coupled to our implementation. MAybe link the perset directly here instead of repeating?
tools/README.md
Outdated
### setupFiles strategy | ||
|
||
Baseline `setupFiles` are injected automatically by kind: | ||
|
||
- Unit baseline: `console.mock.ts`, `reset.mocks.ts` | ||
- Int baseline: `console.mock.ts`, `reset.mocks.ts` | ||
- E2E baseline: `reset.mocks.ts` | ||
|
||
Extend with additional files using `options.setupFiles` — they append after the baseline (paths are project-root-relative): | ||
|
||
```ts | ||
export default createUnitConfig('core', { | ||
projectRoot: new URL('../../', import.meta.url), | ||
setupFiles: ['testing/test-setup/src/lib/cliui.mock.ts'], | ||
}); | ||
``` |
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.
Same here, strongly coupled to implementation that can change often.
tools/README.md
Outdated
|
||
`CreateVitestConfigOptions` (required + optional): | ||
|
||
- `projectKey` (string): coverage/cache naming. | ||
- `kind` ('unit' | 'int' | 'e2e'): test kind. | ||
- `projectRoot` (string | URL): absolute root for all paths. | ||
- `include?: string[]`: override default include globs. | ||
- `setupFiles?: string[]`: extra setup files (appended to baseline; project-root-relative). | ||
- `overrideSetupFiles?: boolean`: skip baseline and use only provided list. | ||
- `globalSetup?: string[]`: override default global setup (project-root-relative). | ||
- `coverage?: { enabled?, exclude?, reportsSubdir? }` | ||
- `testTimeout?: number`: e.g., for E2E. | ||
- `typecheckInclude?: string[]`: include patterns for Vitest typecheck. | ||
- `cacheKey?: string`: custom cache dir suffix. |
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.
The best DX is given if the user does no need to lear another type.
I would suggest try to keep the types as close as possible to existing virest types.
e.g. : createIntConfig({/*vitestTestConfig*/}, { projectKey: 'unit', /*other custom options we need that are not already inside vitest types */})
tools/vitest-config-factory.ts
Outdated
export function createVitestConfig( | ||
options: CreateVitestConfigOptions, | ||
): ViteUserConfig { |
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.
As mentioned in this comment I would use a API and more important data structure/types as lose as possible to vitest.
tools/vitest-config-factory.ts
Outdated
export const createUnitConfig = ( | ||
projectKey: string, | ||
rest: Omit<CreateVitestConfigOptions, 'projectKey' | 'kind'>, | ||
): ViteUserConfig => createVitestConfig({ projectKey, kind: 'unit', ...rest }); | ||
|
||
export const createIntConfig = ( | ||
projectKey: string, | ||
rest: Omit<CreateVitestConfigOptions, 'projectKey' | 'kind'>, | ||
): ViteUserConfig => createVitestConfig({ projectKey, kind: 'int', ...rest }); | ||
|
||
export const createE2eConfig = ( | ||
projectKey: string, | ||
rest: Omit<CreateVitestConfigOptions, 'projectKey' | 'kind'>, | ||
): ViteUserConfig => createVitestConfig({ projectKey, kind: 'e2e', ...rest }); |
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.
I would move all non abstract logic into vitest-setup-presets.ts
tools/vitest-config-factory.ts
Outdated
include?: string[]; | ||
setupFiles?: string[]; | ||
/** If true, the factory will not inject the baseline setupFiles for the given kind. */ | ||
overrideSetupFiles?: boolean; |
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.
I would only apply vitest helper and the special merge logic is handled in each project.
Centralize and Standardize Vitest Configurations
What Changed
Benefits
The new system provides
createUnitConfig
,createIntConfig
, andcreateE2eConfig
factory functions that automatically handle common configuration patterns while still allowing customization through override parameters.Relates to first part of #1065