diff --git a/packages/jest-core/src/FailedTestsInteractiveMode.ts b/packages/jest-core/src/FailedTestsInteractiveMode.ts index d3086a9a29ec..7fe7d8d681f2 100644 --- a/packages/jest-core/src/FailedTestsInteractiveMode.ts +++ b/packages/jest-core/src/FailedTestsInteractiveMode.ts @@ -1,3 +1,10 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + import ansiEscapes = require('ansi-escapes'); import chalk = require('chalk'); import type {AggregatedResult, AssertionLocation} from '@jest/test-result'; @@ -30,7 +37,9 @@ export default class FailedTestsInteractiveMode { put(key: string): void { switch (key) { case 's': - if (this._skippedNum === this._testAssertions.length) break; + if (this._skippedNum === this._testAssertions.length) { + break; + } this._skippedNum += 1; // move skipped test to the end @@ -61,12 +70,12 @@ export default class FailedTestsInteractiveMode { } run( - failedSnapshotTestAssertions: Array, + failedTestAssertions: Array, updateConfig: RunnerUpdateFunction, ): void { - if (!failedSnapshotTestAssertions.length) return; + if (failedTestAssertions.length === 0) return; - this._testAssertions = [...failedSnapshotTestAssertions]; + this._testAssertions = [...failedTestAssertions]; this._countPaths = this._testAssertions.length; this._updateTestRunnerConfig = updateConfig; this._isActive = true; @@ -74,7 +83,7 @@ export default class FailedTestsInteractiveMode { } updateWithResults(results: AggregatedResult): void { - if (!results.snapshot.failure && results.numFailedTests) { + if (!results.snapshot.failure && results.numFailedTests > 0) { return this._drawUIOverlay(); } @@ -108,9 +117,9 @@ export default class FailedTestsInteractiveMode { let stats = `${pluralize('test', this._countPaths)} reviewed`; - if (this._skippedNum) { + if (this._skippedNum > 0) { const skippedText = chalk.bold.yellow( - pluralize('snapshot', this._skippedNum) + ' skipped', + pluralize('test', this._skippedNum) + ' skipped', ); stats = `${stats}, ${skippedText}`; @@ -136,9 +145,9 @@ export default class FailedTestsInteractiveMode { const numRemaining = this._countPaths - numPass - this._skippedNum; let stats = `${pluralize('test', numRemaining)} remaining`; - if (this._skippedNum) { + if (this._skippedNum > 0) { const skippedText = chalk.bold.yellow( - pluralize('snapshot', this._skippedNum) + ' skipped', + pluralize('test', this._skippedNum) + ' skipped', ); stats = `${stats}, ${skippedText}`; diff --git a/packages/jest-core/src/__tests__/FailedTestsInteractiveMode.test.js b/packages/jest-core/src/__tests__/FailedTestsInteractiveMode.test.js index cf456194049c..0f369d342e9e 100644 --- a/packages/jest-core/src/__tests__/FailedTestsInteractiveMode.test.js +++ b/packages/jest-core/src/__tests__/FailedTestsInteractiveMode.test.js @@ -1,3 +1,10 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + import chalk from 'chalk'; import {specialChars} from 'jest-util'; import FailedTestsInteractiveMode from '../FailedTestsInteractiveMode'; diff --git a/packages/jest-core/src/plugins/FailedTestsInteractive.ts b/packages/jest-core/src/plugins/FailedTestsInteractive.ts index abf3c08b2c08..8fcb9b3f9c88 100644 --- a/packages/jest-core/src/plugins/FailedTestsInteractive.ts +++ b/packages/jest-core/src/plugins/FailedTestsInteractive.ts @@ -1,3 +1,10 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + import type {AggregatedResult, AssertionLocation} from '@jest/test-result'; import type {Config} from '@jest/types'; import { @@ -14,9 +21,7 @@ export default class FailedTestsInteractivePlugin extends BaseWatchPlugin { apply(hooks: JestHookSubscriber): void { hooks.onTestRunComplete(results => { - this._failedTestAssertions = this.getFailedSnapshotTestAssertions( - results, - ); + this._failedTestAssertions = this.getFailedTestAssertions(results); if (this._manager.isActive()) this._manager.updateWithResults(results); }); @@ -41,7 +46,13 @@ export default class FailedTestsInteractivePlugin extends BaseWatchPlugin { updateConfigAndRun: UpdateConfigCallback, ): Promise { return new Promise(resolve => { - if (!this._failedTestAssertions?.length) return resolve(); + if ( + !this._failedTestAssertions || + this._failedTestAssertions.length === 0 + ) { + resolve(); + return; + } this._manager.run(this._failedTestAssertions, failure => { updateConfigAndRun({ @@ -50,12 +61,14 @@ export default class FailedTestsInteractivePlugin extends BaseWatchPlugin { testPathPattern: failure?.path || '', }); - if (!this._manager.isActive()) resolve(); + if (!this._manager.isActive()) { + resolve(); + } }); }); } - private getFailedSnapshotTestAssertions( + private getFailedTestAssertions( results: AggregatedResult, ): Array { const failedTestPaths: Array = []; diff --git a/packages/jest-core/src/plugins/__tests__/FailedTestsInteractive.test.js b/packages/jest-core/src/plugins/__tests__/FailedTestsInteractive.test.js index b24e78c00cf2..f8327540237f 100644 --- a/packages/jest-core/src/plugins/__tests__/FailedTestsInteractive.test.js +++ b/packages/jest-core/src/plugins/__tests__/FailedTestsInteractive.test.js @@ -1,32 +1,18 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + import FailedTestsInteractivePlugin from '../FailedTestsInteractive'; describe('FailedTestsInteractive', () => { it('returns usage info when failing tests are present', () => { expect(new FailedTestsInteractivePlugin({}).getUsageInfo()).toBeNull(); - const activateablePlugin = new FailedTestsInteractivePlugin({}); - let mockCallback; - - activateablePlugin.apply({ - onTestRunComplete: callback => { - mockCallback = callback; - }, - }); - - mockCallback({ - snapshot: {}, - testResults: [{testResults: [{status: 'failed'}]}], - }); - - expect(activateablePlugin.getUsageInfo()).toEqual({ - key: 'i', - prompt: 'run failing tests interactively', - }); - }); - - it('calls config update when receiving failed tests', () => { const mockUpdate = jest.fn(); - const plugin = new FailedTestsInteractivePlugin({}); + const activateablePlugin = new FailedTestsInteractivePlugin({}); const testAggregate = { snapshot: {}, testResults: [ @@ -38,16 +24,19 @@ describe('FailedTestsInteractive', () => { }; let mockCallback; - plugin.apply({ + activateablePlugin.apply({ onTestRunComplete: callback => { mockCallback = callback; }, }); mockCallback(testAggregate); + activateablePlugin.run(null, mockUpdate); - plugin.run(null, mockUpdate); - + expect(activateablePlugin.getUsageInfo()).toEqual({ + key: 'i', + prompt: 'run failing tests interactively', + }); expect(mockUpdate).toHaveBeenCalledWith({ mode: 'watch', testNamePattern: `^${testAggregate.testResults[0].testResults[0].fullName}$`,