Skip to content

Instantly share code, notes, and snippets.

@andyjessop
Last active June 5, 2024 08:12
Show Gist options
  • Save andyjessop/e1d9edf8cc4d0f443a4ec27cdc9df056 to your computer and use it in GitHub Desktop.
Save andyjessop/e1d9edf8cc4d0f443a4ec27cdc9df056 to your computer and use it in GitHub Desktop.
Run package tests VSCode debug panel
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Current File",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}/.vscode/utils/run-test.js",
"args": ["${file}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"runtimeExecutable": "node",
"skipFiles": ["<node_internals>/**"]
}
]
}
// .vscode/utils/run-test.js
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
// Function to find the nearest package.json
function findNearestPackageJson(file) {
let dir = path.dirname(file);
while (dir !== path.resolve(dir, '..')) {
const packageJsonPath = path.join(dir, 'package.json');
if (fs.existsSync(packageJsonPath)) {
return packageJsonPath;
}
dir = path.resolve(dir, '..');
}
return null;
}
// Get the current file from arguments
const currentFile = process.argv[2];
const packageJsonPath = findNearestPackageJson(currentFile);
if (!packageJsonPath) {
console.error('No package.json found.');
process.exit(1);
}
const packageJson = require(packageJsonPath);
const packageName = packageJson.name;
const command = `pnpm`;
const args = ['test', '-F', packageName, '--', currentFile];
const testProcess = spawn(command, args, { stdio: 'inherit' });
testProcess.on('close', (code) => {
process.exit(code);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment