diff --git a/package.json b/package.json index 57988753a..f9ed436a8 100644 --- a/package.json +++ b/package.json @@ -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": "" }, "jest.pathToConfig": { "description": "The path to your Jest configuration file", @@ -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" diff --git a/src/JestExt.ts b/src/JestExt.ts index 4ab0e7d1a..a9b9061a3 100644 --- a/src/JestExt.ts +++ b/src/JestExt.ts @@ -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 { @@ -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() @@ -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 }) this.coverageOverlay.enabled = updatedSettings.showCoverageOnLoad diff --git a/src/SnapshotCodeLens/SnapshotCodeLensProvider.ts b/src/SnapshotCodeLens/SnapshotCodeLensProvider.ts index ab42f445a..d90868a75 100644 --- a/src/SnapshotCodeLens/SnapshotCodeLensProvider.ts +++ b/src/SnapshotCodeLens/SnapshotCodeLensProvider.ts @@ -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) 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) diff --git a/src/TestResults/TestResultProvider.ts b/src/TestResults/TestResultProvider.ts index 4d835a135..c14ffafc6 100644 --- a/src/TestResults/TestResultProvider.ts +++ b/src/TestResults/TestResultProvider.ts @@ -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' @@ -99,7 +99,7 @@ export class TestResultProvider { return result } - updateTestResults(data: FormattedTestResults): TestFileAssertionStatus[] { + updateTestResults(data: JestTotalResults): TestFileAssertionStatus[] { this.resetCache() return this.reconciler.updateFileWithJestStatus(data) } diff --git a/src/helpers.ts b/src/helpers.ts index 02043f779..3bd851c59 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -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' @@ -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 } + vscode.window.showWarningMessage("Jest couldn't be found. Maybe you forgot to run `npm install`?") return 'jest' } diff --git a/tests/extension.test.ts b/tests/extension.test.ts index 466b0b4d6..8282fc392 100644 --- a/tests/extension.test.ts +++ b/tests/extension.test.ts @@ -135,7 +135,7 @@ describe('Extension', () => { enableSnapshotPreviews: true, enableSnapshotUpdateMessages: true, pathToConfig: '', - pathToJest: null, + pathToJest: '', restartJestOnSnapshotUpdate: false, rootPath: '', runAllTestsFirst: true, diff --git a/tests/helpers.test.ts b/tests/helpers.test.ts index 99ce8223d..673e6ae13 100644 --- a/tests/helpers.test.ts +++ b/tests/helpers.test.ts @@ -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 -const defaultPathToJest = 'node_modules/.bin/jest' +const readFileMock = readFileSync as jest.Mock 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') + }) + }) + 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) }) @@ -28,7 +75,7 @@ describe('ModuleHelpers', () => { const workspace: any = { rootPath: '', - pathToJest: defaultPathToJest, + pathToJest: '', } expect(pathToJestPackageJSON(workspace)).toBe(expected) }) @@ -39,7 +86,7 @@ describe('ModuleHelpers', () => { const workspace: any = { rootPath: '', - pathToJest: defaultPathToJest, + pathToJest: '', } expect(pathToJestPackageJSON(workspace)).toBe(expected) }) @@ -52,7 +99,7 @@ describe('ModuleHelpers', () => { const workspace: any = { rootPath: path.join('..', '..'), - pathToJest: defaultPathToJest, + pathToJest: '', } expect(pathToJestPackageJSON(workspace)).toBe(expected) }) @@ -63,7 +110,7 @@ describe('ModuleHelpers', () => { const workspace: any = { rootPath: path.join('..', '..'), - pathToJest: defaultPathToJest, + pathToJest: '', } expect(pathToJestPackageJSON(workspace)).toBe(expected) }) diff --git a/typings/jest-editor-support.d.ts b/typings/jest-editor-support.d.ts deleted file mode 100644 index f437038ea..000000000 --- a/typings/jest-editor-support.d.ts +++ /dev/null @@ -1,41 +0,0 @@ -import * as editor from 'jest-editor-support' -import { ChildProcess } from 'child_process' -declare module 'jest-editor-support' { - interface SpawnOptions { - shell?: boolean - } - - interface Options { - createProcess?(workspace: ProjectWorkspace, args: string[], options?: SpawnOptions): ChildProcess - testNamePattern?: string - testFileNamePattern?: string - shell?: boolean - } - - interface JestTotalResults { - coverageMap: any - } - - type FormattedTestResults = { - testResults: TestResult[] - } - - type TestResult = { - name: string - } - - interface SnapshotMetadata { - node: { - loc: editor.Node - } - name: string - content: string - count: number - exists: boolean - } - - class Snapshot { - constructor(parser?: any, customMatchers?: Array) - getMetadata(filepath: string): Array - } -} diff --git a/yarn.lock b/yarn.lock index e1f43832b..d933f62a4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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: @@ -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" @@ -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"