Skip to content

Commit 307b179

Browse files
author
Szymon.Poltorak
committed
fix: path resolution #2
1 parent 14ca642 commit 307b179

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { pathToFileURL } from 'node:url';
1+
import path from 'node:path';
2+
import { fileURLToPath, pathToFileURL } from 'node:url';
23
import {
34
type UserConfig as ViteUserConfig,
45
defineConfig,
@@ -68,7 +69,9 @@ function toAbsolutePaths(
6869
paths?: readonly string[],
6970
): string[] {
7071
return paths && paths.length > 0
71-
? paths.filter(Boolean).map(p => new URL(p, projectRootUrl).pathname)
72+
? paths
73+
.filter(Boolean)
74+
.map(p => path.resolve(fileURLToPath(projectRootUrl), p))
7275
: [];
7376
}
7477

@@ -92,7 +95,7 @@ function defaultGlobalSetup(
9295
): string[] | undefined {
9396
return kind === 'e2e'
9497
? undefined
95-
: [new URL('global-setup.ts', projectRootUrl).pathname];
98+
: [path.resolve(fileURLToPath(projectRootUrl), 'global-setup.ts')];
9699
}
97100

98101
function buildCoverageConfig(params: {
@@ -102,12 +105,12 @@ function buildCoverageConfig(params: {
102105
overrideExclude?: string[];
103106
}): CoverageOptions {
104107
const defaultExclude = ['mocks/**', '**/types.ts'];
105-
const reportsDirectory = new URL(
108+
const reportsDirectory = path.resolve(
109+
fileURLToPath(params.projectRootUrl),
106110
params.kind === 'e2e'
107111
? `e2e/${params.projectKey}/.coverage`
108112
: `packages/${params.projectKey}/.coverage/${params.kind}-tests`,
109-
params.projectRootUrl,
110-
).pathname;
113+
);
111114
return {
112115
reporter: ['text', 'lcov'],
113116
reportsDirectory,
@@ -128,15 +131,18 @@ function buildBaseConfig(params: {
128131
overrideExclude: string[];
129132
}): VitestOverrides {
130133
const cfg: VitestOverrides = {
131-
cacheDir: new URL(
134+
cacheDir: path.resolve(
135+
fileURLToPath(params.projectRootUrl),
132136
`node_modules/.vite/${params.cacheDirName}`,
133-
params.projectRootUrl,
134-
).pathname,
137+
),
135138
test: {
136139
reporters: ['basic'],
137140
globals: true,
138141
cache: {
139-
dir: new URL('node_modules/.vitest', params.projectRootUrl).pathname,
142+
dir: path.resolve(
143+
fileURLToPath(params.projectRootUrl),
144+
'node_modules/.vitest',
145+
),
140146
},
141147
alias: tsconfigPathAliases(params.projectRootUrl),
142148
pool: 'threads',

tools/vitest-tsconfig-path-aliases.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import path from 'node:path';
2+
import { fileURLToPath } from 'node:url';
13
import { loadConfig } from 'tsconfig-paths';
24
import type { Alias, AliasOptions } from 'vite';
35

46
export function tsconfigPathAliases(projectRootUrl?: URL): AliasOptions {
57
const tsconfigPath = projectRootUrl
6-
? new URL('tsconfig.base.json', projectRootUrl).pathname
8+
? path.resolve(fileURLToPath(projectRootUrl), 'tsconfig.base.json')
79
: 'tsconfig.base.json';
810
const result = loadConfig(tsconfigPath);
911
if (result.resultType === 'failed') {
@@ -17,7 +19,9 @@ export function tsconfigPathAliases(projectRootUrl?: URL): AliasOptions {
1719
.map(
1820
([importPath, relativePath]): Alias => ({
1921
find: importPath,
20-
replacement: new URL(`../${relativePath}`, import.meta.url).pathname,
22+
replacement: projectRootUrl
23+
? path.resolve(fileURLToPath(projectRootUrl), relativePath)
24+
: new URL(`../${relativePath}`, import.meta.url).pathname,
2125
}),
2226
);
2327
}

0 commit comments

Comments
 (0)