vscode
vscode copied to clipboard
Tests executing child_process not working inside extension
Describe the bug
When I have a test using child_process, the test run and inside the extension explorer, but at a moment, the ui restart, and the test never stops.
Remarque: The test using child_process runs fine when I launch the debug.
Reproduction
The function to test
import { mkdir, rename, writeFile } from 'node:fs/promises';
import { basename, join } from 'node:path';
import { spawnSync } from 'node:child_process';
import { projectPackage } from './helpers';
/**
* Build a inner project with custom package.json to test some types with tsd
* @param indexFile the files that will be generated to index the project
* @param relativeDir The relative path to the project
* @param projectName : The name of the project
*/
export default async function createProject(
indexFile: string,
relativeDir: string,
projectName?: string,
) {
const dir = join(process.cwd(), relativeDir);
await mkdir(dir);
const json = projectPackage(projectName);
const packageFile = join(dir, 'package.json');
await writeFile(packageFile, JSON.stringify(json, null, 2), {
encoding: 'utf-8',
});
const tscFile = `./"${indexFile}"`;
spawnSync(
'pnpm tsup',
[
tscFile,
'--format',
'esm',
'--dts',
'--outDir',
relativeDir,
'--minify',
'--no-sourcemap',
'--target',
'esnext',
],
{
env: process.env,
stdio: 'inherit',
shell: true,
encoding: 'buffer',
},
);
const declaration = basename(indexFile.replace('.ts', '.d.ts'));
const resolvedDeclarationFile = join(dir, declaration);
const resolvedJsFile = resolvedDeclarationFile.replace('.d.ts', '.js');
await rename(resolvedDeclarationFile, join(dir, 'index.d.ts'));
await rename(resolvedJsFile, join(dir, 'index.js'));
await writeFile(join(dir, 'index.test-d.ts'), '');
}
The test file :
import { rm, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import tsd, { formatter } from 'tsd';
import createProject from './createProject';
const BUILD_DIR = 'build-test';
const TEST_TIMEOUT = 10_000;
describe('createProject', () => {
test(
'#1 => Create the project',
() => createProject('src/helpers.ts', BUILD_DIR),
TEST_TIMEOUT,
);
test(
'#2 => Create the test',
() => {
const write =
'import { expectAssignable } from "tsd"; import { projectPackage } from "./index"; expectAssignable<string>(projectPackage().name);';
return writeFile(
join(process.cwd(), BUILD_DIR, 'index.test-d.ts'),
write,
);
},
TEST_TIMEOUT,
);
test(
'#3 => Test tsd',
async () => {
const _tsd = await tsd({
cwd: BUILD_DIR,
});
const fd = formatter(_tsd);
expect(fd).toBe('');
},
TEST_TIMEOUT,
);
});
beforeAll(() => {
return rm(BUILD_DIR, { recursive: true, force: true });
});
afterAll(() => {
return rm(BUILD_DIR, { recursive: true, force: true });
});
Output
[INFO 14:52:34] [v0.12.0] Vitest extension is activated because Vitest is installed or there is a Vite/Vitest config file in the workspace.
[INFO 14:52:34] [API] Running Vitest v1.6.0 (tsd-cli/vitest.config.ts) with Node.js: /Users/chlbri/.nvm/versions/node/v20.9.0/lib/node_modules/npm/pnpm/node
[INFO 14:52:35] [API] Vitest v1.6.0 (tsd-cli/vitest.config.ts) process 42207 created
[INFO 14:52:35] [API] Collecting tests: src/createProject.test.ts
[INFO 14:52:38] Running 1 file(s) with name pattern: ^\s?createProject
[Worker] Collecting tests due to file changes: build-test/package.json
[Worker] CLI Building entry: ./src/helpers.ts
[Worker] CLI Using tsconfig: tsconfig.json
[Worker] CLI tsup v8.1.0
[Worker] CLI Target: esnext
[Worker] ESM Build start
[Worker] ESM build-test/helpers.js 402.00 B
[Worker] ESM ⚡️ Build success in 39ms
[Worker] DTS Build start
[Worker] DTS ⚡️ Build success in 571ms
[Worker] DTS build-test/helpers.d.ts 461.00 B
[INFO 14:52:40] [API] Vitest process 42207 closed successfully
[INFO 14:52:40] [API] Running Vitest v1.6.0 (tsd-cli/vitest.config.ts) with Node.js: /Users/chlbri/.nvm/versions/node/v20.9.0/lib/node_modules/npm/pnpm/node
[INFO 14:52:40] [API] Vitest v1.6.0 (tsd-cli/vitest.config.ts) process 42301 created
[INFO 14:52:40] [API] Collecting tests: src/createProject.test.ts
MY-COMMENT - ///And it stops here
Version
v0.12.0
Validations
- [X] Check that you are using the latest version of the extension
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
I don't think this is related to the extension. Does it still happen?