Skip to content

Commit

Permalink
fix debug auto config for win32 jest command (jest-community#998)
Browse files Browse the repository at this point in the history
* fix debug auto config for win32 jest command

* fix test
  • Loading branch information
connectdotz authored Jan 27, 2023
1 parent 23bbb21 commit 9f367f9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
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

0 comments on commit 9f367f9

Please sign in to comment.