-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: coverage-c8 to use V8 profiler directly instead of `NODE_V8_CO…
…VERAGE` (#2786)
- Loading branch information
1 parent
489b247
commit 095c639
Showing
11 changed files
with
128 additions
and
52 deletions.
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
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
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 |
---|---|---|
@@ -1,10 +1,46 @@ | ||
import v8 from 'v8' | ||
/* | ||
* For details about the Profiler.* messages see https://chromedevtools.github.io/devtools-protocol/v8/Profiler/ | ||
*/ | ||
|
||
// Flush coverage to disk | ||
import inspector from 'node:inspector' | ||
import type { Profiler } from 'node:inspector' | ||
|
||
export function takeCoverage() { | ||
if (v8.takeCoverage == null) | ||
console.warn('[Vitest] takeCoverage is not available in this NodeJs version.\nCoverage could be incomplete. Update to NodeJs 14.18.') | ||
else | ||
v8.takeCoverage() | ||
const session = new inspector.Session() | ||
|
||
export function startCoverage() { | ||
session.connect() | ||
session.post('Profiler.enable') | ||
session.post('Profiler.startPreciseCoverage', { | ||
callCount: true, | ||
detailed: true, | ||
}) | ||
} | ||
|
||
export async function takeCoverage() { | ||
return new Promise((resolve, reject) => { | ||
session.post('Profiler.takePreciseCoverage', async (error, coverage) => { | ||
if (error) | ||
return reject(error) | ||
|
||
// Reduce amount of data sent over rpc by doing some early result filtering | ||
const result = coverage.result.filter(filterResult) | ||
|
||
resolve({ result }) | ||
}) | ||
}) | ||
} | ||
|
||
export function stopCoverage() { | ||
session.post('Profiler.stopPreciseCoverage') | ||
session.post('Profiler.disable') | ||
} | ||
|
||
function filterResult(coverage: Profiler.ScriptCoverage): boolean { | ||
if (!coverage.url.startsWith('file://')) | ||
return false | ||
|
||
if (coverage.url.includes('/node_modules/')) | ||
return false | ||
|
||
return true | ||
} |
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
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
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
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