From 9e43038b3a7d304e6ca417ed7f05cc9461866b87 Mon Sep 17 00:00:00 2001 From: Nathan L Smith Date: Mon, 19 Mar 2018 23:30:59 -0500 Subject: [PATCH] Coverage option for editor support When using the Jest extension in vscode, running coverage is often slow so it would be nice to be able to turn it on and off as needed. Add a coverage option to the runner. --- CHANGELOG.md | 2 + packages/jest-editor-support/index.d.ts | 1 + packages/jest-editor-support/src/Runner.js | 6 +++ .../src/__tests__/runner.test.js | 39 +++++++++++++++++++ packages/jest-editor-support/src/types.js | 1 + 5 files changed, 49 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 862db3c3d283..df47176ecbf8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +* `[jest-editor-support]` Add `coverage` option to runner + ([#5836](https://github.com/facebook/jest/pull/5836)) * `[expect]` Improve output format for mismatchedArgs in mock/spy calls. ([#5846](https://github.com/facebook/jest/pull/5846)) * `[jest-cli]` Add support for using `--coverage` in combination with watch diff --git a/packages/jest-editor-support/index.d.ts b/packages/jest-editor-support/index.d.ts index 3382c4b14262..5738c730567e 100644 --- a/packages/jest-editor-support/index.d.ts +++ b/packages/jest-editor-support/index.d.ts @@ -13,6 +13,7 @@ export interface SpawnOptions { } export interface Options { + coverage?: boolean; createProcess?( workspace: ProjectWorkspace, args: string[], diff --git a/packages/jest-editor-support/src/Runner.js b/packages/jest-editor-support/src/Runner.js index 4acfa3873b8e..c51e08adb4b7 100644 --- a/packages/jest-editor-support/src/Runner.js +++ b/packages/jest-editor-support/src/Runner.js @@ -66,6 +66,12 @@ export default class Runner extends EventEmitter { if (this.options.testFileNamePattern) { args.push(this.options.testFileNamePattern); } + if (this.options.coverage === true) { + args.push('--coverage'); + } + if (this.options.coverage === false) { + args.push('--no-coverage'); + } const options = { shell: this.options.shell, diff --git a/packages/jest-editor-support/src/__tests__/runner.test.js b/packages/jest-editor-support/src/__tests__/runner.test.js index 3253d1be9150..22e618809c87 100644 --- a/packages/jest-editor-support/src/__tests__/runner.test.js +++ b/packages/jest-editor-support/src/__tests__/runner.test.js @@ -180,6 +180,45 @@ describe('Runner', () => { expect((createProcess: any).mock.calls[0][1]).toContain('--watch'); }); + it('calls createProcess with the --coverage arg when provided', () => { + const expected = '--coverage'; + + const workspace: any = {}; + const options = {coverage: true}; + const sut = new Runner(workspace, options); + sut.start(false); + + const args = (createProcess: any).mock.calls[0][1]; + const index = args.indexOf(expected); + expect(index).not.toBe(-1); + }); + + it('calls createProcess with the ---no-coverage arg when provided and false', () => { + const expected = '--no-coverage'; + + const workspace: any = {}; + const options = {coverage: false}; + const sut = new Runner(workspace, options); + sut.start(false); + + const args = (createProcess: any).mock.calls[0][1]; + const index = args.indexOf(expected); + expect(index).not.toBe(-1); + }); + + it('calls createProcess without the --coverage arg when undefined', () => { + const expected = '--coverage'; + + const workspace: any = {}; + const options = {}; + const sut = new Runner(workspace, options); + sut.start(false); + + const args = (createProcess: any).mock.calls[0][1]; + const index = args.indexOf(expected); + expect(index).toBe(-1); + }); + it('calls createProcess with the --testNamePattern arg when provided', () => { const expected = 'testNamePattern'; diff --git a/packages/jest-editor-support/src/types.js b/packages/jest-editor-support/src/types.js index 22debbe9296d..e8708eb2133f 100644 --- a/packages/jest-editor-support/src/types.js +++ b/packages/jest-editor-support/src/types.js @@ -20,6 +20,7 @@ import type {ChildProcess} from 'child_process'; import type ProjectWorkspace from './project_workspace'; export type Options = { + coverage?: boolean, createProcess?: ( workspace: ProjectWorkspace, args: Array,