Skip to content

Commit d857c8f

Browse files
committed
test: extract shared utils
1 parent ce829a1 commit d857c8f

File tree

3 files changed

+70
-69
lines changed

3 files changed

+70
-69
lines changed

tests-svelte5/vite.config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { svelte } from "@sveltejs/vite-plugin-svelte";
22
import { defineConfig } from "vitest/config";
3-
import { generateAliasesFromExports, getDirname } from "../vite.config";
3+
import {
4+
generateAliasesFromExports,
5+
getDirname,
6+
testConfig,
7+
} from "../tests/utils";
48

59
const __dirname = getDirname(import.meta.url);
610

@@ -17,11 +21,7 @@ export default defineConfig({
1721
},
1822
},
1923
test: {
20-
globals: true,
21-
environment: "jsdom",
22-
clearMocks: true,
23-
// Suppress `console` output in CI.
24-
silent: !!process.env.CI,
24+
...testConfig,
2525
include: ["../tests/**/*.test.ts"],
2626
setupFiles: ["./setup-tests.ts"],
2727
},

tests/utils.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
import pkg from "../package.json";
5+
6+
export function getDirname(url: string): string {
7+
return path.dirname(fileURLToPath(url));
8+
}
9+
10+
/**
11+
* Generates Vite aliases from package.json exports for component subpath imports.
12+
* Resolves imports like `carbon-components-svelte/Theme/Theme.svelte` to
13+
* `./src/Theme/Theme.svelte` since these subpaths aren't in package.json
14+
* exports and Vite needs runtime resolution (tsconfig only handles types).
15+
*/
16+
export function generateAliasesFromExports(
17+
baseDir: string,
18+
srcRelativePath: string = "./src",
19+
) {
20+
const aliases: Record<string, string> = {};
21+
const exports = pkg.exports;
22+
23+
const srcSvelteExport = exports["./src/*.svelte"];
24+
if (!srcSvelteExport) return aliases;
25+
26+
const srcDir = path.resolve(baseDir, srcRelativePath);
27+
if (!fs.existsSync(srcDir)) return aliases;
28+
29+
function scanDirectory(dir: string, basePath: string = "") {
30+
const entries = fs.readdirSync(dir, { withFileTypes: true });
31+
32+
for (const entry of entries) {
33+
const fullPath = path.join(dir, entry.name);
34+
const relativePath = path.join(basePath, entry.name);
35+
36+
if (entry.isDirectory()) {
37+
scanDirectory(fullPath, relativePath);
38+
} else if (entry.isFile() && entry.name.endsWith(".svelte")) {
39+
const importPath = relativePath;
40+
const aliasKey = `${pkg.name}/${importPath}`;
41+
aliases[aliasKey] = path.resolve(baseDir, srcRelativePath, importPath);
42+
}
43+
}
44+
}
45+
46+
scanDirectory(srcDir);
47+
return aliases;
48+
}
49+
50+
export const testConfig = {
51+
globals: true,
52+
environment: "jsdom",
53+
clearMocks: true,
54+
// Suppress `console` output in CI.
55+
silent: !!process.env.CI,
56+
};

vite.config.ts

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,22 @@
1-
import fs from "node:fs";
2-
import path from "node:path";
3-
import { fileURLToPath } from "node:url";
41
import { svelte, vitePreprocess } from "@sveltejs/vite-plugin-svelte";
52
import { defineConfig } from "vitest/config";
6-
import pkg from "./package.json";
3+
import {
4+
generateAliasesFromExports,
5+
getDirname,
6+
testConfig,
7+
} from "./tests/utils";
78

8-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
9-
10-
/**
11-
* Gets the directory name from import.meta.url (ESM equivalent of __dirname).
12-
* @param url - The import.meta.url from the calling file
13-
* @returns The directory path
14-
*/
15-
export function getDirname(url: string): string {
16-
return path.dirname(fileURLToPath(url));
17-
}
18-
19-
/**
20-
* Generates Vite aliases from package.json exports for component subpath imports.
21-
* Resolves imports like `carbon-components-svelte/Theme/Theme.svelte` to
22-
* `./src/Theme/Theme.svelte` since these subpaths aren't in package.json
23-
* exports and Vite needs runtime resolution (tsconfig only handles types).
24-
*
25-
* @param baseDir - Base directory for resolving src path (defaults to __dirname)
26-
* @param srcRelativePath - Relative path to src directory from baseDir (defaults to "./src")
27-
*/
28-
export function generateAliasesFromExports(
29-
baseDir: string = __dirname,
30-
srcRelativePath: string = "./src",
31-
) {
32-
const aliases: Record<string, string> = {};
33-
const exports = pkg.exports;
34-
35-
const srcSvelteExport = exports["./src/*.svelte"];
36-
if (!srcSvelteExport) return aliases;
37-
38-
const srcDir = path.resolve(baseDir, srcRelativePath);
39-
if (!fs.existsSync(srcDir)) return aliases;
40-
41-
function scanDirectory(dir: string, basePath: string = "") {
42-
const entries = fs.readdirSync(dir, { withFileTypes: true });
43-
44-
for (const entry of entries) {
45-
const fullPath = path.join(dir, entry.name);
46-
const relativePath = path.join(basePath, entry.name);
47-
48-
if (entry.isDirectory()) {
49-
scanDirectory(fullPath, relativePath);
50-
} else if (entry.isFile() && entry.name.endsWith(".svelte")) {
51-
const importPath = relativePath;
52-
const aliasKey = `${pkg.name}/${importPath}`;
53-
aliases[aliasKey] = path.resolve(baseDir, srcRelativePath, importPath);
54-
}
55-
}
56-
}
57-
58-
scanDirectory(srcDir);
59-
return aliases;
60-
}
9+
const __dirname = getDirname(import.meta.url);
6110

6211
export default defineConfig({
6312
root: "./tests",
6413
plugins: [svelte({ preprocess: [vitePreprocess()] })],
6514
resolve: {
6615
conditions: ["browser"],
67-
alias: generateAliasesFromExports(),
16+
alias: generateAliasesFromExports(__dirname),
6817
},
6918
test: {
70-
globals: true,
71-
environment: "jsdom",
72-
clearMocks: true,
73-
// Suppress `console` output in CI.
74-
silent: !!process.env.CI,
19+
...testConfig,
7520
setupFiles: ["./tests/setup-tests.ts"],
7621
},
7722
});

0 commit comments

Comments
 (0)