-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathvitest-debug.mjs
55 lines (45 loc) · 1.3 KB
/
vitest-debug.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { spawn } from 'child_process';
import fs from 'fs';
import path from 'path';
// Get the test file from the command-line arguments
const testFile = process.argv[2];
// Function to find the nearest package directory
function findPackageDir(dir) {
if (fs.existsSync(path.join(dir, 'package.json'))) {
return dir;
}
const parentDir = path.dirname(dir);
if (parentDir === dir) {
// Reached the root directory
return null;
}
return findPackageDir(parentDir);
}
const testFilePath = path.resolve(testFile);
const packageDir = findPackageDir(path.dirname(testFilePath));
if (!packageDir) {
console.error('Could not find package directory for', testFilePath);
process.exit(1);
}
// Path to Vitest
const vitestPath = path.resolve(packageDir, 'node_modules', 'vitest', 'vitest.mjs');
// Check if Vitest exists in the package
if (!fs.existsSync(vitestPath)) {
console.error('Vitest not found in', packageDir);
process.exit(1);
}
// Build the Vitest command
const args = [vitestPath, 'run', testFilePath, '--no-coverage'];
// Set environment variables
const env = Object.assign({}, process.env, {
NODE_ENV: 'test',
});
// Spawn the Vitest process
const vitest = spawn('node', args, {
cwd: packageDir,
env,
stdio: 'inherit',
});
vitest.on('close', code => {
process.exit(code);
});