-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff2b65c
commit 64da282
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/jest-core/src/plugins/__tests__/FailedTestsInteractive.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters