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

When --watch is not supported, restart with --watchAll #329

Merged
merged 1 commit into from
May 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Bug-fixes within the same version aren't needed
* Adds a setting to control when the debug CodeLens appears - seanpoulter
* Support the "Jest: Start/Stop" and "Show output" commands without an active
text editor - seanpoulter
* Restart Jest with --watchAll when --watch is not supported without git/hg
- seanpoulter

-->

Expand Down
8 changes: 8 additions & 0 deletions src/Jest/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum WatchMode {
None = 'none',
Watch = 'watch',
WatchAll = 'watchAll',
}

export const isWatchNotSupported = (str = '') =>
new RegExp('^s*--watch is not supported without git/hg, please use --watchAlls*', 'im').test(str)
7 changes: 6 additions & 1 deletion 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 { isWatchNotSupported, WatchMode } from './Jest'

export class JestExt {
private workspace: ProjectWorkspace
Expand Down Expand Up @@ -103,6 +104,10 @@ export class JestExt {
return
}

if (isWatchNotSupported(message)) {
this.jestProcess.watchMode = WatchMode.WatchAll
}

// The "tests are done" message comes through stdErr
// We want to use this as a marker that the console should
// be cleared, as the next input will be from a new test run.
Expand Down Expand Up @@ -152,7 +157,7 @@ export class JestExt {
}

this.jestProcess = this.jestProcessManager.startJestProcess({
watch: true,
watchMode: WatchMode.Watch,
keepAlive: true,
exitCallback: (jestProcess, jestProcessInWatchMode) => {
if (jestProcessInWatchMode) {
Expand Down
17 changes: 8 additions & 9 deletions src/JestProcessManagement/JestProcess.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { platform } from 'os'
import { Runner, ProjectWorkspace } from 'jest-editor-support'
import { WatchMode } from '../Jest'

export class JestProcess {
static readonly keepAliveLimit = 5
Expand All @@ -10,21 +11,19 @@ export class JestProcess {
private resolve: Function
private keepAliveCounter: number
public keepAlive: boolean
public watchMode: boolean
public stopRequested: boolean
watchMode: WatchMode

private startRunner() {
this.stopRequested = false
let exited = false
// Use a shell to run Jest command on Windows in order to correctly spawn `.cmd` files
// For details see https://github.com/jest-community/vscode-jest/issues/98
const useShell = platform() === 'win32'
this.runner = new Runner(this.projectWorkspace, { shell: useShell })

const options = { shell: platform() === 'win32' }
this.runner = new Runner(this.projectWorkspace, options)

this.restoreJestEvents()

// pass watchMode into watchAll parameter also
this.runner.start(this.watchMode, this.watchMode)
this.runner.start(this.watchMode !== WatchMode.None, this.watchMode === WatchMode.WatchAll)

this.runner.on('debuggerProcessExit', () => {
if (!exited) {
Expand All @@ -50,11 +49,11 @@ export class JestProcess {

constructor({
projectWorkspace,
watchMode = false,
watchMode = WatchMode.None,
keepAlive = false,
}: {
projectWorkspace: ProjectWorkspace
watchMode?: boolean
watchMode?: WatchMode
keepAlive?: boolean
}) {
this.keepAlive = keepAlive
Expand Down
37 changes: 17 additions & 20 deletions src/JestProcessManagement/JestProcessManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ProjectWorkspace } from 'jest-editor-support'
import { JestProcess } from './JestProcess'
import { WatchMode } from '../Jest'

export class JestProcessManager {
private projectWorkspace: ProjectWorkspace
Expand All @@ -24,11 +25,11 @@ export class JestProcessManager {
}
}

private runJest({ watch, keepAlive, exitCallback }) {
private runJest({ watchMode, keepAlive, exitCallback }) {
const jestProcess = new JestProcess({
projectWorkspace: this.projectWorkspace,
watchMode: watch,
keepAlive: keepAlive,
watchMode,
keepAlive,
})

this.jestProcesses.unshift(jestProcess)
Expand All @@ -37,9 +38,9 @@ export class JestProcessManager {
return jestProcess
}

private run({ watch, keepAlive, exitCallback }) {
private run({ watchMode, keepAlive, exitCallback }) {
return this.runJest({
watch,
watchMode,
keepAlive,
exitCallback: exitedJestProcess => {
exitCallback(exitedJestProcess)
Expand All @@ -52,7 +53,7 @@ export class JestProcessManager {

private runAllTestsFirst(onExit) {
return this.runJest({
watch: false,
watchMode: WatchMode.None,
keepAlive: false,
exitCallback: onExit,
})
Expand All @@ -61,33 +62,29 @@ export class JestProcessManager {
public startJestProcess(
{
exitCallback = () => {},
watch = false,
watchMode = WatchMode.None,
keepAlive = false,
}: {
exitCallback?: Function
watch?: boolean
watchMode?: WatchMode
keepAlive?: boolean
} = {
exitCallback: () => {},
watch: false,
keepAlive: false,
}
} = {}
): JestProcess {
if (watch && this.runAllTestsFirstInWatchMode) {
if (watchMode !== WatchMode.None && this.runAllTestsFirstInWatchMode) {
return this.runAllTestsFirst(exitedJestProcess => {
this.removeJestProcessReference(exitedJestProcess)
const jestProcessInWatchMode = this.run({
watch: true,
keepAlive: keepAlive,
exitCallback: exitCallback,
watchMode: WatchMode.Watch,
keepAlive,
exitCallback,
})
exitCallback(exitedJestProcess, jestProcessInWatchMode)
})
} else {
return this.run({
watch: watch,
keepAlive: keepAlive,
exitCallback: exitCallback,
watchMode,
keepAlive,
exitCallback,
})
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Jest/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
jest.unmock('../../src/Jest')
import { isWatchNotSupported } from '../../src/Jest'

describe('isWatchNotSupported', () => {
it('returns true when matching the expected message', () => {
const str = '\n--watch is not supported without git/hg, please use --watchAll \n'
expect(isWatchNotSupported(str)).toBe(true)
})

it('returns false otherwise', () => {
expect(isWatchNotSupported()).toBe(false)
})
})
56 changes: 18 additions & 38 deletions tests/JestProcessManagement/JestProcess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ jest.unmock('../../src/JestProcessManagement/JestProcess')
import { Runner, ProjectWorkspace } from 'jest-editor-support'
import { JestProcess } from '../../src/JestProcessManagement/JestProcess'
import { EventEmitter } from 'events'
import { WatchMode } from '../../src/Jest'

describe('JestProcess', () => {
let projectWorkspaceMock
Expand Down Expand Up @@ -39,20 +40,15 @@ describe('JestProcess', () => {
expect(jestProcess.stopRequested).toBeFalsy()
})

it('accepts watchMode boolean argument', () => {
jestProcess = new JestProcess({
projectWorkspace: projectWorkspaceMock,
watchMode: true,
})
expect(jestProcess).not.toBe(null)
})

it('records watchMode in the watchMode property', () => {
const expected = WatchMode.Watch

jestProcess = new JestProcess({
projectWorkspace: projectWorkspaceMock,
watchMode: true,
watchMode: expected,
})
expect(jestProcess.watchMode).toBe(true)

expect(jestProcess.watchMode).toBe(expected)
})

it('creates an instance of jest-editor-support runner', () => {
Expand All @@ -69,34 +65,28 @@ describe('JestProcess', () => {
expect(runnerMock.mock.calls[0][0]).toBe(projectWorkspaceMock)
})

it('starts the jest-editor-support runner', () => {
it('starts the jest-editor-support Runner without watch mode by default', () => {
jestProcess = new JestProcess({
projectWorkspace: projectWorkspaceMock,
})
expect(runnerMockImplementation.start).toHaveBeenCalledTimes(1)
expect(runnerMockImplementation.start.mock.calls[0]).toEqual([false, false])
})

it('passes the watchMode argument == false to the start command when it is not provided', () => {
it('starts the jest-editor-support Runner in --watch mode', () => {
jestProcess = new JestProcess({
projectWorkspace: projectWorkspaceMock,
watchMode: WatchMode.Watch,
})
expect(runnerMockImplementation.start.mock.calls[0][0]).toBe(false)
expect(runnerMockImplementation.start.mock.calls[0]).toEqual([true, false])
})

it('passes the watchMode argument == false to the start command when it is false', () => {
it('starts the jest-editor-support Runner in --watchAll mode', () => {
jestProcess = new JestProcess({
projectWorkspace: projectWorkspaceMock,
watchMode: false,
watchMode: WatchMode.WatchAll,
})
expect(runnerMockImplementation.start.mock.calls[0][0]).toBe(false)
})

it('passes the watchMode argument == true to the start command when it is true', () => {
jestProcess = new JestProcess({
projectWorkspace: projectWorkspaceMock,
watchMode: true,
})
expect(runnerMockImplementation.start.mock.calls[0][0]).toBe(true)
expect(runnerMockImplementation.start.mock.calls[0]).toEqual([true, true])
})
})

Expand Down Expand Up @@ -241,7 +231,7 @@ describe('JestProcess', () => {
expect(jestProcess.keepAlive).toBeFalsy()
})

it('creates new instance of jest-editor-support runner', () => {
it('creates new instance of jest-editor-support Runner', () => {
jestProcess = new JestProcess({
projectWorkspace: projectWorkspaceMock,
keepAlive: true,
Expand Down Expand Up @@ -297,26 +287,16 @@ describe('JestProcess', () => {
expect(runnerMockImplementation.start).toHaveBeenCalledTimes(2)
})

it('passes the watchMode argument to the new runner instance when it is false', () => {
jestProcess = new JestProcess({
projectWorkspace: projectWorkspaceMock,
watchMode: false,
keepAlive: true,
})
jestProcess.onExit(onExit)
eventEmitter.emit('debuggerProcessExit')
expect(runnerMockImplementation.start.mock.calls[1][0]).toBe(false)
})

it('passes the watchMode argument to the new runner instance when it is true', () => {
jestProcess = new JestProcess({
projectWorkspace: projectWorkspaceMock,
watchMode: true,
watchMode: WatchMode.WatchAll,
keepAlive: true,
})
jestProcess.onExit(onExit)
eventEmitter.emit('debuggerProcessExit')
expect(runnerMockImplementation.start.mock.calls[1][0]).toBe(true)

expect(runnerMockImplementation.start.mock.calls[1]).toEqual([true, true])
})

it('removes all event listeners from the previous instance of the runner', () => {
Expand Down
Loading