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

Add usage of shell when creating Settings #297

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
},
"jest.pathToJest": {
"description":
"The path to the Jest binary, or an npm command to run tests prefixed with `--` e.g. `npm test --`",
"The path to the Jest binary, or an npm command to run tests prefixed with `--` e.g. `node_modules/.bin/jest` or `npm test --`",
"type": "string",
"default": null
"default": ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of us is going to have a failing test if this changes. 😉

},
"jest.pathToConfig": {
"description": "The path to your Jest configuration file",
Expand Down Expand Up @@ -232,7 +232,7 @@
"elegant-spinner": "^1.0.1",
"istanbul-lib-coverage": "^1.1.1",
"istanbul-lib-source-maps": "^1.1.0",
"jest-editor-support": "22.4.0",
"jest-editor-support": "23.0.0-charlie.2",
"jest-snapshot": "^22.1.2",
"jest-test-typescript-parser": "^22.1.3",
"micromatch": "^2.3.11"
Expand Down
7 changes: 5 additions & 2 deletions src/JestExt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { DecorationOptions } from './types'
import { hasDocument, isOpenInMultipleEditors } from './editor'
import { CoverageOverlay } from './Coverage/CoverageOverlay'
import { JestProcess, JestProcessManager } from './JestProcessManagement'
import { platform } from 'os'
import { isWatchNotSupported, WatchMode } from './Jest'

export class JestExt {
Expand Down Expand Up @@ -68,7 +69,8 @@ export class JestExt {
this.failingAssertionDecorators = {}
this.failDiagnostics = vscode.languages.createDiagnosticCollection('Jest')
this.clearOnNextInput = true
this.jestSettings = new Settings(workspace)
const useShell = platform() === 'win32'
this.jestSettings = new Settings(workspace, { shell: useShell })
this.pluginSettings = pluginSettings

this.coverageMapProvider = new CoverageMapProvider()
Expand Down Expand Up @@ -259,7 +261,8 @@ export class JestExt {
this.workspace.pathToJest = pathToJest(updatedSettings)
this.workspace.pathToConfig = pathToConfig(updatedSettings)

this.jestSettings = new Settings(this.workspace)
const useShell = platform() === 'win32'
this.jestSettings = new Settings(this.workspace, { shell: useShell })

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing the same thing twice makes me think we should move this into one spot:

updateSettings(workspace) {
  this.jestSettings = new Settings(workspace, { shell: platform() === 'win32' })
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I at first just thought of moving the settings out into its own property (a la this.settingsOptions = { shell: platform() === 'win32' }, but your approach is tidier than that.

this.coverageOverlay.enabled = updatedSettings.showCoverageOnLoad

Expand Down
3 changes: 2 additions & 1 deletion src/SnapshotCodeLens/SnapshotCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export function registerSnapshotCodeLens(enableSnapshotPreviews: boolean) {

class SnapshotCodeLensProvider implements vscode.CodeLensProvider {
public provideCodeLenses(document: vscode.TextDocument, _token: vscode.CancellationToken) {
const snapshots = new Snapshot()
// temporarily use `undefined` as parser argument, until Jest/#6212 is published
const snapshots = new Snapshot(undefined)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about adding another line to the comment explaining when we can remove the argument? It'd be quick for someone who sees it to test it and see if the type updated type definition with the optional arg has been published with jestjs/jest#6212.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My plan was to remove it by myself as soon as there is a new release of Jest (I had it written to my calendar), but since jest-editor-supportv23 already includes that change, the undefined parameter won't be necessary anymore.

return snapshots.getMetadata(document.uri.fsPath).map(snapshot => {
const { line } = snapshot.node.loc.start
const range = new vscode.Range(line - 1, 0, line - 1, 0)
Expand Down
4 changes: 2 additions & 2 deletions src/TestResults/TestResultProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TestReconciler, FormattedTestResults } from 'jest-editor-support'
import { TestReconciler, JestTotalResults } from 'jest-editor-support'
import { TestFileAssertionStatus } from 'jest-editor-support'
import { TestReconciliationState } from './TestReconciliationState'
import { TestResult } from './TestResult'
Expand Down Expand Up @@ -99,7 +99,7 @@ export class TestResultProvider {
return result
}

updateTestResults(data: FormattedTestResults): TestFileAssertionStatus[] {
updateTestResults(data: JestTotalResults): TestFileAssertionStatus[] {
this.resetCache()
return this.reconciler.updateFileWithJestStatus(data)
}
Expand Down
20 changes: 9 additions & 11 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { platform } from 'os'
import { existsSync, readFileSync } from 'fs'
import { normalize, join } from 'path'
import * as vscode from 'vscode'

import { IPluginSettings } from './IPluginSettings'

Expand Down Expand Up @@ -62,22 +63,19 @@ function hasNodeExecutable(rootPath: string, executable: string): boolean {
*/
export function pathToJest(pluginSettings: IPluginSettings) {
if (pluginSettings.pathToJest) {
if (isBootstrappedWithCRA(pluginSettings.rootPath)) {
return 'npm test --'
}
return normalize(pluginSettings.pathToJest)
}

const platform = process.platform
if (platform === 'win32' && existsSync(join(pluginSettings.rootPath, 'node_modules', '.bin', 'jest.cmd'))) {
return normalize(join(pluginSettings.rootPath, 'node_modules', '.bin', 'jest.cmd'))
} else if (
(platform === 'linux' || platform === 'darwin') &&
existsSync(join(pluginSettings.rootPath, 'node_modules', '.bin', 'jest'))
) {
return normalize(join(pluginSettings.rootPath, 'node_modules', '.bin', 'jest'))
if (isBootstrappedWithCRA(pluginSettings.rootPath)) {
return 'npm test --'
}

const defaultPath = normalize('node_modules/.bin/jest')
if (existsSync(join(pluginSettings.rootPath, defaultPath))) {
return defaultPath
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would remove the check for jest.cmd and fail on Windows systems.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wouldn't fail since on Windows both files, 'node_modules/.bin/jest' and 'node_modules/.bin/jest.cmd' are present. But if we already check for the existence of the file, we probably really should keep caring about the extension.

}

vscode.window.showWarningMessage("Jest couldn't be found. Maybe you forgot to run `npm install`?")
return 'jest'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat idea. Should we worry about how noisy this could end up being for users who have installed Jest globally or are working with a package in a monorepo where node_modules isn't in the workspace root (like Jest)? I think we can build on this and lead users to helpful documentation about how to configure the extension. 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, especially because users using a global Jest install at the moment also see the "This extension relies on Jest 20+ features…" error message.
It was just that opening VS Code before npm install finished happened to me more often than thinking about global Jest installs.

}

Expand Down
2 changes: 1 addition & 1 deletion tests/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('Extension', () => {
enableSnapshotPreviews: true,
enableSnapshotUpdateMessages: true,
pathToConfig: '',
pathToJest: null,
pathToJest: '',
restartJestOnSnapshotUpdate: false,
rootPath: '<rootDir>',
runAllTestsFirst: true,
Expand Down
65 changes: 56 additions & 9 deletions tests/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,68 @@
jest.unmock('../src/helpers')
jest.mock('fs')

import { pathToJestPackageJSON, isCRATestCommand } from '../src/helpers'
import { existsSync } from 'fs'
jest.mock('vscode', () => ({
window: {
showWarningMessage: () => {},
},
}))

import { pathToJestPackageJSON, isCRATestCommand, pathToJest } from '../src/helpers'
import { existsSync, readFileSync } from 'fs'
import * as path from 'path'

const existsMock = existsSync as jest.Mock<boolean>
const defaultPathToJest = 'node_modules/.bin/jest'
const readFileMock = readFileSync as jest.Mock<string>

describe('ModuleHelpers', () => {
describe('pathToJest', () => {
it('should return the set value', () => {
const workspace: any = {
pathToJest: 'abcd',
}
expect(pathToJest(workspace)).toBe(workspace.pathToJest)
})

it('should recognize CRA apps', () => {
readFileMock.mockReturnValueOnce('{"scripts":{"test":"react-scripts test"}}')

const workspace: any = {
rootPath: '',
pathToJest: '',
}
expect(pathToJest(workspace)).toBe('npm test --')
})

it('should default to `node_modules/.bin/jest`', () => {
readFileMock.mockReturnValueOnce('{}')
existsMock.mockReturnValueOnce(true)

const defaultPath = path.normalize('node_modules/.bin/jest')
const workspace: any = {
rootPath: '',
pathToJest: '',
}
expect(pathToJest(workspace)).toBe(defaultPath)
})

it('should fallback to `jest` if everything other fails', () => {
readFileMock.mockReturnValueOnce('{}')
existsMock.mockReturnValueOnce(false)

const workspace: any = {
rootPath: '',
pathToJest: '',
}
expect(pathToJest(workspace)).toBe('jest')
})
})

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. This makes me sad we duplicated this effort. 😭

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least we now can keep the best of both PRs 😉. Sorry that I didn't mention it already when discussing 324.

describe('pathToJestPackageJSON', () => {
it('should return null when not found', () => {
existsMock.mockReturnValueOnce(false).mockReturnValueOnce(false)

const workspace: any = {
rootPath: '',
pathToJest: defaultPathToJest,
pathToJest: '',
}
expect(pathToJestPackageJSON(workspace)).toBe(null)
})
Expand All @@ -28,7 +75,7 @@ describe('ModuleHelpers', () => {

const workspace: any = {
rootPath: '',
pathToJest: defaultPathToJest,
pathToJest: '',
}
expect(pathToJestPackageJSON(workspace)).toBe(expected)
})
Expand All @@ -39,7 +86,7 @@ describe('ModuleHelpers', () => {

const workspace: any = {
rootPath: '',
pathToJest: defaultPathToJest,
pathToJest: '',
}
expect(pathToJestPackageJSON(workspace)).toBe(expected)
})
Expand All @@ -52,7 +99,7 @@ describe('ModuleHelpers', () => {

const workspace: any = {
rootPath: path.join('..', '..'),
pathToJest: defaultPathToJest,
pathToJest: '',
}
expect(pathToJestPackageJSON(workspace)).toBe(expected)
})
Expand All @@ -63,7 +110,7 @@ describe('ModuleHelpers', () => {

const workspace: any = {
rootPath: path.join('..', '..'),
pathToJest: defaultPathToJest,
pathToJest: '',
}
expect(pathToJestPackageJSON(workspace)).toBe(expected)
})
Expand Down
41 changes: 0 additions & 41 deletions typings/jest-editor-support.d.ts

This file was deleted.

47 changes: 41 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2260,19 +2260,28 @@ jest-diff@^22.4.3:
jest-get-type "^22.4.3"
pretty-format "^22.4.3"

jest-diff@^23.0.0-charlie.2:
version "23.0.0-charlie.2"
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-23.0.0-charlie.2.tgz#6321277332f4f28301b10acf02ea5e242e0387bd"
dependencies:
chalk "^2.0.1"
diff "^3.2.0"
jest-get-type "^22.1.0"
pretty-format "^23.0.0-charlie.2"

jest-docblock@^22.4.3:
version "22.4.3"
resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-22.4.3.tgz#50886f132b42b280c903c592373bb6e93bb68b19"
dependencies:
detect-newline "^2.1.0"

jest-editor-support@22.4.0:
version "22.4.0"
resolved "https://registry.yarnpkg.com/jest-editor-support/-/jest-editor-support-22.4.0.tgz#1f6e2359dbf90f84c1a8a16b911db5ef4333b0a0"
jest-editor-support@23.0.0-charlie.2:
version "23.0.0-charlie.2"
resolved "https://registry.yarnpkg.com/jest-editor-support/-/jest-editor-support-23.0.0-charlie.2.tgz#741cdbc0b5de9f034cc2eec0aa34ab47c9f6f72d"
dependencies:
babel-traverse "^6.14.1"
babylon "^6.14.1"
jest-snapshot "^22.4.0"
jest-snapshot "^23.0.0-charlie.2"

jest-editor-support@^22.4.3:
version "22.4.3"
Expand Down Expand Up @@ -2301,7 +2310,7 @@ jest-get-type@^21.2.0:
version "21.2.0"
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-21.2.0.tgz#f6376ab9db4b60d81e39f30749c6c466f40d4a23"

jest-get-type@^22.4.3:
jest-get-type@^22.1.0, jest-get-type@^22.4.3:
version "22.4.3"
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-22.4.3.tgz#e3a8504d8479342dd4420236b322869f18900ce4"

Expand Down Expand Up @@ -2347,6 +2356,14 @@ jest-matcher-utils@^22.4.3:
jest-get-type "^22.4.3"
pretty-format "^22.4.3"

jest-matcher-utils@^23.0.0-charlie.2:
version "23.0.0-charlie.2"
resolved "https://registry.yarnpkg.com/jest-matcher-utils/-/jest-matcher-utils-23.0.0-charlie.2.tgz#b564b9c02869dafd4b3f10343079ea92974a476a"
dependencies:
chalk "^2.0.1"
jest-get-type "^22.1.0"
pretty-format "^23.0.0-charlie.2"

jest-message-util@^22.4.3:
version "22.4.3"
resolved "https://registry.yarnpkg.com/jest-message-util/-/jest-message-util-22.4.3.tgz#cf3d38aafe4befddbfc455e57d65d5239e399eb7"
Expand Down Expand Up @@ -2423,7 +2440,7 @@ jest-serializer@^22.4.3:
version "22.4.3"
resolved "https://registry.yarnpkg.com/jest-serializer/-/jest-serializer-22.4.3.tgz#a679b81a7f111e4766235f4f0c46d230ee0f7436"

jest-snapshot@^22.1.2, jest-snapshot@^22.4.0, jest-snapshot@^22.4.3:
jest-snapshot@^22.1.2, jest-snapshot@^22.4.3:
version "22.4.3"
resolved "https://registry.yarnpkg.com/jest-snapshot/-/jest-snapshot-22.4.3.tgz#b5c9b42846ffb9faccb76b841315ba67887362d2"
dependencies:
Expand All @@ -2434,6 +2451,17 @@ jest-snapshot@^22.1.2, jest-snapshot@^22.4.0, jest-snapshot@^22.4.3:
natural-compare "^1.4.0"
pretty-format "^22.4.3"

jest-snapshot@^23.0.0-charlie.2:
version "23.0.0-charlie.2"
resolved "https://registry.yarnpkg.com/jest-snapshot/-/jest-snapshot-23.0.0-charlie.2.tgz#b56d5fa9cfde5df6a5a9b067c794ffdb2f1fe88a"
dependencies:
chalk "^2.0.1"
jest-diff "^23.0.0-charlie.2"
jest-matcher-utils "^23.0.0-charlie.2"
mkdirp "^0.5.1"
natural-compare "^1.4.0"
pretty-format "^23.0.0-charlie.2"

jest-test-typescript-parser@^22.1.3:
version "22.4.3"
resolved "https://registry.yarnpkg.com/jest-test-typescript-parser/-/jest-test-typescript-parser-22.4.3.tgz#805d6e203c1aea5811e7019d998fe1917f130930"
Expand Down Expand Up @@ -3472,6 +3500,13 @@ pretty-format@^22.4.3:
ansi-regex "^3.0.0"
ansi-styles "^3.2.0"

pretty-format@^23.0.0-charlie.2:
version "23.0.0-charlie.2"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-23.0.0-charlie.2.tgz#ab87c9fd8ff445bd2ace394c84000201a041f217"
dependencies:
ansi-regex "^3.0.0"
ansi-styles "^3.2.0"

private@^0.1.7:
version "0.1.8"
resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff"
Expand Down