Skip to content

Commit

Permalink
Add code lens option for collectCoverageFrom (#372)
Browse files Browse the repository at this point in the history
Co-authored-by: Bashizade, Armin <[email protected]>
  • Loading branch information
arminbashizade and Bashizade, Armin authored Oct 11, 2024
1 parent 4dea33a commit 1bc2dc8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@
"run",
"debug"
],
"description": "Enable desired codeLens, possible value : 'run', 'debug', 'watch', 'coverage'. Defaults to ['run', 'debug'] ",
"description": "Enable desired codeLens, possible value : 'run', 'debug', 'watch', 'coverage', 'current-test-coverage'. Defaults to ['run', 'debug'] ",
"items": {
"type": "string",
"description": "Either 'run', 'debug', 'watch', 'coverage'"
"description": "Either 'run', 'debug', 'watch', 'coverage', 'current-test-coverage'"
},
"scope": "window"
},
Expand Down
2 changes: 2 additions & 0 deletions src/JestRunnerCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ function getCodeLensForOption(range: Range, codeLensOption: CodeLensOption, full
debug: 'Debug',
watch: 'Run --watch',
coverage: 'Run --coverage',
'current-test-coverage': 'Run --collectCoverageFrom (target file/dir)'
};
const commandMap: Record<CodeLensOption, string> = {
run: 'extension.runJest',
debug: 'extension.debugJest',
watch: 'extension.watchJest',
coverage: 'extension.runJestCoverage',
'current-test-coverage': 'extension.runJestCurrentTestCoverage'
};
return new CodeLens(range, {
arguments: [fullTestName],
Expand Down
8 changes: 8 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export function activate(context: vscode.ExtensionContext): void {
}
);

const runJestCurrentTestCoverage = vscode.commands.registerCommand(
'extension.runJestCurrentTestCoverage',
async (argument: Record<string, unknown> | string) => {
return jestRunner.runCurrentTest(argument, ['--coverage'], true);
}
);

const runJestPath = vscode.commands.registerCommand('extension.runJestPath', async (argument: vscode.Uri) =>
jestRunner.runTestsOnPath(argument.path)
);
Expand Down Expand Up @@ -71,6 +78,7 @@ export function activate(context: vscode.ExtensionContext): void {
}
context.subscriptions.push(runJest);
context.subscriptions.push(runJestCoverage);
context.subscriptions.push(runJestCurrentTestCoverage);
context.subscriptions.push(runJestAndUpdateSnapshots);
context.subscriptions.push(runJestFile);
context.subscriptions.push(runJestPath);
Expand Down
27 changes: 25 additions & 2 deletions src/jestRunner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as vscode from 'vscode';
import * as fs from 'fs';

import { JestRunnerConfig } from './jestRunnerConfig';
import { parse } from './parser';
Expand All @@ -7,6 +8,8 @@ import {
escapeRegExpForPath,
escapeSingleQuotes,
findFullTestName,
getFileName,
getDirName,
normalizePath,
pushMany,
quote,
Expand Down Expand Up @@ -50,7 +53,11 @@ export class JestRunner {
await this.runExternalNativeTerminalCommand(this.commands);
}

public async runCurrentTest(argument?: Record<string, unknown> | string, options?: string[]): Promise<void> {
public async runCurrentTest(
argument?: Record<string, unknown> | string,
options?: string[],
collectCoverageFromCurrentFile?: boolean,
): Promise<void> {
const currentTestName = typeof argument === 'string' ? argument : undefined;
const editor = vscode.window.activeTextEditor;
if (!editor) {
Expand All @@ -60,9 +67,25 @@ export class JestRunner {
await editor.document.save();

const filePath = editor.document.fileName;

const finalOptions = options;
if (collectCoverageFromCurrentFile) {
const targetFileDir = getDirName(filePath);
const targetFileName = getFileName(filePath).replace(/\.(test|spec)\./, '.');

// if a file does not exist with the same name as the test file but without the test/spec part
// use test file's directory for coverage target
const coverageTarget = fs.existsSync(`${targetFileDir}/${targetFileName}`)
? `**/${targetFileName}`
: `**/${getFileName(targetFileDir)}/**`;

finalOptions.push('--collectCoverageFrom');
finalOptions.push(quote(coverageTarget));
}

const testName = currentTestName || this.findCurrentTestName(editor);
const resolvedTestName = updateTestNameIfUsingProperties(testName);
const command = this.buildJestCommand(filePath, resolvedTestName, options);
const command = this.buildJestCommand(filePath, resolvedTestName, finalOptions);

this.previousCommand = command;

Expand Down
13 changes: 11 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import * as path from 'path';
import { execSync } from 'child_process';

export function getDirName(filePath: string): string {
return path.dirname(filePath);
}

export function getFileName(filePath: string): string {
return path.basename(filePath);
}

export function isWindows(): boolean {
return process.platform.includes('win32');
}
Expand Down Expand Up @@ -74,10 +83,10 @@ export function pushMany<T>(arr: T[], items: T[]): number {
return Array.prototype.push.apply(arr, items);
}

export type CodeLensOption = 'run' | 'debug' | 'watch' | 'coverage';
export type CodeLensOption = 'run' | 'debug' | 'watch' | 'coverage' | 'current-test-coverage';

function isCodeLensOption(option: string): option is CodeLensOption {
return ['run', 'debug', 'watch', 'coverage'].includes(option);
return ['run', 'debug', 'watch', 'coverage', 'current-test-coverage'].includes(option);
}

export function validateCodeLensOptions(maybeCodeLensOptions: string[]): CodeLensOption[] {
Expand Down

0 comments on commit 1bc2dc8

Please sign in to comment.