Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests executing child_process not working inside extension #412

Open
4 tasks done
chlbri opened this issue Jun 7, 2024 · 0 comments
Open
4 tasks done

Tests executing child_process not working inside extension #412

chlbri opened this issue Jun 7, 2024 · 0 comments

Comments

@chlbri
Copy link

chlbri commented Jun 7, 2024

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 });
});
Capture d’écran 2024-06-07 à 14 54 28

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant