Skip to content

Commit 7008a48

Browse files
author
Szymon.Poltorak
committed
refactor(testing): improve vitest config factory and test coverage
1 parent 96cb4b0 commit 7008a48

File tree

4 files changed

+390
-68
lines changed

4 files changed

+390
-68
lines changed

testing/test-setup-config/README.md

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,7 @@ Utilities to centralize and standardize Vitest configuration across the monorepo
55
- `vitest-config-factory.ts`: builds typed Vitest configs with sensible defaults
66
- `vitest-setup-presets.ts`: provides create functions and exportable setup file groups
77

8-
The create functions (`createUnitConfig`, `createIntConfig`, `createE2eConfig`) automatically include appropriate setup files for each test type.
9-
10-
### Defaults
11-
12-
**Common**: `reporters: ['basic']`, `globals: true`, `environment: 'node'`, `pool: 'threads'` with `singleThread: true`, alias from tsconfig paths
13-
14-
**Coverage**: Unit/Int enabled (reports to `<projectRoot>/packages/<project>/.coverage`), E2E disabled. Excludes `['mocks/**', '**/types.ts']`
15-
16-
**Global setup**: Unit/Int use `['<projectRoot>/global-setup.ts']`, E2E none by default
17-
18-
**Include patterns**: Unit `src/**/*.unit.test.*`, Int `src/**/*.int.test.*`, E2E `tests/**/*.e2e.test.*`
19-
20-
### Setup files
21-
22-
**Automatic inclusion**: Unit (console mocking, cleanup, UI/filesystem mocks, basic matchers), Int (console mocking, cleanup), E2E (cleanup only)
23-
24-
**Custom setup files**: ⚠️ Specifying `setupFiles` in overrides will completely replace the defaults. To extend the default list, manually combine them with `setupPresets`:
25-
26-
- `setupPresets.unit.{base, git, portalClient, matchersCore, matcherPath}`
27-
- `setupPresets.int.{base, cliui, fs, git, portalClient, matcherPath, chromePath}`
28-
- `setupPresets.e2e.{base}`
29-
30-
### Parameters
31-
32-
- `projectKey`: Used for cache and coverage directory naming
33-
- `projectRoot`: Required path/URL to the project root for resolving all paths
34-
- Standard Vitest configuration options can be provided in the overrides parameter
8+
The create functions (`createUnitConfig`, `createIntConfig`, `createE2eConfig`) automatically include appropriate setup files for each test type. See the unit tests for detailed documentation of defaults, coverage settings, and setup file presets.
359

3610
### Examples
3711

testing/test-setup-config/src/lib/vitest-config-factory.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,21 @@ import { tsconfigPathAliases } from './vitest-tsconfig-path-aliases.js';
1111
export type TestKind = 'unit' | 'int' | 'e2e';
1212

1313
export type VitestConfigFactoryOptions = {
14+
/**
15+
* Used for cache and coverage directory naming
16+
*/
1417
projectKey: string;
18+
/**
19+
* Required path/URL to the project root for resolving all paths
20+
*/
1521
kind: TestKind;
22+
/**
23+
* The root directory of the project
24+
*/
1625
projectRoot: string | URL;
26+
/**
27+
* Optional test cache directory name
28+
*/
1729
cacheKey?: string;
1830
};
1931

@@ -35,7 +47,7 @@ export function createVitestConfig(
3547
: `${options.projectRoot}/`,
3648
)
3749
: options.projectRoot;
38-
const cacheDirName = options.cacheKey ?? options.projectKey;
50+
const cacheDirName = options.cacheKey ?? `cache-${options.projectKey}`;
3951

4052
const coverageEnabled =
4153
overrides.test?.coverage?.enabled ?? options.kind !== 'e2e';

testing/test-setup-config/src/lib/vitest-config-factory.unit.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('createVitestConfig', () => {
6969

7070
expect(config).toEqual(
7171
expect.objectContaining({
72-
cacheDir: mockPath('node_modules', '.vite', 'test-package'),
72+
cacheDir: mockPath('node_modules', '.vite', 'cache-test-package'),
7373
test: expect.objectContaining({
7474
reporters: ['basic'],
7575
globals: true,
@@ -115,7 +115,7 @@ describe('createVitestConfig', () => {
115115
MOCK_PROJECT_ROOT_URL,
116116
'node_modules',
117117
'.vite',
118-
'test-package',
118+
'cache-test-package',
119119
),
120120
test: expect.objectContaining({
121121
include: EXPECTED_INCLUDES.unit,
@@ -254,7 +254,7 @@ describe('createVitestConfig', () => {
254254
);
255255
});
256256

257-
it('should fallback to projectKey when cacheKey is not provided', () => {
257+
it('should fallback to cache-{projectKey} when cacheKey is not provided', () => {
258258
const options: VitestConfigFactoryOptions = {
259259
projectKey: 'test-package',
260260
kind: 'unit',
@@ -265,7 +265,7 @@ describe('createVitestConfig', () => {
265265

266266
expect(config).toEqual(
267267
expect.objectContaining({
268-
cacheDir: mockPath('node_modules', '.vite', 'test-package'),
268+
cacheDir: mockPath('node_modules', '.vite', 'cache-test-package'),
269269
}),
270270
);
271271
});

0 commit comments

Comments
 (0)