Skip to content

Commit

Permalink
address #694
Browse files Browse the repository at this point in the history
  • Loading branch information
connectdotz committed May 15, 2021
1 parent d5a24b8 commit 2a3b524
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 27 deletions.
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-jest",
"displayName": "Jest",
"description": "Use Facebook's Jest With Pleasure.",
"version": "4.0.1",
"version": "4.0.2-rc.1",
"publisher": "Orta",
"engines": {
"vscode": "^1.45.0"
Expand Down Expand Up @@ -317,7 +317,9 @@
"request": "launch",
"program": "^\"\\${workspaceFolder}/node_modules/.bin/jest\"",
"args": [
"--runInBand"
"--runInBand",
"--runTestsByPath",
"${file}"
],
"cwd": "^\"\\${workspaceFolder}\"",
"console": "integratedTerminal",
Expand All @@ -339,7 +341,9 @@
"args": [
"test",
"--env=jsdom",
"--runInBand"
"--runInBand",
"--runTestsByPath",
"${file}"
],
"cwd": "^\"\\${workspaceFolder}\"",
"console": "integratedTerminal",
Expand All @@ -358,7 +362,9 @@
"program": "^\"\\${workspaceFolder}/scripts/test\"",
"args": [
"--env=jsdom",
"--runInBand"
"--runInBand",
"--runTestsByPath",
"${file}"
],
"cwd": "^\"\\${workspaceFolder}\"",
"console": "integratedTerminal",
Expand Down
12 changes: 9 additions & 3 deletions src/DebugConfigurationProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { getTestCommand, isCreateReactAppTestCommand } from './helpers';
import { escapeFilePath, getTestCommand, isCreateReactAppTestCommand } from './helpers';

export class DebugConfigurationProvider implements vscode.DebugConfigurationProvider {
private fileNameToRun = '';
Expand Down Expand Up @@ -33,11 +33,17 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
debugConfiguration.args = [];
}
if (this.fileNameToRun) {
debugConfiguration.args.push(this.fileNameToRun);
if (this.testToRun) {
debugConfiguration.args.push('--testNamePattern');
debugConfiguration.args.push(this.testToRun);
}
if (!debugConfiguration.args.includes('--runTestsByPath')) {
debugConfiguration.args.push('--runTestsByPath');
}
if (!debugConfiguration.args.includes('${file}')) {
debugConfiguration.args.push(escapeFilePath(this.fileNameToRun));
}

this.fileNameToRun = '';
this.testToRun = '';
}
Expand All @@ -60,7 +66,7 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
type: 'node',
name: 'vscode-jest-tests',
request: 'launch',
args: ['--runInBand'],
args: ['--runInBand', '--runTestsByPath', '${file}'],
cwd: '${workspaceFolder}',
console: 'integratedTerminal',
internalConsoleOptions: 'neverOpen',
Expand Down
17 changes: 12 additions & 5 deletions src/JestProcessManagement/JestProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { extensionId } from '../appGlobals';
import { Logging } from '../logging';
import { JestProcessRequest } from './types';
import { requestString } from './helper';
import { removeSurroundingQuote } from '../helpers';
import { escapeFilePath, removeSurroundingQuote } from '../helpers';

export const RunnerEvents: RunnerEvent[] = [
'processClose',
Expand Down Expand Up @@ -110,7 +110,9 @@ export class JestProcess {
}
break;
case 'by-file': {
options.testFileNamePattern = this.quoteFileName(this.request.testFileNamePattern);
options.testFileNamePattern = this.quoteFileName(
escapeFilePath(this.request.testFileNamePattern)
);
const args: string[] = ['--findRelatedTests'];
if (this.request.updateSnapshot) {
args.push('--updateSnapshot');
Expand All @@ -119,13 +121,18 @@ export class JestProcess {
break;
}

case 'by-file-test':
options.testFileNamePattern = this.quoteFileName(this.request.testFileNamePattern);
case 'by-file-test': {
options.testFileNamePattern = this.quoteFileName(
escapeFilePath(this.request.testFileNamePattern)
);
options.testNamePattern = this.request.testNamePattern;
const args: string[] = ['--runTestsByPath'];
if (this.request.updateSnapshot) {
options.args = { args: ['--updateSnapshot'] };
args.push('--updateSnapshot');
}
options.args = { args };
break;
}
case 'not-test':
delete options.reporters;
options.args = { args: this.request.args, replace: true };
Expand Down
8 changes: 8 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ export function testIdString(type: IdStringType, identifier: TestIdentifier): st
}
}

/**
* escape windows file path '\' to '\\', see https://jestjs.io/docs/cli#jest-regexfortestfiles
* @param filePath
*/
export function escapeFilePath(filePath: string): string {
return filePath.replace(/\\/g, '\\\\');
}

/**
* Generate path to icon used in decorations
* NOTE: Should not be called repeatedly for the performance reasons. Cache your results.
Expand Down
43 changes: 30 additions & 13 deletions tests/DebugConfigurationProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
jest.unmock('../src/DebugConfigurationProvider');

import { DebugConfigurationProvider } from '../src/DebugConfigurationProvider';
import { getTestCommand, isCreateReactAppTestCommand } from '../src/helpers';
import { getTestCommand, isCreateReactAppTestCommand, escapeFilePath } from '../src/helpers';

describe('DebugConfigurationProvider', () => {
it('should by default return a DebugConfiguration for Jest', () => {
Expand Down Expand Up @@ -37,19 +37,36 @@ describe('DebugConfigurationProvider', () => {
expect(config.args).toContain('--runInBand');
});

it('should append the specified tests', () => {
const fileName = 'fileName';
const testNamePattern = 'testNamePattern';
const expected = [fileName, '--testNamePattern', testNamePattern];
let configuration: any = { name: 'vscode-jest-tests' };
it.each`
debugConfigArgs | expectByPathArg | expectFileNameArg
${[]} | ${true} | ${true}
${['--runTestsByPath']} | ${false} | ${true}
${['--runTestsByPath', '${file}']} | ${false} | ${false}
`(
'should append the specified tests arguments',
({ debugConfigArgs, expectByPathArg, expectFileNameArg }) => {
((escapeFilePath as unknown) as jest.Mock<{}>).mockImplementation((s) => s);
const fileName = 'fileName';
const testNamePattern = 'testNamePattern';
const expected = [...debugConfigArgs, '--testNamePattern', testNamePattern];

const sut = new DebugConfigurationProvider();
sut.prepareTestRun(fileName, testNamePattern);
let configuration: any = { name: 'vscode-jest-tests', args: debugConfigArgs };

configuration = sut.resolveDebugConfiguration(undefined, configuration);
const sut = new DebugConfigurationProvider();
sut.prepareTestRun(fileName, testNamePattern);

expect(configuration).toBeDefined();
expect(configuration.env && configuration.env.CI).toBeTruthy();
expect(configuration.args).toEqual(expected);
});
configuration = sut.resolveDebugConfiguration(undefined, configuration);

expect(configuration).toBeDefined();
expect(configuration.env && configuration.env.CI).toBeTruthy();
if (expectByPathArg) {
expected.push('--runTestsByPath');
}
if (expectFileNameArg) {
expected.push(fileName);
expect(escapeFilePath).toBeCalled();
}
expect(configuration.args).toEqual(expected);
}
);
});
5 changes: 3 additions & 2 deletions tests/JestProcessManagement/JestProcess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ describe('JestProcess', () => {
${'all-tests'} | ${undefined} | ${[false, false]} | ${true} | ${undefined}
${'watch-tests'} | ${undefined} | ${[true, false]} | ${true} | ${undefined}
${'watch-all-tests'} | ${undefined} | ${[true, true]} | ${true} | ${undefined}
${'by-file'} | ${{ testFileNamePattern: '"abc def"' }} | ${[false, false]} | ${true} | ${undefined}
${'by-file-test'} | ${{ testFileNamePattern: '"abc def"', testNamePattern: 'test' }} | ${[false, false]} | ${true} | ${undefined}
${'by-file'} | ${{ testFileNamePattern: '"c:\\a\\b.ts"' }} | ${[false, false]} | ${true} | ${{ args: { args: ['--findRelatedTests'] }, testFileNamePattern: '"c:\\\\a\\\\b.ts"' }}
${'by-file-test'} | ${{ testFileNamePattern: '"/a/b.js"', testNamePattern: 'test' }} | ${[false, false]} | ${true} | ${{ args: { args: ['--runTestsByPath'] }, testFileNamePattern: '"/a/b.js"' }}
${'not-test'} | ${{ args: ['--listTests'] }} | ${[false, false]} | ${false} | ${{ args: { args: ['--listTests'], replace: true } }}
`(
'supports jest process request: $type',
Expand All @@ -156,6 +156,7 @@ describe('JestProcess', () => {
} else {
expect(options.reporters).toBeUndefined();
}

expect(options).toEqual(expect.objectContaining(extraRunnerOptions ?? extraProperty ?? {}));
expect(mockRunner.start).toBeCalledWith(...startArgs);
closeRunner();
Expand Down
13 changes: 13 additions & 0 deletions tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
testIdString,
escapeRegExp,
removeSurroundingQuote,
escapeFilePath,
} from '../src/helpers';

// Manually (forcefully) set the executable's file extension to test its addition independendly of the operating system.
Expand Down Expand Up @@ -251,3 +252,15 @@ describe('removeSurroundingQuote', () => {
expect(removeSurroundingQuote(str)).toEqual(expected);
});
});

describe('escapeFilePath', () => {
it.each`
path | expected
${'/a/b/c'} | ${'/a/b/c'}
${'C:/a/b/c.js'} | ${'C:/a/b/c.js'}
${'c:\\a\\b\\c.ts'} | ${'c:\\\\a\\\\b\\\\c.ts'}
${''} | ${''}
`('escape $path => $expected', ({ path, expected }) => {
expect(escapeFilePath(path)).toEqual(expected);
});
});

0 comments on commit 2a3b524

Please sign in to comment.