Skip to content

Commit

Permalink
fix active editor files sometimes got attached to the wrong workspace…
Browse files Browse the repository at this point in the history
… folder (jest-community#1108)

* fix active editor files sometimes got attached to the wrong workspace folder

* remove outdated comment

* update comment
  • Loading branch information
connectdotz authored Feb 4, 2024
1 parent ce948cf commit 9474880
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 102 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ You can see the full [features](#features) and learn more details in the [How-To
Happy testing!

## Releases
- **Next** ([v6.1.1](https://github.com/jest-community/vscode-jest/releases/tag/v6.1.1-next)): [release note](release-notes/release-note-v6.md#v610-pre-release)
- **Next** ([v6.1.2](https://github.com/jest-community/vscode-jest/releases/tag/v6.1.2-next)): [release note](release-notes/release-note-v6.md#v610-pre-release)
- **Current** ([v5.2.3](https://github.com/jest-community/vscode-jest/releases/tag/v5.2.3)): [release note](release-notes/release-note-v5.x.md#v523)
- **Previous** ([v5.1.0](https://github.com/jest-community/vscode-jest/releases/tag/v5.1.0)): [release note](release-notes/release-note-v5.x.md#v510)

Expand Down
2 changes: 1 addition & 1 deletion 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": "6.1.1",
"version": "6.1.2",
"publisher": "Orta",
"engines": {
"vscode": "^1.68.1"
Expand Down
59 changes: 28 additions & 31 deletions src/JestExt/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,7 @@ export class JestExt {
this.testProvider = new JestTestProvider(this.getExtExplorerContext());
this.resetStatusBar();

vscode.window.visibleTextEditors.forEach((editor) => {
this.triggerUpdateActiveEditor(editor);
});
this.updateVisibleTextEditors();
return;
}

Expand Down Expand Up @@ -310,9 +308,7 @@ export class JestExt {
await this.updateTestFileList();

// update visible editors that belong to this folder
vscode.window.visibleTextEditors.forEach((editor) => {
this.triggerUpdateActiveEditor(editor);
});
this.updateVisibleTextEditors();
} catch (e) {
this.outputActionMessages(
`Failed to start jest session: ${e}`,
Expand Down Expand Up @@ -372,13 +368,8 @@ export class JestExt {
updateCurrentDiagnostics(sortedResults.fail, this.failDiagnostics, editor);
}

public triggerUpdateActiveEditor(editor: vscode.TextEditor): void {
// there is use case that the active editor is not in the workspace but is in jest test file list
if (!this.isInWorkspaceFolder(editor) && !this.isTestFileEditor(editor)) {
return;
}
this.coverageOverlay.updateVisibleEditors();

private triggerUpdateActiveEditor(editor: vscode.TextEditor): void {
this.coverageOverlay.update(editor);
this.updateTestFileEditor(editor);
}

Expand Down Expand Up @@ -420,6 +411,25 @@ export class JestExt {
await this.startSession(true);
}

/**
* Updates the valid text editors based on the specified document.
* If a document is provided, it triggers an update for the active editor matches the document.
* If no document is provided, it triggers an update for all editors that are in the workspace folder
*
* @param document The document to match against the active editor. Optional.
*/
private updateVisibleTextEditors(document?: vscode.TextDocument): void {
vscode.window.visibleTextEditors.forEach((editor) => {
if (document) {
if (editor.document === document) {
this.triggerUpdateActiveEditor(editor);
}
} else if (this.isInWorkspaceFolder(editor)) {
this.triggerUpdateActiveEditor(editor);
}
});
}

private isInWorkspaceFolder(editor: vscode.TextEditor): boolean {
return isInFolder(editor.document.uri, this.extContext.workspace);
}
Expand All @@ -434,12 +444,7 @@ export class JestExt {
return false;
}

if (this.testResultProvider.isTestFile(editor.document.fileName) === 'no') {
return false;
}

// if isTestFile returns unknown or true, treated it like a test file to give it best chance to display any test result if ever available
return true;
return this.testResultProvider.isTestFile(editor.document.fileName);
}

/**
Expand Down Expand Up @@ -613,7 +618,7 @@ export class JestExt {
} else {
const name = editor.document.fileName;
let pInfo;
if (this.testResultProvider.isTestFile(name) !== 'yes') {
if (!this.testResultProvider.isTestFile(name)) {
// run related tests from source file
pInfo = this.processSession.scheduleProcess({
type: 'by-file',
Expand Down Expand Up @@ -670,14 +675,14 @@ export class JestExt {
}
const isTestFile = this.testResultProvider.isTestFile(document.fileName);

if (isTestFile === 'no' && this.extContext.settings.runMode.config.testFileOnly) {
if (!isTestFile && this.extContext.settings.runMode.config.testFileOnly) {
// not a test file and configured not to re-run test for non-test files => mark the workspace dirty
this.dirtyFiles.add(document.fileName);
} else {
this.processSession.scheduleProcess({
type: 'by-file',
testFileName: document.fileName,
notTestFile: isTestFile !== 'yes',
notTestFile: !isTestFile,
});
}
}
Expand All @@ -687,15 +692,7 @@ export class JestExt {
* @param document refresh UI for the specific document. if undefined, refresh all active editors in the workspace.
*/
private refreshDocumentChange(document?: vscode.TextDocument): void {
for (const editor of vscode.window.visibleTextEditors) {
if (
(document && editor.document === document) ||
this.isInWorkspaceFolder(editor) ||
this.isTestFileEditor(editor)
) {
this.triggerUpdateActiveEditor(editor);
}
}
this.updateVisibleTextEditors(document);

this.updateStatusBar({
stats: this.toSBStats(this.testResultProvider.getTestSuiteStats()),
Expand Down
34 changes: 24 additions & 10 deletions src/TestResults/TestResultProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,28 @@ export class TestResultProvider {
if (this.testFiles && this.testFiles.length > 0) {
return this.testFiles;
}
return Array.from(this.testSuites.keys());
return Array.from(this.testSuites.keys()).filter((f) => this.isTestFile(f));
}

isTestFile(fileName: string): 'yes' | 'no' | 'maybe' {
isTestFile(fileName: string): boolean {
if (this.testFiles?.includes(fileName) || this.testSuites.get(fileName)?.isTestFile) {
return 'yes';
return true;
}
if (!this.testFiles) {
return 'maybe';

//if we already have testFiles, then we can be certain that the file is not a test file
if (this.testFiles) {
return false;
}
return 'no';

const _record = this.testSuites.get(fileName) ?? this.addTestSuiteRecord(fileName);
if (_record.isTestFile === false) {
return false;
}

// check if the file is a test file by parsing the content
const isTestFile = _record.testBlocks !== 'failed';
_record.update({ isTestFile });
return isTestFile;
}

public getTestSuiteResult(filePath: string): TestSuiteResult | undefined {
Expand All @@ -293,7 +304,8 @@ export class TestResultProvider {
**/
private updateMatchedResults(filePath: string, record: TestSuiteRecord): void {
let error: string | undefined;
// make sure we do not fire changeEvent since that will be proceeded with match or unmatch event anyway
let status = record.status;
// make sure we do not fire changeEvent since that will be proceeded with match or unmatched event anyway
const testBlocks = record.testBlocks;
if (testBlocks === 'failed') {
record.update({ status: 'KnownFail', message: 'test file parse error', results: [] });
Expand Down Expand Up @@ -321,14 +333,16 @@ export class TestResultProvider {
} catch (e) {
console.warn(`failed to match test results for ${filePath}:`, e);
error = `encountered internal match error: ${e}`;
status = 'KnownFail';
}
} else {
// there might be many reasons for this, for example the test is not yet run, so leave it as unknown
error = 'no assertion generated for file';
}

// no need to do groupByRange as the source block will not have blocks under the same location
record.update({
status: 'KnownFail',
status,
message: error,
results: itBlocks.map((t) => match.toMatchResult(t, 'no assertion found', 'match-failed')),
});
Expand All @@ -347,7 +361,7 @@ export class TestResultProvider {
* @returns valid test result list or an empty array if the source file is not a test or can not be parsed.
*/
getResults(filePath: string, record?: TestSuiteRecord): TestResult[] | undefined {
if (this.isTestFile(filePath) === 'no') {
if (!this.isTestFile(filePath)) {
return;
}

Expand All @@ -367,7 +381,7 @@ export class TestResultProvider {
*/

getSortedResults(filePath: string): SortedTestResults | undefined {
if (this.isTestFile(filePath) === 'no') {
if (!this.isTestFile(filePath)) {
return;
}

Expand Down
Loading

0 comments on commit 9474880

Please sign in to comment.