-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
feat(jest-reporters): pass reporterContext to custom reporter constructors as third argument
#12657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
07a9d43
feat: pass reporterContext to Reporter constructor
mrazauskas 238be92
reporterConstructor
mrazauskas 1a53908
add changelog entry
mrazauskas 50a8241
fix changelog
mrazauskas 6b7f017
rebase
mrazauskas deb6074
keep type reexports
mrazauskas e13963c
sort exports
mrazauskas 4afff9f
expose getSnapshotStatus and getSnapshotSummary
mrazauskas 3a80da7
better TestSchedulerContext type?
mrazauskas aed2772
tweak docs
mrazauskas 1e7f073
Update docs/Configuration.md
mrazauskas bbb8f97
Update docs/Configuration.md
SimenB 59ee2af
fix reporters type
mrazauskas ee0cc73
clean up logic
mrazauskas d1a625f
Merge branch 'fix-reporters-type'
mrazauskas cdb3daf
more test
mrazauskas c331a40
Merge branch 'fix-reporters-type' into feat-reporterContext
mrazauskas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -822,73 +822,62 @@ When using multi-project runner, it's recommended to add a `displayName` for eac | |
|
|
||
| Default: `undefined` | ||
|
|
||
| Use this configuration option to add custom reporters to Jest. A custom reporter is a class that implements `onRunStart`, `onTestStart`, `onTestResult`, `onRunComplete` methods that will be called when any of those events occurs. | ||
|
|
||
| If custom reporters are specified, the default Jest reporters will be overridden. To keep default reporters, `default` can be passed as a module name. | ||
|
|
||
| This will override default reporters: | ||
| Use this configuration option to add reporters to Jest. Optionally, a reporter can be configured by passing options object as a second argument of a tuple: | ||
|
|
||
| ```json | ||
| { | ||
| "reporters": ["<rootDir>/my-custom-reporter.js"] | ||
| "reporters": [ | ||
| "default", | ||
| ["<rootDir>/custom-reporter.js", {"banana": "yes", "pineapple": "no"}] | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| This will use custom reporter in addition to default reporters that Jest provides: | ||
| :::tip | ||
|
|
||
| ```json | ||
| { | ||
| "reporters": ["default", "<rootDir>/my-custom-reporter.js"] | ||
| } | ||
| ``` | ||
| Take a look at a list of [awesome reporters](https://github.com/jest-community/awesome-jest#reporters) from Awesome Jest. | ||
|
|
||
| Additionally, custom reporters can be configured by passing an `options` object as a second argument: | ||
| If custom reporters are specified, the default Jest reporters will be overridden. If you wish to keep them, `'default'` can be passed as a module name: | ||
|
|
||
| ```json | ||
| { | ||
| "reporters": [ | ||
| "default", | ||
| ["<rootDir>/my-custom-reporter.js", {"banana": "yes", "pineapple": "no"}] | ||
| ["jest-junit", {"outputName": "junit-report.xml", "suiteName": "some-name"}] | ||
|
||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| Custom reporter modules must define a class that takes a `GlobalConfig` and reporter options as constructor arguments: | ||
| ::: | ||
|
|
||
| Example reporter: | ||
| Custom reporter module must export a class that takes `globalConfig`, `reporterConfig` and `reporterContext` as constructor arguments and implements at least `onRunComplete()` method (for the full list of methods and argument types see `Reporter` interface in [packages/jest-reporters/src/types.ts](https://github.com/facebook/jest/blob/main/packages/jest-reporters/src/types.ts)): | ||
mrazauskas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```js title="my-custom-reporter.js" | ||
| class MyCustomReporter { | ||
| constructor(globalConfig, options) { | ||
| ```js title="custom-reporter.js" | ||
| class CustomReporter { | ||
| constructor(globalConfig, reporterOptions, reporterContext) { | ||
| this._globalConfig = globalConfig; | ||
| this._options = options; | ||
| this._options = reporterOptions; | ||
| this._context = reporterContext; | ||
| } | ||
|
|
||
| onRunComplete(contexts, results) { | ||
| onRunComplete(testContexts, results) { | ||
| console.log('Custom reporter output:'); | ||
| console.log('GlobalConfig: ', this._globalConfig); | ||
| console.log('Options: ', this._options); | ||
| console.log('Context: ', this._context); | ||
| } | ||
| } | ||
|
|
||
| module.exports = MyCustomReporter; | ||
| // or export default MyCustomReporter; | ||
| ``` | ||
|
|
||
| Custom reporters can also force Jest to exit with non-0 code by returning an Error from `getLastError()` methods | ||
|
|
||
| ```js | ||
| class MyCustomReporter { | ||
| // ... | ||
| // Optionally, reporters can force Jest to exit with non zero code by returning | ||
| // an `Error` from `getLastError()` method. | ||
| getLastError() { | ||
| if (this._shouldFail) { | ||
| return new Error('my-custom-reporter.js reported an error'); | ||
| return new Error('Custom error reported!'); | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| For the full list of methods and argument types see `Reporter` interface in [packages/jest-reporters/src/types.ts](https://github.com/facebook/jest/blob/main/packages/jest-reporters/src/types.ts) | ||
| module.exports = CustomReporter; | ||
| ``` | ||
|
|
||
| ### `resetMocks` \[boolean] | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.