Skip to content

Commit 9474880

Browse files
authored
fix active editor files sometimes got attached to the wrong workspace folder (#1108)
* fix active editor files sometimes got attached to the wrong workspace folder * remove outdated comment * update comment
1 parent ce948cf commit 9474880

File tree

6 files changed

+135
-102
lines changed

6 files changed

+135
-102
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ You can see the full [features](#features) and learn more details in the [How-To
2626
Happy testing!
2727

2828
## Releases
29-
- **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)
29+
- **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)
3030
- **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)
3131
- **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)
3232

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-jest",
33
"displayName": "Jest",
44
"description": "Use Facebook's Jest With Pleasure.",
5-
"version": "6.1.1",
5+
"version": "6.1.2",
66
"publisher": "Orta",
77
"engines": {
88
"vscode": "^1.68.1"

Diff for: src/JestExt/core.ts

+28-31
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,7 @@ export class JestExt {
280280
this.testProvider = new JestTestProvider(this.getExtExplorerContext());
281281
this.resetStatusBar();
282282

283-
vscode.window.visibleTextEditors.forEach((editor) => {
284-
this.triggerUpdateActiveEditor(editor);
285-
});
283+
this.updateVisibleTextEditors();
286284
return;
287285
}
288286

@@ -310,9 +308,7 @@ export class JestExt {
310308
await this.updateTestFileList();
311309

312310
// update visible editors that belong to this folder
313-
vscode.window.visibleTextEditors.forEach((editor) => {
314-
this.triggerUpdateActiveEditor(editor);
315-
});
311+
this.updateVisibleTextEditors();
316312
} catch (e) {
317313
this.outputActionMessages(
318314
`Failed to start jest session: ${e}`,
@@ -372,13 +368,8 @@ export class JestExt {
372368
updateCurrentDiagnostics(sortedResults.fail, this.failDiagnostics, editor);
373369
}
374370

375-
public triggerUpdateActiveEditor(editor: vscode.TextEditor): void {
376-
// there is use case that the active editor is not in the workspace but is in jest test file list
377-
if (!this.isInWorkspaceFolder(editor) && !this.isTestFileEditor(editor)) {
378-
return;
379-
}
380-
this.coverageOverlay.updateVisibleEditors();
381-
371+
private triggerUpdateActiveEditor(editor: vscode.TextEditor): void {
372+
this.coverageOverlay.update(editor);
382373
this.updateTestFileEditor(editor);
383374
}
384375

@@ -420,6 +411,25 @@ export class JestExt {
420411
await this.startSession(true);
421412
}
422413

414+
/**
415+
* Updates the valid text editors based on the specified document.
416+
* If a document is provided, it triggers an update for the active editor matches the document.
417+
* If no document is provided, it triggers an update for all editors that are in the workspace folder
418+
*
419+
* @param document The document to match against the active editor. Optional.
420+
*/
421+
private updateVisibleTextEditors(document?: vscode.TextDocument): void {
422+
vscode.window.visibleTextEditors.forEach((editor) => {
423+
if (document) {
424+
if (editor.document === document) {
425+
this.triggerUpdateActiveEditor(editor);
426+
}
427+
} else if (this.isInWorkspaceFolder(editor)) {
428+
this.triggerUpdateActiveEditor(editor);
429+
}
430+
});
431+
}
432+
423433
private isInWorkspaceFolder(editor: vscode.TextEditor): boolean {
424434
return isInFolder(editor.document.uri, this.extContext.workspace);
425435
}
@@ -434,12 +444,7 @@ export class JestExt {
434444
return false;
435445
}
436446

437-
if (this.testResultProvider.isTestFile(editor.document.fileName) === 'no') {
438-
return false;
439-
}
440-
441-
// 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
442-
return true;
447+
return this.testResultProvider.isTestFile(editor.document.fileName);
443448
}
444449

445450
/**
@@ -613,7 +618,7 @@ export class JestExt {
613618
} else {
614619
const name = editor.document.fileName;
615620
let pInfo;
616-
if (this.testResultProvider.isTestFile(name) !== 'yes') {
621+
if (!this.testResultProvider.isTestFile(name)) {
617622
// run related tests from source file
618623
pInfo = this.processSession.scheduleProcess({
619624
type: 'by-file',
@@ -670,14 +675,14 @@ export class JestExt {
670675
}
671676
const isTestFile = this.testResultProvider.isTestFile(document.fileName);
672677

673-
if (isTestFile === 'no' && this.extContext.settings.runMode.config.testFileOnly) {
678+
if (!isTestFile && this.extContext.settings.runMode.config.testFileOnly) {
674679
// not a test file and configured not to re-run test for non-test files => mark the workspace dirty
675680
this.dirtyFiles.add(document.fileName);
676681
} else {
677682
this.processSession.scheduleProcess({
678683
type: 'by-file',
679684
testFileName: document.fileName,
680-
notTestFile: isTestFile !== 'yes',
685+
notTestFile: !isTestFile,
681686
});
682687
}
683688
}
@@ -687,15 +692,7 @@ export class JestExt {
687692
* @param document refresh UI for the specific document. if undefined, refresh all active editors in the workspace.
688693
*/
689694
private refreshDocumentChange(document?: vscode.TextDocument): void {
690-
for (const editor of vscode.window.visibleTextEditors) {
691-
if (
692-
(document && editor.document === document) ||
693-
this.isInWorkspaceFolder(editor) ||
694-
this.isTestFileEditor(editor)
695-
) {
696-
this.triggerUpdateActiveEditor(editor);
697-
}
698-
}
695+
this.updateVisibleTextEditors(document);
699696

700697
this.updateStatusBar({
701698
stats: this.toSBStats(this.testResultProvider.getTestSuiteStats()),

Diff for: src/TestResults/TestResultProvider.ts

+24-10
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,28 @@ export class TestResultProvider {
267267
if (this.testFiles && this.testFiles.length > 0) {
268268
return this.testFiles;
269269
}
270-
return Array.from(this.testSuites.keys());
270+
return Array.from(this.testSuites.keys()).filter((f) => this.isTestFile(f));
271271
}
272272

273-
isTestFile(fileName: string): 'yes' | 'no' | 'maybe' {
273+
isTestFile(fileName: string): boolean {
274274
if (this.testFiles?.includes(fileName) || this.testSuites.get(fileName)?.isTestFile) {
275-
return 'yes';
275+
return true;
276276
}
277-
if (!this.testFiles) {
278-
return 'maybe';
277+
278+
//if we already have testFiles, then we can be certain that the file is not a test file
279+
if (this.testFiles) {
280+
return false;
279281
}
280-
return 'no';
282+
283+
const _record = this.testSuites.get(fileName) ?? this.addTestSuiteRecord(fileName);
284+
if (_record.isTestFile === false) {
285+
return false;
286+
}
287+
288+
// check if the file is a test file by parsing the content
289+
const isTestFile = _record.testBlocks !== 'failed';
290+
_record.update({ isTestFile });
291+
return isTestFile;
281292
}
282293

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

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

@@ -367,7 +381,7 @@ export class TestResultProvider {
367381
*/
368382

369383
getSortedResults(filePath: string): SortedTestResults | undefined {
370-
if (this.isTestFile(filePath) === 'no') {
384+
if (!this.isTestFile(filePath)) {
371385
return;
372386
}
373387

0 commit comments

Comments
 (0)