-
Notifications
You must be signed in to change notification settings - Fork 32
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
Report on test coverage #675
Draft
72636c
wants to merge
9
commits into
main
Choose a base branch
from
test-coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+88
−1
Draft
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a130b4c
Report on test coverage
72636c 48b4127
Merge branch 'master' into test-coverage
72636c b991adf
Merge branch 'master' into test-coverage
72636c c6478d2
Remove Buildkite for now
72636c ece8f09
Merge branch 'master' into test-coverage
72636c 7aec95b
Merge branch 'master' into test-coverage
72636c 1c1c70e
Merge branch 'master' into test-coverage
72636c 1fbb07c
Merge branch 'master' into test-coverage
72636c 39441a9
Merge branch 'master' into test-coverage
72636c 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 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,79 @@ | ||
import type { AggregatedResult } from '@jest/test-result'; | ||
import istanbulReport from 'istanbul-lib-report'; | ||
import istanbulReports from 'istanbul-reports'; | ||
|
||
export const createWriteOverride = () => { | ||
const chunks: Uint8Array[] = []; | ||
|
||
const output = () => Buffer.concat(chunks).toString('utf8'); | ||
|
||
const write = ( | ||
buffer: Uint8Array | string, | ||
encodingOrCb?: BufferEncoding | ((err?: Error) => void), | ||
cb?: (err?: Error) => void, | ||
) => { | ||
chunks.push( | ||
typeof buffer === 'string' | ||
? Buffer.from( | ||
buffer, | ||
typeof encodingOrCb === 'string' ? encodingOrCb : undefined, | ||
) | ||
: buffer, | ||
); | ||
|
||
if (typeof encodingOrCb === 'function') { | ||
encodingOrCb(); | ||
} else { | ||
cb?.(); | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
return { | ||
output, | ||
write, | ||
}; | ||
}; | ||
|
||
/** | ||
* Renders out test coverage using the Istanbul `text` reporter. | ||
* | ||
* This is unfortunately hacky in a couple of ways; | ||
* | ||
* 1. Jest does not support custom coverage reporters (facebook/jest#9112), so | ||
* we rely on the default `CoverageReporter` running before us and use the | ||
* `coverageMap` that it places on the aggregated result state. | ||
* | ||
* 2. `istanbul-reports` does not support writing to a custom stream, so we need | ||
* to temporarily override `process.stdout.write` 😱. | ||
* | ||
* {@link https://github.com/facebook/jest/blob/v27.3.1/packages/jest-reporters/src/CoverageReporter.ts#L103} | ||
// | ||
*/ | ||
export const renderCoverageText = ( | ||
coverageMap: AggregatedResult['coverageMap'], | ||
) => { | ||
if (!coverageMap) { | ||
// Coverage was not stored on the aggregated result by `CoverageReporter`. | ||
// Maybe `collectCoverage` / `--coverage` was not specified. | ||
return; | ||
} | ||
|
||
const reportContext = istanbulReport.createContext({ | ||
coverageMap, | ||
}); | ||
|
||
const overrideWrite = createWriteOverride(); | ||
|
||
const originalWrite = process.stdout.write.bind(this); | ||
process.stdout.write = overrideWrite.write; | ||
|
||
try { | ||
istanbulReports.create('text').execute(reportContext); | ||
} finally { | ||
process.stdout.write = originalWrite; | ||
} | ||
|
||
return overrideWrite.output(); | ||
}; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@samchungy how do you feel about having this reporter support both Buildkite and GitHub 😬
If we want coverage integrations across both, it may be wasteful to define two reporters and to run
renderCoverageText
twice.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love it but
renderCoverageText
is kinda costly I guess. Even the loop processing this I guess.Do reporters run in series? I wonder if we could store it in memory? 🤔
If we do this -> might want to rename the reporter? maybe
reporters/annotate/index.ts
?