Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
connectdotz committed Sep 11, 2024
1 parent 9fd1ab8 commit 1f3e963
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/DebugConfigurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const DEBUG_CONFIG_PLATFORMS = ['windows', 'linux', 'osx'];
const testNamePatternRegex = /\$\{jest.testNamePattern\}/g;
const testFileRegex = /\$\{jest.testFile\}/g;
const testFilePatternRegex = /\$\{jest.testFilePattern\}/g;
const replaceTestPathPatternRegex = /--(testPathPattern(s?)|runTestsByPath)/g;

export type DebugConfigOptions = Partial<
Pick<PluginResourceSettings, 'jestCommandLine' | 'rootPath' | 'nodeEnv'>
Expand Down Expand Up @@ -130,8 +131,8 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
if (debugInfo.useTestPathPattern) {
// if the debugInfo indicated this is a testPathPattern (such as running all tests within a folder)
// , we need to replace the --runTestsByPath argument with the correct --testPathPattern(s) argument
if (arg.includes('--runTestsByPath')) {
return arg.replace('--runTestsByPath', this.getTestPathPatternOption());
if (replaceTestPathPatternRegex.test(arg)) {
return arg.replace(replaceTestPathPatternRegex, this.getTestPathPatternOption());
}
if (testFileRegex.test(arg)) {
return arg.replace(testFileRegex, escapeRegExp(debugInfo.testPath));
Expand Down
71 changes: 67 additions & 4 deletions tests/DebugConfigurationProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('DebugConfigurationProvider', () => {
beforeEach(() => {
(toFilePath as unknown as jest.Mock<{}>).mockImplementation((s) => s);
(escapeRegExp as unknown as jest.Mock<{}>).mockImplementation((s) => s);
(escapeQuotes as unknown as jest.Mock<{}>).mockImplementation((s) => s);
});

it('should by default return a DebugConfiguration for Jest', () => {
Expand Down Expand Up @@ -77,6 +78,7 @@ describe('DebugConfigurationProvider', () => {

it.each`
debugConfigArgs | expectedArgs
${undefined} | ${['--testNamePattern', testName, '--runTestsByPath', testPath]}
${[]} | ${['--testNamePattern', testName, '--runTestsByPath', testPath]}
${['--runInBand']} | ${['--runInBand', '--testNamePattern', testName, '--runTestsByPath', testPath]}
`(
Expand Down Expand Up @@ -133,10 +135,6 @@ describe('DebugConfigurationProvider', () => {
expect(configuration.args).toEqual(expected);
});
it('will translate multiple variables in a single arg', () => {
(toFilePath as unknown as jest.Mock<{}>).mockImplementation((s) => s);
(escapeRegExp as unknown as jest.Mock<{}>).mockImplementation((s) => s);
(escapeQuotes as unknown as jest.Mock<{}>).mockImplementation((s) => s);

let configuration: any = {
name: 'vscode-jest-tests.v2',
args: ['--testNamePattern "${jest.testNamePattern}" --runTestsByPath "${jest.testFile}"'],
Expand Down Expand Up @@ -421,4 +419,69 @@ describe('DebugConfigurationProvider', () => {
});
});
});
describe('debug with useTestPathPattern = true', () => {
let debugInfo;
const testPath = '/a/__tests__';
const testName = 'a test';
const createDebugConfig = (type: 'v1' | 'v2' | 'v2-custom') => {
switch (type) {
case 'v1':
return { name: 'vscode-jest-tests', args: [] };
case 'v2':
return {
name: 'vscode-jest-tests.v2',
args: [
'--runInBand',
'--watchAll=false',
'--testNamePattern',
'${jest.testNamePattern}',
'--runTestsByPath',
'${jest.testFile}',
],
};
case 'v2-custom':
return {
name: 'vscode-jest-tests.v2',
args: [
'--runInBand',
'--watchAll=false',
'--testNamePattern',
'${jest.testNamePattern}',
'--testPathPattern',
'${jest.testFile}',
],
};
}
};
beforeEach(() => {
debugInfo = { testPath, testName, useTestPathPattern: true };
});

describe('should update debug config args with testPathPattern(s) accordingly', () => {
it.each`
configType | useJest30
${'v1'} | ${true}
${'v1'} | ${false}
${'v1'} | ${undefined}
${'v2'} | ${true}
${'v2'} | ${false}
${'v2'} | ${undefined}
${'v2-custom'} | ${true}
${'v2-custom'} | ${false}
${'v2-custom'} | ${undefined}
`('for $configType, useJest30=$useJest30', ({ configType, useJest30 }) => {
let debugConfig: any = createDebugConfig(configType);

const sut = new DebugConfigurationProvider();
const ws = makeWorkspaceFolder('whatever');
sut.prepareTestRun(debugInfo, ws, useJest30);

debugConfig = sut.resolveDebugConfiguration(undefined, debugConfig);

const testPathPatternOption = useJest30 ? '--testPathPatterns' : '--testPathPattern';
expect(debugConfig.args).toEqual(expect.arrayContaining([testPathPatternOption, testPath]));
expect(debugConfig.args).not.toContain('--runTestsByPath');
});
});
});
});

0 comments on commit 1f3e963

Please sign in to comment.