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

fix debug auto config for win32 jest command #998

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/DebugConfigurationProvider.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';

import {
toFilePath,
Expand All @@ -8,6 +9,7 @@ import {
escapeRegExp,
parseCmdLine,
} from './helpers';
import { platform } from 'os';

export const DEBUG_CONFIG_PLATFORMS = ['windows', 'linux', 'osx'];
const testNamePatternRegex = /\$\{jest.testNamePattern\}/g;
Expand Down Expand Up @@ -204,11 +206,12 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
};
} else {
// convert the cmd to absolute path
const program = path.isAbsolute(cmd)
let program = path.isAbsolute(cmd)
? cmd
: absoluteRootPath
? path.resolve(absoluteRootPath, cmd)
: ['${workspaceFolder}', cmd].join(path.sep);
program = this.adjustProgram(program);
const args = [...cmdArgs, ...config.args];
finalConfig = { ...finalConfig, cwd, program, args };
}
Expand All @@ -218,4 +221,19 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv

return finalConfig;
}

// adopt program/command for debug purpose
private adjustProgram(program: string): string {
if (platform() === 'win32' && program.endsWith('\\node_modules\\.bin\\jest.cmd')) {
const newProgram = program.replace(
'\\node_modules\\.bin\\jest.cmd',
'\\node_modules\\jest\\bin\\jest.js'
);
if (fs.existsSync(newProgram)) {
return newProgram;
}
throw new Error(`failed to find jest binary: ${newProgram}`);
}
return program;
}
}
31 changes: 30 additions & 1 deletion tests/DebugConfigurationProvider.test.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
parseCmdLine,
} from '../src/helpers';
import * as os from 'os';
import * as fs from 'fs';
import { makeWorkspaceFolder } from './test-helper';

describe('DebugConfigurationProvider', () => {
Expand Down Expand Up @@ -193,7 +194,7 @@ describe('DebugConfigurationProvider', () => {
${12} | ${false} | ${'"/dir with space/jest" --arg1=1 --arg2 2 "some string"'} | ${{ cmd: '/dir with space/jest', args: ['--arg1=1', '--arg2', '2', '"some string"'], program: '/dir with space/jest' }}
${13} | ${false} | ${"'/dir with space/jest' --arg1=1 --arg2 2 'some string'"} | ${{ cmd: '/dir with space/jest', args: ['--arg1=1', '--arg2', '2', "'some string'"], program: '/dir with space/jest' }}
${14} | ${false} | ${'jest --arg1 "escaped \\"this\\" string" --arg2 2'} | ${{ cmd: 'jest', args: ['--arg1', '"escaped \\"this\\" string"', '--arg2', '2'], program: '${workspaceFolder}/jest' }}
${15} | ${true} | ${'.\\node_modules\\.bin\\jest'} | ${{ cmd: 'node_modules\\.bin\\jest', args: [], program: '${workspaceFolder}\\node_modules\\.bin\\jest' }}
${15} | ${true} | ${'.\\node_modules\\.bin\\jest.cmd'} | ${{ cmd: 'node_modules\\jest\\bin\\jest.js', args: [], program: '${workspaceFolder}\\node_modules\\jest\\bin\\jest.js' }}
${16} | ${true} | ${'..\\jest --config="..\\jest-config.json"'} | ${{ cmd: '..\\jest', args: ['--config=', '"..\\jest-config.json"'], program: '${workspaceFolder}\\..\\jest' }}
${17} | ${true} | ${'jest --config "..\\dir with space\\jest-config.json"'} | ${{ cmd: 'jest', args: ['--config', '"..\\dir with space\\jest-config.json"'], program: '${workspaceFolder}\\jest' }}
${18} | ${true} | ${'\\absolute\\jest --runInBand'} | ${{ cmd: '\\absolute\\jest', args: ['--runInBand'], program: '\\absolute\\jest' }}
Expand All @@ -204,6 +205,8 @@ describe('DebugConfigurationProvider', () => {
if (!canRunTest(isWin32)) {
return;
}

(fs.existsSync as jest.Mocked<any>) = jest.fn().mockReturnValue(true);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { args, program, windows, ...restConfig } = config;
const sut = new DebugConfigurationProvider();
Expand All @@ -229,6 +232,32 @@ describe('DebugConfigurationProvider', () => {
const sut = new DebugConfigurationProvider();
expect(() => sut.withCommandLine(workspace, cmdLine)).toThrow('invalid cmdLine');
});
describe('on win32, should throw error if the raw jest binary can not be found', () => {
let platformSpy;
beforeAll(() => {
platformSpy = jest.spyOn(os, 'platform').mockImplementation(() => 'win32');
});
afterAll(() => {
platformSpy.mockRestore();
});
it.each`
exists
${true}
${false}
`('file exists = $exists', ({ exists }) => {
(fs.existsSync as jest.Mocked<any>) = jest.fn().mockReturnValue(exists);
const sut = new DebugConfigurationProvider();
if (!exists) {
expect(() =>
sut.withCommandLine(workspace, 'whatever\\node_modules\\.bin\\jest.cmd')
).toThrow();
} else {
expect(() =>
sut.withCommandLine(workspace, 'whatever\\node_modules\\.bin\\jest.cmd')
).not.toThrow();
}
});
});
it.each`
cmd | cArgs | appendExtraArg
${'yarn'} | ${['test']} | ${false}
Expand Down