-
Notifications
You must be signed in to change notification settings - Fork 296
/
Copy pathcore.ts
875 lines (772 loc) · 29.4 KB
/
core.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
import * as vscode from 'vscode';
import { JestTotalResults } from 'jest-editor-support';
import { statusBar, StatusBar, StatusBarUpdate, SBTestStats } from '../StatusBar';
import {
TestResultProvider,
resultsWithLowerCaseWindowsDriveLetters,
SortedTestResults,
TestResultProviderOptions,
} from '../TestResults';
import { emptyTestStats, getValidJestCommand } from '../helpers';
import { CoverageMapProvider, CoverageCodeLensProvider } from '../Coverage';
import { updateDiagnostics, updateCurrentDiagnostics, resetDiagnostics } from '../diagnostics';
import { DebugConfigurationProvider } from '../DebugConfigurationProvider';
import { TestExplorerRunRequest, TestStats } from '../types';
import { CoverageOverlay } from '../Coverage/CoverageOverlay';
import { resultsWithoutAnsiEscapeSequence } from '../TestResults/TestResult';
import { CoverageMapData } from 'istanbul-lib-coverage';
import { Logging } from '../logging';
import { createProcessSession, ProcessSession } from './process-session';
import {
JestExtContext,
JestSessionEvents,
JestExtSessionContext,
JestRunEvent,
JestTestDataAvailableEvent,
} from './types';
import { DebugInfo } from '../types';
import { extensionName, SupportedLanguageIds } from '../appGlobals';
import { createJestExtContext, getExtensionResourceSettings, prefixWorkspace } from './helper';
import { PluginResourceSettings } from '../Settings';
import { WizardTaskId } from '../setup-wizard';
import { ItemCommand, JestExtExplorerContext } from '../test-provider/types';
import { JestTestProvider } from '../test-provider';
import { JestProcessInfo } from '../JestProcessManagement';
import { getExitErrorDef } from '../errors';
import { WorkspaceManager, isInFolder } from '../workspace-manager';
import { ansiEsc, JestOutputTerminal } from './output-terminal';
import { QuickFixActionType } from '../quick-fix';
import { executableTerminalLinkProvider } from '../terminal-link-provider';
import { outputManager } from '../output-manager';
interface JestCommandSettings {
rootPath: string;
jestCommandLine: string;
}
/** extract lines starts and end with [] */
export class JestExt {
coverageMapProvider: CoverageMapProvider;
coverageOverlay: CoverageOverlay;
testResultProvider: TestResultProvider;
debugConfigurationProvider: DebugConfigurationProvider;
coverageCodeLensProvider: CoverageCodeLensProvider;
// The ability to show fails in the problems section
private failDiagnostics: vscode.DiagnosticCollection;
private processSession: ProcessSession;
private vscodeContext: vscode.ExtensionContext;
private status: ReturnType<StatusBar['bind']>;
private logging: Logging;
private extContext: JestExtContext;
private dirtyFiles: Set<string> = new Set();
private testProvider?: JestTestProvider;
public events: JestSessionEvents;
private workspaceManager: WorkspaceManager;
private output: JestOutputTerminal;
private debugConfig?: vscode.DebugConfiguration;
constructor(
vscodeContext: vscode.ExtensionContext,
workspaceFolder: vscode.WorkspaceFolder,
debugConfigurationProvider: DebugConfigurationProvider,
coverageCodeLensProvider: CoverageCodeLensProvider
) {
this.vscodeContext = vscodeContext;
this.output = new JestOutputTerminal(workspaceFolder.name);
const pluginSettings = this.getExtensionResourceSettings(workspaceFolder);
this.updateOutputSetting(pluginSettings);
this.extContext = createJestExtContext(workspaceFolder, pluginSettings, this.output);
this.logging = this.extContext.loggingFactory.create('JestExt');
this.workspaceManager = new WorkspaceManager();
this.failDiagnostics = vscode.languages.createDiagnosticCollection(
`Jest (${workspaceFolder.name})`
);
this.coverageCodeLensProvider = coverageCodeLensProvider;
this.coverageMapProvider = new CoverageMapProvider();
this.coverageOverlay = new CoverageOverlay(
vscodeContext,
this.coverageMapProvider,
pluginSettings.runMode.config.coverage,
pluginSettings.coverageFormatter,
pluginSettings.coverageColors
);
this.events = {
onRunEvent: new vscode.EventEmitter<JestRunEvent>(),
onTestSessionStarted: new vscode.EventEmitter<JestExtSessionContext>(),
onTestSessionStopped: new vscode.EventEmitter<void>(),
onTestDataAvailable: new vscode.EventEmitter<JestTestDataAvailableEvent>(),
};
this.setupRunEvents(this.events);
this.testResultProvider = new TestResultProvider(
this.events,
this.testResultProviderOptions(pluginSettings)
);
this.debugConfigurationProvider = debugConfigurationProvider;
this.status = statusBar.bind(workspaceFolder);
// reset the jest diagnostics
resetDiagnostics(this.failDiagnostics);
this.processSession = this.createProcessSession();
this.setupStatusBar();
}
public get name(): string {
return this.extContext.workspace.name;
}
public get workspaceFolder(): vscode.WorkspaceFolder {
return this.extContext.workspace;
}
/**
* Gets the plugin resource settings for a workspace folder.
* @param workspaceFolder The workspace folder to get the plugin resource settings for.
* @returns The plugin resource settings for the workspace folder.
* @throws An error if Jest is disabled for the workspace folder.
*/
private getExtensionResourceSettings(
workspaceFolder: vscode.WorkspaceFolder
): PluginResourceSettings {
const pluginSettings = getExtensionResourceSettings(workspaceFolder);
if (pluginSettings.enable === false) {
throw new Error(`Jest is disabled for workspace ${workspaceFolder.name}`);
}
const { outputConfig, openTesting } = outputManager.outputConfigs();
this.output.write(
'Critical Settings:\r\n' +
`jest.runMode: ${JSON.stringify(pluginSettings.runMode.config, undefined, 4)}\r\n` +
`jest.outputConfig: ${JSON.stringify(outputConfig.value, undefined, 4)}\r\n` +
`testing.openTesting: ${JSON.stringify(openTesting.value, undefined, 4)}\r\n`,
'info'
);
return pluginSettings;
}
public showOutput(): void {
this.extContext.output.show();
}
private getExtExplorerContext(): JestExtExplorerContext {
return {
...this.extContext,
sessionEvents: this.events,
session: this.processSession,
testResultProvider: this.testResultProvider,
debugTests: this.debugTests,
};
}
public setupExtensionForFolder(args?: { taskId: WizardTaskId }): Thenable<void> {
const command = `${extensionName}.setup-extension`;
return vscode.commands.executeCommand(command, {
workspace: this.extContext.workspace,
taskId: args?.taskId,
verbose: this.extContext.settings.debugMode,
});
}
private longRunMessage(event: Extract<JestRunEvent, { type: 'long-run' }>): string {
const messages = [`Long Running Tests Warning: Jest process "${event.process.request.type}"`];
if (event.numTotalTestSuites != null) {
messages.push(`for ${event.numTotalTestSuites} suites`);
}
messages.push(`has exceeded ${event.threshold}ms.`);
return messages.join(' ');
}
private enableOutputOnRun(): void {
outputManager.showOutputOn('run', this.output, this.extContext.settings.runMode);
}
private setupRunEvents(events: JestSessionEvents): void {
events.onRunEvent.event((event: JestRunEvent) => {
// only process the test running event
if (event.process.request.type === 'not-test') {
return;
}
this.enableOutputOnRun();
switch (event.type) {
case 'start': {
this.updateStatusBar({ state: 'running' });
break;
}
case 'end': {
const state = event.error ? 'exec-error' : 'done';
this.updateStatusBar({ state });
// testError should be persistent per run-cycle. Not clear up this flag at end end of the cycle
// could cause the processes with multiple run cycles, such as watch mode, to failed to act properly.
if (event.process.userData?.testError) {
event.process.userData.testError = undefined;
}
break;
}
case 'exit':
if (event.error) {
this.updateStatusBar({ state: 'exec-error' });
if (!event.process.userData?.execError) {
this.outputActionMessages(
`Jest process exited unexpectedly: ${event.error}`,
['wizard', 'defer', 'disable-folder', 'help'],
true,
event.error
);
event.process.userData = { ...(event.process.userData ?? {}), execError: true };
}
} else {
this.updateStatusBar({ state: 'done' });
}
break;
case 'data': {
if (event.isError) {
this.updateStatusBar({ state: 'exec-error' });
}
break;
}
case 'test-error': {
if (!event.process.userData?.testError) {
outputManager.showOutputOn('test-error', this.output, this.extContext.settings.runMode);
event.process.userData = { ...(event.process.userData ?? {}), testError: true };
}
break;
}
case 'long-run': {
this.outputActionMessages(this.longRunMessage(event), ['help-long-run'], false);
break;
}
}
});
}
private outputActionMessages = (
errorMessage: string,
actionTypes: QuickFixActionType[],
isError: boolean,
extra?: unknown
): void => {
const msg = prefixWorkspace(this.extContext, errorMessage);
this.logging(isError ? 'error' : 'warn', `${msg}:`, extra);
this.output.write(errorMessage, isError ? 'error' : 'new-line');
const quickFixLink = executableTerminalLinkProvider.executableLink(
this.extContext.workspace.name,
`${extensionName}.with-workspace.show-quick-fix`,
actionTypes
);
this.output.write(`Open Quick Fix: \u2192 ${quickFixLink}`, 'info');
};
private createProcessSession(): ProcessSession {
const sessionContext = {
...this.extContext,
updateWithData: this.updateWithData.bind(this),
onRunEvent: this.events.onRunEvent,
};
return createProcessSession(sessionContext);
}
private toSBStats(stats: TestStats): SBTestStats {
return { ...stats, isDirty: this.dirtyFiles.size > 0 };
}
private setTestFiles(list: string[] | undefined): void {
this.testResultProvider.updateTestFileList(list);
this.updateStatusBar({ stats: this.toSBStats(this.testResultProvider.getTestSuiteStats()) });
}
/**
* starts a new session, notify all session-aware components and gather the metadata.
*/
public async startSession(newSession = false): Promise<void> {
try {
if (this.extContext.settings.runMode.config.deferred) {
// in deferred mode, we only start the test provider and nothing else
this.testProvider?.dispose();
this.testProvider = new JestTestProvider(this.getExtExplorerContext());
this.resetStatusBar();
this.updateVisibleTextEditors();
return;
}
this.dirtyFiles.clear();
this.resetStatusBar();
// new session is needed when the JestExtContext changes
if (newSession) {
await this.processSession.stop();
this.processSession = this.createProcessSession();
}
this.testProvider?.dispose();
this.testProvider = new JestTestProvider(this.getExtExplorerContext());
const readyState = await this.validateJestCommandLine();
if (readyState !== 'pass') {
return;
}
await this.processSession.start();
this.events.onTestSessionStarted.fire({ ...this.extContext, session: this.processSession });
await this.updateTestFileList();
// update visible editors that belong to this folder
this.updateVisibleTextEditors();
} catch (e) {
this.outputActionMessages(
`Failed to start jest session: ${e}`,
['wizard', 'defer', 'disable-folder', 'help'],
true,
e
);
this.updateStatusBar({ state: 'exec-error' });
}
}
public async stopSession(): Promise<void> {
try {
await this.processSession.stop();
this.testProvider?.dispose();
this.testProvider = undefined;
this.events.onTestSessionStopped.fire();
this.updateStatusBar({ state: 'stopped' });
} catch (e) {
this.outputActionMessages(
`Failed to stop jest session: ${e}`,
['defer', 'disable-folder', 'help'],
true,
e
);
this.updateStatusBar({ state: 'exec-error' });
}
}
private updateTestFileEditor(editor: vscode.TextEditor): void {
if (!this.isTestFileEditor(editor)) {
return;
}
const filePath = editor.document.fileName;
let sortedResults: SortedTestResults | undefined;
try {
sortedResults = this.testResultProvider.getSortedResults(filePath);
} catch (e) {
this.extContext.output.write(`${filePath}: failed to parse test results: ${e}`, 'error');
// assign an empty result so we can clear the outdated decorators/diagnostics etc
sortedResults = {
fail: [],
skip: [],
success: [],
unknown: [],
};
}
if (!sortedResults) {
return;
}
updateCurrentDiagnostics(sortedResults.fail, this.failDiagnostics, editor);
}
private triggerUpdateActiveEditor(editor: vscode.TextEditor): void {
this.coverageOverlay.update(editor);
this.updateTestFileEditor(editor);
}
private updateOutputSetting(settings: PluginResourceSettings): void {
this.output.revealOnError = !settings.runMode.config.deferred;
this.output.close();
}
private testResultProviderOptions(settings: PluginResourceSettings): TestResultProviderOptions {
return {
verbose: settings.debugMode ?? false,
parserOptions: settings.parserPluginOptions
? { plugins: settings.parserPluginOptions }
: undefined,
};
}
public async triggerUpdateSettings(newSettings?: PluginResourceSettings): Promise<void> {
const updatedSettings =
newSettings ?? this.getExtensionResourceSettings(this.extContext.workspace);
// output
this.updateOutputSetting(updatedSettings);
// TestResultProvider
this.testResultProvider.options = this.testResultProviderOptions(updatedSettings);
// coverage
this.coverageOverlay.dispose();
this.coverageOverlay = new CoverageOverlay(
this.vscodeContext,
this.coverageMapProvider,
updatedSettings.runMode.config.coverage,
updatedSettings.coverageFormatter,
updatedSettings.coverageColors
);
this.extContext = createJestExtContext(this.extContext.workspace, updatedSettings, this.output);
this.debugConfig = undefined;
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);
}
private isSupportedDocument(document?: vscode.TextDocument | undefined): boolean {
// if no testFiles list, then error on including more possible files as long as they are in the supported languages - this is backward compatible with v3 logic
return (document && SupportedLanguageIds.includes(document.languageId)) ?? false;
}
private isTestFileEditor(editor: vscode.TextEditor): boolean {
if (!this.isSupportedDocument(editor.document)) {
return false;
}
return this.testResultProvider.isTestFile(editor.document.fileName);
}
/**
* validate if there is a valid jest commandline. If the simple default command line is not valid,
* it will take on a more
*
* @returns
*/
public async validateJestCommandLine(): Promise<'pass' | 'fail' | 'restart'> {
if (this.extContext.settings.jestCommandLine) {
return Promise.resolve('pass');
}
const updateSettings = async (update: JestCommandSettings): Promise<'restart'> => {
this.extContext.settings.jestCommandLine = update.jestCommandLine;
this.extContext.settings.rootPath = update.rootPath;
await this.triggerUpdateSettings(this.extContext.settings);
return 'restart';
};
const outputSettings = (settings: JestCommandSettings) => {
this.extContext.output.write(
'found:\r\n' +
` ${ansiEsc('bold', 'rootPath')}: ${settings.rootPath}\r\n` +
` ${ansiEsc('bold', 'jestCommandLine')}: ${settings.jestCommandLine}\r\n`,
'new-line'
);
};
const t0 = Date.now();
this.extContext.output.write('auto config:', 'info');
const result = await getValidJestCommand(
this.extContext.workspace,
this.workspaceManager,
this.extContext.settings.rootPath
);
const perf = Date.now() - t0;
/* istanbul ignore next */
if (perf > 2000) {
this.extContext.output.write(
`auto config took ${perf} msec. Might be more efficient to update settings directly`,
'warn'
);
}
const foundPackage = result.uris && result.uris.length > 0;
if (foundPackage) {
this.extContext.output.write(
'examining the following package roots:\r\n' +
` ${result.uris?.map((uri) => uri.fsPath).join('\r\n ')}`,
'new-line'
);
}
let msg = 'Not able to auto detect a valid jest command';
let actionType: QuickFixActionType = 'setup-cmdline';
switch (result.validSettings.length) {
case 1:
outputSettings(result.validSettings[0]);
return updateSettings(result.validSettings[0]);
case 0: {
if (foundPackage) {
this.extContext.output.write(
'not able to find test script or jest/CRA binary in any of the package roots',
'warn'
);
} else {
this.extContext.output.write('no package.json file found', 'warn');
}
break;
}
default: {
msg = `${msg}: multiple candidates found`;
if (vscode.workspace.workspaceFolders?.length === 1) {
msg += ' Perhaps this is a multi-root monorepo?';
actionType = 'setup-monorepo';
}
break;
}
}
this.outputActionMessages(msg, [actionType, 'defer', 'disable-folder', 'help'], true);
this.updateStatusBar({ state: 'exec-error' });
return 'fail';
}
/* istanbul ignore next */
public activate(): void {
// do nothing
}
public deactivate(): void {
this.stopSession();
this.extContext.output.dispose();
this.testResultProvider.dispose();
this.testProvider?.dispose();
statusBar.removeWorkspaceFolder(this.extContext.workspace);
this.events.onRunEvent.dispose();
this.events.onTestSessionStarted.dispose();
this.events.onTestSessionStopped.dispose();
}
//** commands */
public debugTests = async (debugInfo: DebugInfo): Promise<void> => {
const getDebugConfig = (
folder?: vscode.WorkspaceFolder
): vscode.DebugConfiguration | undefined => {
const configs = vscode.workspace
.getConfiguration('launch', folder)
?.get<vscode.DebugConfiguration[]>('configurations');
if (!configs) {
return undefined;
}
const { sorted } = this.debugConfigurationProvider.getDebugConfigNames(
this.extContext.workspace
);
for (const name of sorted) {
const found = configs.find((c) => c.name === name);
if (found) {
return found;
}
}
};
this.debugConfigurationProvider.prepareTestRun(
debugInfo,
this.extContext.workspace,
this.extContext.settings.useJest30
);
let debugConfig =
getDebugConfig(this.extContext.workspace) ?? getDebugConfig() ?? this.debugConfig;
if (!debugConfig) {
this.logging(
'debug',
'No debug config named "vscode-jest-tests.v2" or "vscode-jest-tests" found in launch.json, will use a default config.'
);
debugConfig = this.debugConfigurationProvider.createDebugConfig(this.extContext.workspace, {
jestCommandLine: this.extContext.settings.jestCommandLine,
rootPath: this.extContext.settings.rootPath,
nodeEnv: this.extContext.settings.nodeEnv,
});
this.debugConfig = debugConfig;
this.extContext.output.write('auto config debug config:', 'info');
this.extContext.output.write(JSON.stringify(debugConfig, undefined, ' '), 'new-line');
}
await vscode.debug.startDebugging(this.extContext.workspace, debugConfig);
};
public async runAllTests(editor?: vscode.TextEditor): Promise<void> {
this.enableOutputOnRun();
await this.exitDeferMode();
if (!editor) {
if (this.processSession.scheduleProcess({ type: 'all-tests', nonBlocking: true })) {
this.dirtyFiles.clear();
return;
}
} else {
const name = editor.document.fileName;
let pInfo;
if (!this.testResultProvider.isTestFile(name)) {
// run related tests from source file
pInfo = this.processSession.scheduleProcess({
type: 'by-file',
testFileName: name,
notTestFile: true,
});
} else {
// note: use file-pattern instead of file-path to increase compatibility, such as for angular users.
// However, we should keep an eye on performance, as matching by pattern could be slower than by explicit path.
// If performance ever become an issue, we could consider optimization...
pInfo = this.processSession.scheduleProcess({
type: 'by-file-pattern',
testFileNamePattern: name,
});
}
if (pInfo) {
this.dirtyFiles.delete(name);
return;
}
}
this.logging('error', 'failed to schedule the run for', editor?.document.fileName);
}
//** window events handling */
onDidCloseTextDocument(document: vscode.TextDocument): void {
this.removeCachedTestResults(document);
}
removeCachedTestResults(document: vscode.TextDocument, invalidateResult = false): void {
if (!document || document.isUntitled) {
return;
}
const filePath = document.fileName;
if (invalidateResult) {
this.testResultProvider.invalidateTestResults(filePath);
} else {
this.testResultProvider.removeCachedResults(filePath);
}
}
onDidChangeActiveTextEditor(editor: vscode.TextEditor): void {
this.triggerUpdateActiveEditor(editor);
}
private handleOnSaveRun(document: vscode.TextDocument): void {
if (
!this.isSupportedDocument(document) ||
this.extContext.settings.runMode.config.deferred ||
this.extContext.settings.runMode.config.type !== 'on-save'
) {
return;
}
const isTestFile = this.testResultProvider.isTestFile(document.fileName);
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,
});
}
}
/**
* refresh UI for the given document editor or all active editors in the workspace
* @param document refresh UI for the specific document. if undefined, refresh all active editors in the workspace.
*/
private refreshDocumentChange(document?: vscode.TextDocument): void {
this.updateVisibleTextEditors(document);
this.updateStatusBar({
stats: this.toSBStats(this.testResultProvider.getTestSuiteStats()),
});
}
/**
* This event is fired with the document not dirty when:
* - before the onDidSaveTextDocument event
* - the document was changed by an external editor
*/
onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent): void {
if (event.document.isDirty) {
return;
}
if (event.document.uri.scheme === 'git') {
return;
}
// Ignore a clean file with a change:
if (event.contentChanges.length > 0) {
return;
}
// there is a bit redundant since didSave already handle the save changes
// but not sure if there are other non-editor related change we are trying
// to capture, so leave it be for now...
this.refreshDocumentChange(event.document);
}
onWillSaveTextDocument(event: vscode.TextDocumentWillSaveEvent): void {
if (event.document.isDirty) {
this.removeCachedTestResults(event.document, true);
}
}
onDidSaveTextDocument(document: vscode.TextDocument): void {
this.handleOnSaveRun(document);
this.refreshDocumentChange(document);
}
private async updateTestFileList(): Promise<void> {
return new Promise((resolve, reject) => {
this.processSession.scheduleProcess({
type: 'list-test-files',
onResult: (files, error, exitCode) => {
this.setTestFiles(files);
this.logging('debug', `found ${files?.length} testFiles`);
if (error) {
const msg =
'failed to retrieve test file list. TestExplorer might show incomplete test items';
this.extContext.output.write(error, 'new-line');
const errorType = getExitErrorDef(exitCode) ?? 'error';
this.extContext.output.write(msg, errorType);
this.logging('error', msg, error);
reject(error);
} else {
resolve();
}
},
});
});
}
onDidCreateFiles(_event: vscode.FileCreateEvent): void {
this.updateTestFileList();
}
onDidRenameFiles(_event: vscode.FileRenameEvent): void {
this.updateTestFileList();
}
onDidDeleteFiles(_event: vscode.FileDeleteEvent): void {
this.updateTestFileList();
}
toggleCoverage(): Promise<void> {
this.extContext.settings.runMode.toggleCoverage();
return this.triggerUpdateSettings(this.extContext.settings);
}
// exit defer runMode
async exitDeferMode(trigger?: TestExplorerRunRequest): Promise<void> {
if (trigger) {
this.enableOutputOnRun();
}
if (this.extContext.settings.runMode.config.deferred) {
this.extContext.settings.runMode.exitDeferMode();
this.extContext.output.write('exit defer mode', 'new-line');
await this.triggerUpdateSettings(this.extContext.settings);
if (trigger && this.testProvider) {
try {
await this.testProvider.runTests(trigger.request, trigger.token, true);
} catch (e) {
this.logging('error', 'failed to resume runs prior to defer mode', e);
this.extContext.output.write(
'failed to resume runs prior to defer mode, you might need to trigger the run again',
'error'
);
}
}
}
}
async saveRunMode(): Promise<void> {
try {
await this.extContext.settings.runMode.save(this.extContext.workspace);
} catch (e) {
this.logging('error', 'failed to save runMode', e);
this.extContext.output.write(
'failed to save the runMode settings. ${e}. \r\nPlease report this error.',
'error'
);
}
}
// this method is invoked by the TestExplorer UI
async changeRunMode(): Promise<void> {
const runMode = await this.extContext.settings.runMode.quickSwitch(this.vscodeContext);
if (runMode) {
const newSettings = { ...this.extContext.settings, runMode };
return this.triggerUpdateSettings(newSettings);
}
}
async runItemCommand(testItem: vscode.TestItem, itemCommand: ItemCommand): Promise<void> {
this.enableOutputOnRun();
await this.exitDeferMode();
return this.testProvider?.runItemCommand(testItem, itemCommand);
}
enableLoginShell(): void {
if (this.extContext.settings.shell.useLoginShell) {
return;
}
this.extContext.settings.shell.enableLoginShell();
this.triggerUpdateSettings(this.extContext.settings);
this.extContext.output.write(
`possible process env issue detected, restarting with a login-shell...\r\n`,
'warn'
);
}
private setupStatusBar(): void {
this.updateStatusBar({ state: 'initial' });
}
private resetStatusBar(): void {
this.updateStatusBar({
state: 'initial',
mode: this.extContext.settings.runMode,
stats: emptyTestStats(),
});
}
private updateStatusBar(status: StatusBarUpdate): void {
this.status.update(status);
}
_updateCoverageMap(coverageMap?: CoverageMapData): Promise<void> {
return this.coverageMapProvider.update(coverageMap).then(() => {
this.coverageCodeLensProvider.coverageChanged();
this.coverageOverlay.updateVisibleEditors();
});
}
private updateWithData(data: JestTotalResults, process: JestProcessInfo): void {
const noAnsiData = resultsWithoutAnsiEscapeSequence(data);
const normalizedData = resultsWithLowerCaseWindowsDriveLetters(noAnsiData);
this.events.onTestDataAvailable.fire({ data: normalizedData, process });
this._updateCoverageMap(normalizedData.coverageMap);
const statusList = this.testResultProvider.updateTestResults(normalizedData, process);
updateDiagnostics(statusList, this.failDiagnostics);
this.refreshDocumentChange();
}
}