Skip to content

Commit

Permalink
chore: add copyright headers
Browse files Browse the repository at this point in the history
Apply suggestions from code review

Co-authored-by: Simen Bekkhus <[email protected]>

style: update usage info labels

test: merge tests with similar conditions
  • Loading branch information
NullDivision committed Jan 18, 2021
1 parent 98f1eb0 commit d853b70
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 40 deletions.
27 changes: 18 additions & 9 deletions packages/jest-core/src/FailedTestsInteractiveMode.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -61,20 +70,20 @@ export default class FailedTestsInteractiveMode {
}

run(
failedSnapshotTestAssertions: Array<AssertionLocation>,
failedTestAssertions: Array<AssertionLocation>,
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;
this._run();
}

updateWithResults(results: AggregatedResult): void {
if (!results.snapshot.failure && results.numFailedTests) {
if (!results.snapshot.failure && results.numFailedTests > 0) {
return this._drawUIOverlay();
}

Expand Down Expand Up @@ -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}`;
Expand All @@ -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}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
25 changes: 19 additions & 6 deletions packages/jest-core/src/plugins/FailedTestsInteractive.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
});
Expand All @@ -41,7 +46,13 @@ export default class FailedTestsInteractivePlugin extends BaseWatchPlugin {
updateConfigAndRun: UpdateConfigCallback,
): Promise<void> {
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({
Expand All @@ -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<AssertionLocation> {
const failedTestPaths: Array<AssertionLocation> = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -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: [
Expand All @@ -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}$`,
Expand Down

0 comments on commit d853b70

Please sign in to comment.