Skip to content

Commit

Permalink
feat: add basic plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
NullDivision committed Nov 22, 2020
1 parent ff2b65c commit 64da282
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
52 changes: 52 additions & 0 deletions packages/jest-core/src/plugins/FailedTestsInteractive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type {AggregatedResult, AssertionLocation} from '@jest/test-result';
import {BaseWatchPlugin, JestHookSubscriber, UsageData} from 'jest-watcher';

export default class FailedTestsInteractivePlugin extends BaseWatchPlugin {
private _failedSnapshotTestAssertions?: Array<AssertionLocation>;

apply(hooks: JestHookSubscriber): void {
hooks.onTestRunComplete(results => {
this._failedSnapshotTestAssertions = this.getFailedSnapshotTestAssertions(
results,
);
});
}

getUsageInfo(): UsageData | null {
if (this._failedSnapshotTestAssertions?.length) {
return {key: 'i', prompt: 'run failing tests interactively'};
}

return null;
}

private getFailedSnapshotTestAssertions(
results: AggregatedResult,
): Array<AssertionLocation> {
const failedTestPaths: Array<AssertionLocation> = [];

if (
// skip if no failed tests
results.numFailedTests === 0 ||
// skip if missing test results
!results.testResults ||
// skip if unmatched snapshots are present
results.snapshot.unmatched
) {
return failedTestPaths;
}

results.testResults.forEach(testResult => {
testResult.testResults.forEach(result => {
if (result.status === 'failed') {
failedTestPaths.push({
fullName: result.fullName,
path: testResult.testFilePath,
});
}
});
});

return failedTestPaths;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import FailedTestsInteractivePlugin from '../FailedTestsInteractive';

const MockFailedResults = {
snapshot: {},
testResults: [{testResults: [{status: 'failed'}]}],
};

describe('FailtedTestsInteractive', () => {
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(MockFailedResults);

expect(activateablePlugin.getUsageInfo()).toEqual({
key: 'i',
prompt: 'run failing tests interactively',
});
});
});
2 changes: 2 additions & 0 deletions packages/jest-core/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
filterInteractivePlugins,
getSortedUsageRows,
} from './lib/watchPluginsHelpers';
import FailedTestsInteractivePlugin from './plugins/FailedTestsInteractive';
import QuitPlugin from './plugins/Quit';
import TestNamePatternPlugin from './plugins/TestNamePattern';
import TestPathPatternPlugin from './plugins/TestPathPattern';
Expand All @@ -58,6 +59,7 @@ const {print: preRunMessagePrint} = preRunMessage;
let hasExitListener = false;

const INTERNAL_PLUGINS = [
FailedTestsInteractivePlugin,
TestPathPatternPlugin,
TestNamePatternPlugin,
UpdateSnapshotsPlugin,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-watcher/src/BaseWatchPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
WatchPlugin,
} from './types';

class BaseWatchPlugin implements WatchPlugin {
abstract class BaseWatchPlugin implements WatchPlugin {
protected _stdin: NodeJS.ReadStream;
protected _stdout: NodeJS.WriteStream;

Expand Down

0 comments on commit 64da282

Please sign in to comment.