Skip to content
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

Configure when the debug CodeLens is displayed #292

Merged
merged 7 commits into from
May 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
out
node_modules
_jest-editor/
.vscode/symbols.json
coverage/
integrations/create-react-example/node_modules
/coverage/
/.vscode/symbols.json
node_modules/
out/

**/.DS_Store
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ Bug-fixes within the same version aren't needed

## Master

* [your thing] - [you]
* Adds a setting to control when the debug CodeLens appears - seanpoulter

-->

## Master

### 2.7.2

* Fix decorators showing corrupt values when two tests of the same name in one file (e.g. in different describe blocks) exist - ThomasRooney
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@
"type": "boolean",
"default": true
},
"jest.debugCodeLens.showWhenTestStateIn": {
"description": "Show the debug CodeLens when the it/test block state is in this collection",
"type": "array",
"items": {
"enum": ["fail", "pass", "skip", "unknown"]
},
"default": ["fail", "unknown"]
},
"jest.enableSnapshotPreviews": {
"description": "Whether snapshot previews should show",
"type": "boolean",
Expand Down
25 changes: 16 additions & 9 deletions src/DebugCodeLens/DebugCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import { escapeRegExp } from '../helpers'
import { basename } from 'path'
import { DebugCodeLens } from './DebugCodeLens'
import { TestReconciliationState, TestResultProvider } from '../TestResults'
import { TestState, TestStateByTestReconciliationState } from './TestState'

export class DebugCodeLensProvider implements vscode.CodeLensProvider {
private _enabled: boolean
private _showWhenTestStateIn: TestState[]
onDidChange: vscode.EventEmitter<void>
testResultProvider: TestResultProvider

constructor(testResultProvider: TestResultProvider, enabled: boolean) {
constructor(testResultProvider: TestResultProvider, showWhenTestStateIn: TestState[]) {
this.testResultProvider = testResultProvider
this._enabled = enabled
this._showWhenTestStateIn = showWhenTestStateIn
this.onDidChange = new vscode.EventEmitter()
}

get enabled() {
return this._enabled
get showWhenTestStateIn() {
return this._showWhenTestStateIn
}

set enabled(value: boolean) {
this._enabled = value
set showWhenTestStateIn(value: TestState[]) {
this._showWhenTestStateIn = value
this.onDidChange.fire()
}

Expand All @@ -32,15 +33,16 @@ export class DebugCodeLensProvider implements vscode.CodeLensProvider {
provideCodeLenses(document: vscode.TextDocument, _: vscode.CancellationToken): vscode.CodeLens[] {
const result = []

if (!this._enabled || document.isUntitled) {
if (this._showWhenTestStateIn.length === 0 || document.isUntitled) {
return result
}

const filePath = document.fileName
const testResults = this.testResultProvider.getResults(filePath)
const fileName = basename(document.fileName)

for (const test of testResults) {
if (test.status === TestReconciliationState.KnownSuccess || test.status === TestReconciliationState.KnownSkip) {
if (!this.showCodeLensAboveTest(test)) {
continue
}

Expand All @@ -53,6 +55,11 @@ export class DebugCodeLensProvider implements vscode.CodeLensProvider {
return result
}

showCodeLensAboveTest(test: { status: TestReconciliationState }) {
const state = TestStateByTestReconciliationState[test.status]
return this._showWhenTestStateIn.includes(state)
}

resolveCodeLens(codeLens: vscode.CodeLens, _: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens> {
if (codeLens instanceof DebugCodeLens) {
codeLens.command = {
Expand Down
15 changes: 15 additions & 0 deletions src/DebugCodeLens/TestState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { TestReconciliationState } from '../TestResults'

export enum TestState {
Fail = 'fail',
Pass = 'pass',
Skip = 'skip',
Unknown = 'unknown',
}

export const TestStateByTestReconciliationState = {
[TestReconciliationState.KnownFail]: TestState.Fail,
[TestReconciliationState.KnownSkip]: TestState.Skip,
[TestReconciliationState.KnownSuccess]: TestState.Pass,
[TestReconciliationState.Unknown]: TestState.Unknown,
}
1 change: 1 addition & 0 deletions src/DebugCodeLens/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { DebugCodeLensProvider } from './DebugCodeLensProvider'
export { TestState } from './TestState'
11 changes: 8 additions & 3 deletions src/IPluginSettings.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { TestState } from './DebugCodeLens'

export interface IPluginSettings {
autoEnable?: boolean
enableCodeLens?: boolean
debugCodeLens: {
enabled: boolean
showWhenTestStateIn: TestState[]
}
enableInlineErrorMessages?: boolean
enableSnapshotPreviews?: boolean
enableSnapshotUpdateMessages?: boolean
pathToJest?: string
pathToConfig?: string
pathToJest?: string
restartJestOnSnapshotUpdate?: boolean
rootPath?: string
runAllTestsFirst?: boolean
showCoverageOnLoad: boolean
restartJestOnSnapshotUpdate?: boolean
}
10 changes: 8 additions & 2 deletions src/JestExt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ export class JestExt {
this.coverageOverlay = new CoverageOverlay(this.coverageMapProvider, pluginSettings.showCoverageOnLoad)

this.testResultProvider = new TestResultProvider()
this.debugCodeLensProvider = new DebugCodeLensProvider(this.testResultProvider, pluginSettings.enableCodeLens)
this.debugCodeLensProvider = new DebugCodeLensProvider(
this.testResultProvider,
pluginSettings.debugCodeLens.enabled ? pluginSettings.debugCodeLens.showWhenTestStateIn : []
)
this.debugConfigurationProvider = new DebugConfigurationProvider()

this.jestProcessManager = new JestProcessManager({
Expand Down Expand Up @@ -244,7 +247,10 @@ export class JestExt {
this.jestSettings = new Settings(this.workspace)

this.coverageOverlay.enabled = updatedSettings.showCoverageOnLoad
this.debugCodeLensProvider.enabled = updatedSettings.enableCodeLens

this.debugCodeLensProvider.showWhenTestStateIn = updatedSettings.debugCodeLens.enabled
? updatedSettings.debugCodeLens.showWhenTestStateIn
: []

this.stopProcess()

Expand Down
14 changes: 9 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { IPluginSettings } from './IPluginSettings'
import { registerStatusBar } from './statusBar'
import { registerSnapshotCodeLens, registerSnapshotPreview } from './SnapshotCodeLens'
import { registerCoverageCodeLens } from './Coverage'
import { TestState } from './DebugCodeLens'

let extensionInstance: JestExt

Expand Down Expand Up @@ -76,19 +77,22 @@ export function deactivate() {
extensionInstance.deactivate()
}

function getExtensionSettings(): IPluginSettings {
export function getExtensionSettings(): IPluginSettings {
const config = vscode.workspace.getConfiguration('jest')
return {
autoEnable: config.get<boolean>('autoEnable'),
pathToConfig: config.get<string>('pathToConfig'),
pathToJest: config.get<string>('pathToJest'),
enableCodeLens: config.get<boolean>('enableCodeLens'),
debugCodeLens: {
enabled: config.get<boolean>('enableCodeLens'),
showWhenTestStateIn: config.get<TestState[]>('debugCodeLens.showWhenTestStateIn'),
},
enableInlineErrorMessages: config.get<boolean>('enableInlineErrorMessages'),
enableSnapshotPreviews: config.get<boolean>('enableSnapshotPreviews'),
enableSnapshotUpdateMessages: config.get<boolean>('enableSnapshotUpdateMessages'),
pathToConfig: config.get<string>('pathToConfig'),
pathToJest: config.get<string>('pathToJest'),
restartJestOnSnapshotUpdate: config.get<boolean>('restartJestOnSnapshotUpdate'),
rootPath: path.join(vscode.workspace.rootPath, config.get<string>('rootPath')),
runAllTestsFirst: config.get<boolean>('runAllTestsFirst'),
showCoverageOnLoad: config.get<boolean>('showCoverageOnLoad'),
restartJestOnSnapshotUpdate: config.get<boolean>('restartJestOnSnapshotUpdate'),
}
}