-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,599 additions
and
1,382 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export function findFullTestName(selectedLine: number, children: any[]): string | undefined { | ||
if (!children) { | ||
return; | ||
} | ||
for (const element of children) { | ||
if (element.type === 'describe' && selectedLine === element.start.line) { | ||
return element.name; | ||
} | ||
if (element.type !== 'describe' && selectedLine >= element.start.line && selectedLine <= element.end.line) { | ||
return element.name; | ||
} | ||
} | ||
for (const element of children) { | ||
const result = findFullTestName(selectedLine, element.children); | ||
if (result) { | ||
return element.name + ' ' + result; | ||
} | ||
} | ||
} | ||
|
||
export type CodeLensOption = 'run' | 'debug' | 'watch' | 'coverage'; | ||
|
||
function isCodeLensOption(option: string): option is CodeLensOption { | ||
return ['run', 'debug', 'watch', 'coverage'].includes(option); | ||
} | ||
|
||
export function validateCodeLensOptions(maybeCodeLensOptions: string[]): CodeLensOption[] { | ||
return [...new Set(maybeCodeLensOptions)].filter((value) => isCodeLensOption(value)) as CodeLensOption[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { IJestRunnerCommandBuilderConfig } from './jestRunnerConfig'; | ||
import { | ||
escapeRegExp, | ||
escapeRegExpForPath, | ||
escapeQuotesInTestName, | ||
normalizePath, | ||
quote, | ||
resolveTestNameStringInterpolation, | ||
updateTestNameIfUsingProperties, | ||
} from './util'; | ||
|
||
export class CommandBuilder { | ||
constructor(private readonly config: IJestRunnerCommandBuilderConfig) {} | ||
|
||
buildJestCommand(filePath: string, testName?: string, options?: string[]): string { | ||
const args = this.buildJestArgs(filePath, testName, options); | ||
return `${this.config.jestCommand} ${args.join(' ')}`; | ||
} | ||
|
||
buildJestArgs(filePath: string, testName: string | undefined, options: string[] = []): string[] { | ||
const args: string[] = []; | ||
|
||
args.push(quote(escapeRegExpForPath(normalizePath(filePath)))); | ||
|
||
const jestConfigPath = this.config.getJestConfigPath(filePath); | ||
if (jestConfigPath) { | ||
args.push('-c'); | ||
args.push(quote(normalizePath(jestConfigPath))); | ||
} | ||
|
||
if (testName) { | ||
args.push('-t'); | ||
testName = resolveTestName(testName); | ||
args.push(quote(testName)); | ||
} | ||
|
||
const setOptions = new Set(options); | ||
|
||
if (this.config.runOptions) { | ||
this.config.runOptions.forEach((option) => setOptions.add(option)); | ||
} | ||
|
||
args.push(...setOptions); | ||
|
||
return args; | ||
} | ||
} | ||
|
||
function resolveTestName(testName: string): string { | ||
testName = updateTestNameIfUsingProperties(testName); | ||
testName = resolveTestNameStringInterpolation(testName); | ||
testName = escapeRegExp(testName); | ||
testName = escapeQuotesInTestName(testName); | ||
testName = testName.replace(/\n/g, '\\n'); | ||
testName = testName.replace(/\r/g, '\\r'); | ||
return testName; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { tmpdir } from 'os'; | ||
import { mkdtempSync } from 'fs'; | ||
import { rimrafSync } from 'rimraf'; | ||
import * as path from 'path'; | ||
import { runJestCommand } from './util/runJestCommand'; | ||
import { Shell } from './util/shellHandler'; | ||
import { isWindows } from '../util'; | ||
|
||
const ALL_SHELLS: Array<Shell> = isWindows() ? ['cmd', 'powershell'] : ['bash']; | ||
|
||
describe('CommandBuilder', () => { | ||
let tempDir: string; | ||
|
||
beforeEach(() => { | ||
tempDir = mkdtempSync(path.resolve(tmpdir(), 'commandBuilder')); | ||
}); | ||
|
||
afterEach(() => { | ||
rimrafSync(tempDir); | ||
}); | ||
|
||
describe.each(ALL_SHELLS)('mustMatchSelf (%s)', (shell: Shell) => { | ||
it('single quote', () => mustMatchSelf(shell, `test with ' single quote`)); | ||
it('double quote', () => mustMatchSelf(shell, 'test with " double quote')); | ||
it('parenthesis', () => mustMatchSelf(shell, 'test with () parenthesis')); | ||
it('lf', () => mustMatchSelf(shell, `test with \nlf`)); | ||
it('lf#2', () => mustMatchSelf(shell, 'test with \nmanual lf')); | ||
it('crlf', () => mustMatchSelf(shell, 'test with \r\nmanual crlf')); | ||
it('backticks', () => mustMatchSelf(shell, 'test with `backticks`')); | ||
it('regex', () => mustMatchSelf(shell, 'test with regex .*$^|[]')); | ||
it('prototype .name property', () => mustMatchSelf(shell, 'TestClass.prototype.myFunction.name', 'myFunction')); | ||
}); | ||
|
||
async function mustMatchSelf(shell: Shell, testName: string, expectedTestName?: string) { | ||
if (shouldSkipMustMatchSelf(shell, testName)) { | ||
return; | ||
} | ||
const jestJson = await runJestCommand(shell, tempDir, testName); | ||
const expectedPassedTests = [expectedTestName ?? testName]; | ||
return expect(jestJson).toEqual( | ||
expect.objectContaining({ | ||
passedTests: expectedPassedTests, | ||
}) | ||
); | ||
} | ||
|
||
// FIXME: these are broken | ||
function shouldSkipMustMatchSelf(shell: Shell, testName: string): boolean { | ||
if (isWindows()) { | ||
if (shell === 'powershell' && testName === 'test with `backticks`') { | ||
return true; | ||
} | ||
if (shell === 'powershell' && testName === 'test with " double quote') { | ||
return true; | ||
} | ||
} else { | ||
return shell === 'pwsh' && testName === `test with ' single quote`; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
describe.each(ALL_SHELLS)('mustMatchAll (%s)', (shell: Shell) => { | ||
it('all match', () => mustMatchAll(shell, 'test with ')); | ||
it('using %', () => mustMatchAll(shell, 'test with %p')); | ||
it('using $', () => mustMatchAll(shell, 'test with $var')); | ||
}); | ||
|
||
async function mustMatchAll(shell: Shell, testName: string) { | ||
const jestJson = await runJestCommand(shell, tempDir, testName); | ||
expect(jestJson).toEqual( | ||
expect.objectContaining({ | ||
numPassedTests: 15, | ||
}) | ||
); | ||
} | ||
}); |
Oops, something went wrong.