Skip to content

Conversation

poltorak
Copy link
Collaborator

@poltorak poltorak commented Sep 5, 2025

Centralize and Standardize Vitest Configurations

What Changed

  • New centralized configuration system: Created testing/test-setup-config package with a factory-based approach for generating Vitest configurations
  • Setup presets: Introduced modular setup file presets that can be composed for different test types (unit, integration, e2e)
  • Simplified configuration files: Refactored 6 existing Vitest config files to use the new centralized system, reducing them from verbose configurations to simple function calls

Benefits

  • Eliminates duplication: Removes repetitive boilerplate across multiple vitest config files
  • Ensures consistency: Standardizes test configurations, coverage settings, and setup files across all packages
  • Improves maintainability: Changes to test configuration defaults now only need to be made in one place
  • Better developer experience: New configs are more readable and easier to customize with sensible defaults and preset combinations

The new system provides createUnitConfig, createIntConfig, and createE2eConfig factory functions that automatically handle common configuration patterns while still allowing customization through override parameters.

Relates to first part of #1065

@github-actions github-actions bot added 📖 Project documentation improvements or additions to the project documentation 🔬 testing writing tests 🛠️ tooling labels Sep 5, 2025
Copy link

nx-cloud bot commented Sep 5, 2025

View your CI Pipeline Execution ↗ for commit 9433ca5

Command Status Duration Result
nx code-pushup --nx-bail -- compare ✅ Succeeded 1m 4s View ↗
nx code-pushup --nx-bail -- ✅ Succeeded 1m 27s View ↗
nx code-pushup --nx-bail -- print-config --outp... ✅ Succeeded 4m 23s View ↗

☁️ Nx Cloud last updated this comment at 2025-09-09 14:17:53 UTC

Copy link

pkg-pr-new bot commented Sep 5, 2025

Open in StackBlitz

@code-pushup/ci

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/ci@1107

@code-pushup/cli

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/cli@1107

@code-pushup/create-cli

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/create-cli@1107

@code-pushup/core

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/core@1107

@code-pushup/models

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/models@1107

@code-pushup/nx-plugin

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/nx-plugin@1107

@code-pushup/coverage-plugin

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/coverage-plugin@1107

@code-pushup/eslint-plugin

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/eslint-plugin@1107

@code-pushup/js-packages-plugin

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/js-packages-plugin@1107

@code-pushup/jsdocs-plugin

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/jsdocs-plugin@1107

@code-pushup/lighthouse-plugin

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/lighthouse-plugin@1107

@code-pushup/typescript-plugin

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/typescript-plugin@1107

@code-pushup/utils

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/utils@1107

@code-pushup/models-transformers

npm i https://pkg.pr.new/code-pushup/cli/@code-pushup/models-transformers@1107

commit: 9433ca5

Copy link

github-actions bot commented Sep 5, 2025

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

🏷️ Category ⭐ Previous score ⭐ Current score 🔄 Score change
Performance 🔴 37 🔴 36 ↓ −1.8
Code coverage 🟡 90 🟡 90 ↓ −0.1
Security 🟡 64 🟡 64
Updates 🟡 85 🟡 85
Accessibility 🟢 92 🟢 92
Best Practices 🟢 100 🟢 100
SEO 🟡 61 🟡 61
Type Safety 🟢 100 🟢 100
Bug prevention 🟢 100 🟢 100
Miscellaneous 🟢 100 🟢 100
Code style 🟢 100 🟢 100
Documentation 🔴 24 🔴 24
👎 2 groups regressed, 👍 4 audits improved, 👎 5 audits regressed, 12 audits changed without impacting score

🗃️ Groups

🔌 Plugin 🗃️ Group ⭐ Previous score ⭐ Current score 🔄 Score change
Lighthouse Performance 🔴 37 🔴 36 ↓ −1.8
Code coverage Code coverage metrics 🟡 90 🟡 90 ↓ −0.1

19 other groups are unchanged.

🛡️ Audits

