Skip to content

Commit 24c3c14

Browse files
committed
fix(cli): add CJS/MJS config file tests and improve catch comment
1 parent 24fb2f7 commit 24c3c14

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

packages/cli/src/__tests__/resolve-vite-config.spec.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import fs from 'node:fs';
2+
import { mkdtempSync } from 'node:fs';
3+
import { tmpdir } from 'node:os';
24
import path from 'node:path';
35

46
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
5-
import { mkdtempSync } from 'node:fs';
6-
import { tmpdir } from 'node:os';
77

88
import { findViteConfigUp } from '../resolve-vite-config';
99

1010
describe('findViteConfigUp', () => {
1111
let tempDir: string;
1212

1313
beforeEach(() => {
14-
tempDir = mkdtempSync(path.join(tmpdir(), 'vite-config-test-'));
14+
// Resolve symlinks (macOS /var -> /private/var) to match path.resolve behavior
15+
tempDir = fs.realpathSync(mkdtempSync(path.join(tmpdir(), 'vite-config-test-')));
1516
});
1617

1718
afterEach(() => {
@@ -89,4 +90,31 @@ describe('findViteConfigUp', () => {
8990
const result = findViteConfigUp(subDir, tempDir);
9091
expect(result).toBe(path.join(tempDir, 'vite.config.mts'));
9192
});
93+
94+
it('should find .cjs config files', () => {
95+
const subDir = path.join(tempDir, 'packages', 'my-lib');
96+
fs.mkdirSync(subDir, { recursive: true });
97+
fs.writeFileSync(path.join(tempDir, 'vite.config.cjs'), '');
98+
99+
const result = findViteConfigUp(subDir, tempDir);
100+
expect(result).toBe(path.join(tempDir, 'vite.config.cjs'));
101+
});
102+
103+
it('should find .cts config files', () => {
104+
const subDir = path.join(tempDir, 'packages', 'my-lib');
105+
fs.mkdirSync(subDir, { recursive: true });
106+
fs.writeFileSync(path.join(tempDir, 'vite.config.cts'), '');
107+
108+
const result = findViteConfigUp(subDir, tempDir);
109+
expect(result).toBe(path.join(tempDir, 'vite.config.cts'));
110+
});
111+
112+
it('should find .mjs config files', () => {
113+
const subDir = path.join(tempDir, 'packages', 'my-lib');
114+
fs.mkdirSync(subDir, { recursive: true });
115+
fs.writeFileSync(path.join(tempDir, 'vite.config.mjs'), '');
116+
117+
const result = findViteConfigUp(subDir, tempDir);
118+
expect(result).toBe(path.join(tempDir, 'vite.config.mjs'));
119+
});
92120
});

packages/cli/src/resolve-vite-config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function findViteConfigUp(startDir: string, stopDir: string): string | un
2626
}
2727
}
2828
const parent = path.dirname(dir);
29-
if (parent === dir || !dir.startsWith(stop)) {
29+
if (parent === dir || !parent.startsWith(stop)) {
3030
break;
3131
}
3232
dir = parent;
@@ -56,7 +56,7 @@ function findWorkspaceRoot(startDir: string): string | undefined {
5656
return dir;
5757
}
5858
} catch {
59-
// ignore
59+
// Skip malformed package.json and continue searching parent directories
6060
}
6161
}
6262
if (fs.existsSync(path.join(dir, 'lerna.json'))) {

0 commit comments

Comments
 (0)