Skip to content

Commit 9433ca5

Browse files
author
Szymon.Poltorak
committed
fix: path resolution #5
1 parent 3cd092b commit 9433ca5

File tree

2 files changed

+78
-44
lines changed

2 files changed

+78
-44
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function sanitizeOverrides(overrides: VitestOverrides): VitestOverrides {
200200
return { ...overrides, test: sanitizedTest };
201201
}
202202

203-
function getProjectRootPath(projectRootUrl: URL): string {
203+
export function getProjectRootPath(projectRootUrl: URL): string {
204204
try {
205205
return fileURLToPath(projectRootUrl);
206206
} catch {

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

Lines changed: 77 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type VitestConfigFactoryOptions,
88
type VitestOverrides,
99
createVitestConfig,
10+
getProjectRootPath,
1011
} from './vitest-config-factory.js';
1112

1213
// Only mock defineConfig - assume it works correctly, we're not testing Vite
@@ -27,17 +28,11 @@ vi.mock('./vitest-tsconfig-path-aliases.js', () => ({
2728
const MOCK_PROJECT_ROOT_STRING = '/Users/test/project';
2829
const MOCK_PROJECT_ROOT_URL = pathToFileURL(`${MOCK_PROJECT_ROOT_STRING}/`);
2930

30-
// Cross-platform path helpers to match what the actual code generates
31+
// Simple path helpers - just use them directly in tests!
3132
const mockPath = (...segments: string[]) =>
3233
path.resolve(MOCK_PROJECT_ROOT_STRING, ...segments);
33-
const mockCacheDir = (name: string) => mockPath('node_modules', '.vite', name);
34-
const mockVitestCacheDir = () => mockPath('node_modules', '.vitest');
35-
const mockGlobalSetup = () => mockPath('global-setup.ts');
36-
const mockReportsDir = (projectKey: string, kind: string) =>
37-
kind === 'e2e'
38-
? mockPath('e2e', projectKey, '.coverage')
39-
: mockPath('packages', projectKey, '.coverage', `${kind}-tests`);
40-
const mockSetupFile = mockPath;
34+
const mockUrlPath = (url: URL, ...segments: string[]) =>
35+
path.resolve(getProjectRootPath(url), ...segments);
4136

4237
const TEST_TIMEOUTS = {
4338
SHORT: 5000,
@@ -74,23 +69,28 @@ describe('createVitestConfig', () => {
7469

7570
expect(config).toEqual(
7671
expect.objectContaining({
77-
cacheDir: mockCacheDir('test-package'),
72+
cacheDir: mockPath('node_modules', '.vite', 'test-package'),
7873
test: expect.objectContaining({
7974
reporters: ['basic'],
8075
globals: true,
8176
cache: {
82-
dir: mockVitestCacheDir(),
77+
dir: mockPath('node_modules', '.vitest'),
8378
},
8479
alias: expect.any(Object),
8580
pool: 'threads',
8681
poolOptions: { threads: { singleThread: true } },
8782
environment: 'node',
8883
include: EXPECTED_INCLUDES.unit,
89-
globalSetup: [mockGlobalSetup()],
84+
globalSetup: [mockPath('global-setup.ts')],
9085
setupFiles: [],
9186
coverage: expect.objectContaining({
9287
reporter: ['text', 'lcov'],
93-
reportsDirectory: mockReportsDir('test-package', 'unit'),
88+
reportsDirectory: mockPath(
89+
'packages',
90+
'test-package',
91+
'.coverage',
92+
'unit-tests',
93+
),
9494
exclude: DEFAULT_EXCLUDES,
9595
}),
9696
}),
@@ -111,10 +111,17 @@ describe('createVitestConfig', () => {
111111

112112
expect(config).toEqual(
113113
expect.objectContaining({
114-
cacheDir: mockCacheDir('test-package'),
114+
cacheDir: mockUrlPath(
115+
MOCK_PROJECT_ROOT_URL,
116+
'node_modules',
117+
'.vite',
118+
'test-package',
119+
),
115120
test: expect.objectContaining({
116121
include: EXPECTED_INCLUDES.unit,
117-
globalSetup: [mockGlobalSetup()],
122+
globalSetup: [
123+
mockUrlPath(MOCK_PROJECT_ROOT_URL, 'global-setup.ts'),
124+
],
118125
}),
119126
}),
120127
);
@@ -161,9 +168,14 @@ describe('createVitestConfig', () => {
161168
expect.objectContaining({
162169
test: expect.objectContaining({
163170
include: EXPECTED_INCLUDES.int,
164-
globalSetup: [mockGlobalSetup()],
171+
globalSetup: [mockPath('global-setup.ts')],
165172
coverage: expect.objectContaining({
166-
reportsDirectory: mockReportsDir('test-package', 'int'),
173+
reportsDirectory: mockPath(
174+
'packages',
175+
'test-package',
176+
'.coverage',
177+
'int-tests',
178+
),
167179
}),
168180
}),
169181
}),
@@ -215,7 +227,7 @@ describe('createVitestConfig', () => {
215227
globalSetup: undefined,
216228
coverage: expect.objectContaining({
217229
reporter: ['text', 'lcov'],
218-
reportsDirectory: mockReportsDir('test-package', 'e2e'),
230+
reportsDirectory: mockPath('e2e', 'test-package', '.coverage'),
219231
exclude: DEFAULT_EXCLUDES,
220232
}),
221233
}),
@@ -237,7 +249,7 @@ describe('createVitestConfig', () => {
237249

238250
expect(config).toEqual(
239251
expect.objectContaining({
240-
cacheDir: mockCacheDir('custom-cache-key'),
252+
cacheDir: mockPath('node_modules', '.vite', 'custom-cache-key'),
241253
}),
242254
);
243255
});
@@ -253,7 +265,7 @@ describe('createVitestConfig', () => {
253265

254266
expect(config).toEqual(
255267
expect.objectContaining({
256-
cacheDir: mockCacheDir('test-package'),
268+
cacheDir: mockPath('node_modules', '.vite', 'test-package'),
257269
}),
258270
);
259271
});
@@ -278,7 +290,7 @@ describe('createVitestConfig', () => {
278290
expect(config).toEqual(
279291
expect.objectContaining({
280292
test: expect.objectContaining({
281-
setupFiles: [mockSetupFile('setup.ts')],
293+
setupFiles: [mockPath('setup.ts')],
282294
}),
283295
}),
284296
);
@@ -302,10 +314,7 @@ describe('createVitestConfig', () => {
302314
expect(config).toEqual(
303315
expect.objectContaining({
304316
test: expect.objectContaining({
305-
setupFiles: [
306-
mockSetupFile('setup1.ts'),
307-
mockSetupFile('setup2.ts'),
308-
],
317+
setupFiles: [mockPath('setup1.ts'), mockPath('setup2.ts')],
309318
}),
310319
}),
311320
);
@@ -330,9 +339,9 @@ describe('createVitestConfig', () => {
330339
expect.objectContaining({
331340
test: expect.objectContaining({
332341
setupFiles: [
333-
mockSetupFile('setup1.ts'),
334-
mockSetupFile('setup2.ts'),
335-
mockSetupFile('setup3.ts'),
342+
mockPath('setup1.ts'),
343+
mockPath('setup2.ts'),
344+
mockPath('setup3.ts'),
336345
],
337346
}),
338347
}),
@@ -405,7 +414,12 @@ describe('createVitestConfig', () => {
405414
test: expect.objectContaining({
406415
coverage: expect.objectContaining({
407416
reporter: ['text', 'lcov'],
408-
reportsDirectory: mockReportsDir('test-package', 'unit'),
417+
reportsDirectory: mockPath(
418+
'packages',
419+
'test-package',
420+
'.coverage',
421+
'unit-tests',
422+
),
409423
exclude: [...DEFAULT_EXCLUDES, 'custom/**', 'ignore/**'],
410424
}),
411425
}),
@@ -435,7 +449,12 @@ describe('createVitestConfig', () => {
435449
test: expect.objectContaining({
436450
coverage: expect.objectContaining({
437451
reporter: ['text', 'lcov'],
438-
reportsDirectory: mockReportsDir('test-package', 'unit'),
452+
reportsDirectory: mockPath(
453+
'packages',
454+
'test-package',
455+
'.coverage',
456+
'unit-tests',
457+
),
439458
exclude: DEFAULT_EXCLUDES,
440459
}),
441460
}),
@@ -492,7 +511,12 @@ describe('createVitestConfig', () => {
492511
const config = createVitestConfig(options, overrides);
493512
expectCoverageConfig(config, {
494513
reporter: ['text', 'lcov', 'html', 'json'],
495-
reportsDirectory: mockReportsDir('test-package', 'unit'),
514+
reportsDirectory: mockPath(
515+
'packages',
516+
'test-package',
517+
'.coverage',
518+
'unit-tests',
519+
),
496520
exclude: [...DEFAULT_EXCLUDES, 'custom/**'],
497521
thresholds: {
498522
global: {
@@ -548,9 +572,7 @@ describe('createVitestConfig', () => {
548572
const config = createVitestConfig(options, overrides);
549573

550574
const testConfig = (config as any).test;
551-
expect(testConfig.setupFiles).toEqual([
552-
mockSetupFile('should-be-removed.ts'),
553-
]);
575+
expect(testConfig.setupFiles).toEqual([mockPath('should-be-removed.ts')]);
554576
expect(testConfig.testTimeout).toBe(TEST_TIMEOUTS.SHORT);
555577
expect(testConfig.pool).toBe('forks');
556578
});
@@ -592,7 +614,12 @@ describe('createVitestConfig', () => {
592614
expect((config as any).test.testTimeout).toBe(TEST_TIMEOUTS.SHORT);
593615
expectCoverageConfig(config, {
594616
reporter: ['text', 'lcov'],
595-
reportsDirectory: mockReportsDir('test-package', 'unit'),
617+
reportsDirectory: mockPath(
618+
'packages',
619+
'test-package',
620+
'.coverage',
621+
'unit-tests',
622+
),
596623
exclude: DEFAULT_EXCLUDES,
597624
});
598625
});
@@ -616,7 +643,12 @@ describe('createVitestConfig', () => {
616643
expect((config as any).test.testTimeout).toBe(TEST_TIMEOUTS.SHORT);
617644
expectCoverageConfig(config, {
618645
reporter: ['text', 'lcov'],
619-
reportsDirectory: mockReportsDir('test-package', 'unit'),
646+
reportsDirectory: mockPath(
647+
'packages',
648+
'test-package',
649+
'.coverage',
650+
'unit-tests',
651+
),
620652
exclude: DEFAULT_EXCLUDES,
621653
});
622654
});
@@ -645,7 +677,7 @@ describe('createVitestConfig', () => {
645677
expectedIncludes[kind],
646678
);
647679
expect((config as any).test.globalSetup).toStrictEqual(
648-
kind === 'e2e' ? undefined : [mockGlobalSetup()],
680+
kind === 'e2e' ? undefined : [mockPath('global-setup.ts')],
649681
);
650682
});
651683
});
@@ -686,21 +718,23 @@ describe('createVitestConfig', () => {
686718

687719
expect(config).toEqual(
688720
expect.objectContaining({
689-
cacheDir: `${MOCK_PROJECT_ROOT_STRING}/node_modules/.vite/complex-scenario`,
721+
cacheDir: mockPath('node_modules', '.vite', 'complex-scenario'),
690722
build: {
691723
target: 'es2020',
692724
},
693725
test: expect.objectContaining({
694-
setupFiles: [
695-
mockSetupFile('setup1.ts'),
696-
mockSetupFile('setup2.ts'),
697-
],
726+
setupFiles: [mockPath('setup1.ts'), mockPath('setup2.ts')],
698727
testTimeout: TEST_TIMEOUTS.LONG,
699728
environment: 'jsdom',
700729
include: EXPECTED_INCLUDES.int,
701730
coverage: expect.objectContaining({
702731
exclude: ['mocks/**', '**/types.ts', 'e2e/**', 'dist/**'],
703-
reportsDirectory: mockReportsDir('test-package', 'int'),
732+
reportsDirectory: mockPath(
733+
'packages',
734+
'test-package',
735+
'.coverage',
736+
'int-tests',
737+
),
704738
}),
705739
}),
706740
}),

0 commit comments

Comments
 (0)