🔌 Plugin 🛡️ Audit 📏 Previous value 📏 Current value 🔄 Value change
Lighthouse Initial server response time was short 🟥 Root document took 650 ms 🟩 Root document took 540 ms ↓ −17 %
Lighthouse Total Blocking Time 🟥 1,640 ms 🟥 2,400 ms ↑ +46.6 %
Lighthouse First Contentful Paint 🟥 3.2 s 🟨 3.0 s ↓ −6.1 %
Lighthouse Speed Index 🟥 6.2 s 🟥 6.4 s ↑ +3.4 %
Lighthouse Time to Interactive 🟥 16.0 s 🟥 17.0 s ↑ +6.5 %
Code coverage Line coverage 🟨 86.3 % 🟨 84.4 % ↓ −2.1 %
Lighthouse Max Potential First Input Delay 🟥 800 ms 🟥 920 ms ↑ +14.9 %
Code coverage Function coverage 🟩 92.3 % 🟩 92.4 % ↑ +0.1 %
Code coverage Branch coverage 🟨 85.5 % 🟨 85.5 % ↑ +0.1 %
Lighthouse Avoids enormous network payloads 🟨 Total size was 2,687 KiB 🟨 Total size was 2,724 KiB ↑ +1.4 %
Lighthouse Minimizes main-thread work 🟥 9.6 s 🟥 11.5 s ↑ +19.8 %
Lighthouse Uses efficient cache policy on static assets 🟨 30 resources found 🟨 31 resources found ↓ −0.2 %
Lighthouse Metrics 🟩 100% 🟩 100% ↑ +6.5 %
Lighthouse JavaScript execution time 🟥 4.2 s 🟥 4.9 s ↑ +18.3 %
Lighthouse Largest Contentful Paint 🟥 11.0 s 🟥 11.2 s ↑ +2.4 %
Lighthouse Server Backend Latencies 🟩 1,540 ms 🟩 1,450 ms ↓ −5.8 %
Lighthouse Reduce unused JavaScript 🟥 Potential savings of 602 KiB 🟥 Potential savings of 598 KiB ↓ −1.1 %
Lighthouse Avoids an excessive DOM size 🟥 2,296 elements 🟥 2,303 elements ↑ +0.3 %
JS Packages Outdated NPM dev dependencies. 🟨 50 outdated package versions (21 major, 23 minor, 6 patch) 🟨 56 outdated package versions (21 major, 29 minor, 6 patch) ↑ +12 %
JS Packages Outdated NPM prod dependencies. 🟨 14 outdated package versions (3 major, 6 minor, 5 patch) 🟨 16 outdated package versions (3 major, 8 minor, 5 patch) ↑ +14.3 %
Lighthouse Network Round Trip Times 🟩 70 ms 🟩 70 ms ↑ +0.2 %

589 other audits are unchanged.

Copy link
Collaborator

@BioPhoton BioPhoton left a 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'],
Copy link
Collaborator

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?

projectRoot: new URL('../../', import.meta.url),
setupFiles: [
'testing/test-setup/src/lib/cliui.mock.ts',
'testing/test-setup/src/lib/fs.mock.ts',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be default

'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',
Copy link
Collaborator

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

Comment on lines 13 to 14
'testing/test-setup/src/lib/extend/path.matcher.ts',
'testing/test-setup/src/lib/extend/jest-extended.matcher.ts',
Copy link
Collaborator

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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Collaborator

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
Comment on lines 37 to 61
### 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.*`
Copy link
Collaborator

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
Comment on lines 63 to 78
### 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'],
});
```
Copy link
Collaborator

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
Comment on lines 110 to 123

`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.
Copy link
Collaborator

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 */})

Comment on lines 30 to 32
export function createVitestConfig(
options: CreateVitestConfigOptions,
): ViteUserConfig {
Copy link
Collaborator

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.

Comment on lines 138 to 151
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 });
Copy link
Collaborator

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

include?: string[];
setupFiles?: string[];
/** If true, the factory will not inject the baseline setupFiles for the given kind. */
overrideSetupFiles?: boolean;
Copy link
Collaborator

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.

@poltorak poltorak marked this pull request as ready for review September 10, 2025 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📖 Project documentation improvements or additions to the project documentation 🔬 testing writing tests 🛠️ tooling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants