-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sidebar support #357
Closed
Closed
Sidebar support #357
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c3276aa
first version of sidebar implemented
gstamac 4e40c11
open test file on node click
gstamac 50e0088
cache test file parsing for diagnostics
gstamac 039dae9
renamed sidebar icons
gstamac 15d9fd2
Merge remote-tracking branch 'upstream/master'
gstamac 2732054
removed orig files from merge
gstamac b0df658
Merge remote-tracking branch 'upstream/master'
gstamac d86f461
fixed tests and merge errors
gstamac 67d3e2b
use verbose for CI tests
gstamac 34ecbef
removed console.log from JestExt.test.ts
gstamac 40eec87
added changelog
gstamac 2d19ca3
removed verbose for CI tests
gstamac 084c4e1
added support for tests without describe
gstamac cce9684
changed default settings for sidebar
gstamac bb78173
fixed settings tests
gstamac 4f9ad71
try to fix Travis CI with --maxWorkers=4
gstamac 7ccdd30
Merge remote-tracking branch 'upstream/master'
gstamac f576f45
sidebar update on partial test run
gstamac 9ea3637
fixed "== should be ==="
gstamac 832c1f6
listen to settings update
gstamac 02ec9ad
rename sidebar config names
gstamac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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,162 @@ | ||
import * as vscode from 'vscode' | ||
import { TestResultFile, TestResultSuite, TestResultTest } from './TestResultTree' | ||
import { extensionName } from '../appGlobals' | ||
|
||
export type NodeStatus = 'unknown' | 'passed' | 'failed' | 'skipped' | ||
|
||
export class JestTreeNode extends vscode.TreeItem { | ||
constructor( | ||
label: string, | ||
public readonly children: JestTreeNode[], | ||
context: SidebarContext, | ||
public contextValue: string = '', | ||
public readonly status: NodeStatus = 'unknown' | ||
) { | ||
super(label, children.length > 0 ? context.getTreeItemCollapsibleState() : vscode.TreeItemCollapsibleState.None) | ||
|
||
if (this.status === 'unknown') { | ||
this.status = this.calculateStatus() | ||
} | ||
|
||
this.iconPath = context.getIconPath(this.status) | ||
} | ||
|
||
get tooltip(): string { | ||
return this.terseTooltip | ||
} | ||
|
||
get terseTooltip(): string { | ||
if (this.children.length > 0) { | ||
return this.children.map(c => `${this.label} > ` + c.terseTooltip.replace(/\n/g, `\n${this.label} > `)).join('\n') | ||
} | ||
const prettyStatus = this.status.charAt(0).toUpperCase() + this.status.toLowerCase().slice(1) | ||
return `${this.label} ● ${prettyStatus}` | ||
} | ||
|
||
calculateStatus(): NodeStatus { | ||
if (this.children.length > 0) { | ||
if (this.children.find(c => c.status === 'failed')) { | ||
return 'failed' | ||
} | ||
if (this.children.find(c => c.status === 'skipped')) { | ||
return 'skipped' | ||
} | ||
if (!this.children.find(c => c.status !== 'passed')) { | ||
return 'passed' | ||
} | ||
} | ||
|
||
return 'unknown' | ||
} | ||
} | ||
|
||
export class JestTreeNodeForTest extends JestTreeNode { | ||
constructor(private test: TestResultTest, context: SidebarContext) { | ||
super(test.name, [], context, 'test', convertTestStatus(test.status)) | ||
} | ||
|
||
get tooltip(): string { | ||
if (this.test.failureMessages.length > 0) { | ||
return `${this.terseTooltip}\n\n${this.test.failureMessages.join('\n')}` | ||
} | ||
return this.terseTooltip | ||
} | ||
|
||
get command(): vscode.Command { | ||
return { | ||
title: 'Show test', | ||
command: `${extensionName}.show-test`, | ||
arguments: [this.test.filename, this.test.line], | ||
} | ||
// codeLens.command = { | ||
// arguments: [codeLens.fileName, escapeRegExp(codeLens.testName)], | ||
// command: `${extensionName}.run-test`, | ||
// title: 'Debug', | ||
// } | ||
} | ||
} | ||
|
||
export interface ISidebarSettings { | ||
showFiles: boolean | ||
autoExpand: boolean | ||
} | ||
|
||
export class SidebarContext { | ||
showFiles: boolean | ||
autoExpand: boolean | ||
|
||
constructor(private extensionContext: vscode.ExtensionContext, settings: ISidebarSettings) { | ||
this.updateSettings(settings) | ||
} | ||
|
||
updateSettings(settings: ISidebarSettings): void { | ||
this.autoExpand = settings.autoExpand | ||
this.showFiles = settings.showFiles | ||
} | ||
|
||
getTreeItemCollapsibleState() { | ||
return this.autoExpand ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.Collapsed | ||
} | ||
|
||
getIconPath(iconColor: string) { | ||
return { | ||
light: this.extensionContext.asAbsolutePath('./src/SideBar/light-' + iconColor + '.svg'), | ||
dark: this.extensionContext.asAbsolutePath('./src/SideBar/dark-' + iconColor + '.svg'), | ||
} | ||
} | ||
} | ||
|
||
function convertTestStatus(testStatus: 'failed' | 'passed' | 'pending'): NodeStatus { | ||
return testStatus === 'pending' ? 'skipped' : testStatus | ||
} | ||
|
||
export function generateTree(files: undefined | TestResultFile[], context: SidebarContext): JestTreeNode { | ||
const rootNode = new JestTreeNode( | ||
'Tests', | ||
files === undefined ? [] : getNodesFromFiles(files, context), | ||
context, | ||
'root' | ||
) | ||
rootNode.collapsibleState = vscode.TreeItemCollapsibleState.Expanded | ||
return rootNode | ||
} | ||
|
||
function getNodesFromFiles(files: TestResultFile[], context: SidebarContext): JestTreeNode[] { | ||
return [].concat(...files.map(f => getNodesFromFile(f, context))) | ||
} | ||
|
||
function getNodesFromFile(file: TestResultFile, context: SidebarContext): JestTreeNode[] { | ||
if (context.showFiles) { | ||
return [new JestTreeNode(cleanFilename(file.name), getNodesFromSuite(file.suite, context), context, 'file')] | ||
} else { | ||
if (file.suite.tests.length === 0) { | ||
return getNodesFromSuite(file.suite, context) | ||
} else { | ||
return file.suite.suites | ||
.map(s => new JestTreeNode(s.name, getNodesFromSuite(s, context), context, 'suite')) | ||
.concat([ | ||
new JestTreeNode( | ||
cleanFilename(file.name), | ||
file.suite.tests.map(t => new JestTreeNodeForTest(t, context)), | ||
context, | ||
'file' | ||
), | ||
]) | ||
} | ||
} | ||
} | ||
|
||
function cleanFilename(filename: string): string { | ||
const file = vscode.Uri.file(filename) | ||
const folder = vscode.workspace.getWorkspaceFolder(file) | ||
if (folder && folder.uri && file.path.toLowerCase().startsWith(folder.uri.path.toLowerCase())) { | ||
return file.path.substring(folder.uri.path.length + 1) | ||
} | ||
return filename | ||
} | ||
|
||
function getNodesFromSuite(suite: TestResultSuite, context: SidebarContext): JestTreeNode[] { | ||
return suite.suites | ||
.map(s => new JestTreeNode(s.name, getNodesFromSuite(s, context), context, 'suite')) | ||
.concat(suite.tests.map(t => new JestTreeNodeForTest(t, context))) | ||
} |
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,74 @@ | ||
import * as vscode from 'vscode' | ||
import { JestTotalResults, JestFileResults } from 'jest-editor-support' | ||
import { JestTreeNode, SidebarContext, ISidebarSettings, generateTree } from './JestTreeNode' | ||
import { TestResultFile } from './TestResultTree' | ||
import { TestResultProvider } from '../TestResults' | ||
|
||
export class JestTreeProvider implements vscode.TreeDataProvider<JestTreeNode> { | ||
private _onDidChangeTreeData: vscode.EventEmitter<JestTreeNode | undefined> = new vscode.EventEmitter< | ||
JestTreeNode | undefined | ||
>() | ||
readonly onDidChangeTreeData: vscode.Event<JestTreeNode | undefined> = this._onDidChangeTreeData.event | ||
|
||
private context: SidebarContext | ||
private rootNode: JestTreeNode | ||
private allResults: JestFileResults[] | ||
|
||
constructor( | ||
private testResultProvider: TestResultProvider, | ||
extensionContext: vscode.ExtensionContext, | ||
settings: ISidebarSettings | ||
) { | ||
this.context = new SidebarContext(extensionContext, settings) | ||
this.clear() | ||
} | ||
|
||
updateSettings(settings: ISidebarSettings): void { | ||
this.context.updateSettings(settings) | ||
} | ||
|
||
clear(): void { | ||
this.allResults = [] | ||
this.rootNode = generateTree(undefined, this.context) | ||
this._onDidChangeTreeData.fire() | ||
} | ||
|
||
refresh(data: JestTotalResults): void { | ||
this.loadTestResults(data) | ||
this._onDidChangeTreeData.fire() | ||
} | ||
|
||
getTreeItem(element: JestTreeNode): vscode.TreeItem { | ||
return element | ||
} | ||
|
||
getChildren(element?: JestTreeNode): JestTreeNode[] { | ||
if (!element) { | ||
return this.getRootElements() | ||
} else { | ||
return this.getElementChildren(element) | ||
} | ||
} | ||
|
||
private loadTestResults(data: JestTotalResults) { | ||
this.allResults = this.allResults | ||
.filter(r => !data.testResults.find(r1 => r1.name === r.name)) | ||
.concat(data.testResults) | ||
.sort((a, b) => a.name.localeCompare(b.name)) | ||
const testFiles = this.allResults.map(r => this.loadTestResultsForFile(r)) | ||
this.rootNode = generateTree(testFiles, this.context) | ||
} | ||
|
||
private loadTestResultsForFile(data: JestFileResults): TestResultFile { | ||
const parsedResults = this.testResultProvider.getResults(data.name) | ||
return new TestResultFile(data, parsedResults) | ||
} | ||
|
||
private getRootElements(): JestTreeNode[] { | ||
return [this.rootNode] | ||
} | ||
|
||
private getElementChildren(node: JestTreeNode): JestTreeNode[] { | ||
return node.children | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if we should process all the tests up front like this, giving we will only need to display the tree when the file is actually displayed in the editor... please see if you can move the logic to
triggerUpdateActiveEditor
instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tree is displayed every time the tests are run not only when the file is selected. You probably mean the outline tree but what we are showing here is the test sidebar tree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed, I was thinking about the outline view, my bad.
trying it again on vscode-jest itself, added a few fake tests and encountered the following issues:
jest.showFilesInSidebar
setting, howeverjest.showFilesInSidebar
to true?it
blocks are not shown, and if reload/restart vscode, it crashed the plugin, put it in the never-ending spinning "Starting watch mode" and the Test sidebar is empty... (same error as mentioned in earlier comment.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added support for root tests, removed the folder name from filename and changed the default setting to show files by default. My practice has always been to have all tests inside describe and to have only one describe per file. That's why I chose the implementation I did.
I see you have to reload the window when the settings change but I think that's the case for all vscode-jest settings because
getExtensionSettings
is called onextension.activate
event.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please take a look at triggerUpdateSettings