-
-
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(browser): support v8 coverage (#6273)
- Loading branch information
1 parent
198a3e6
commit 34199bd
Showing
28 changed files
with
428 additions
and
202 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
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { cdp } from '@vitest/browser/context' | ||
import type { V8CoverageProvider } from './provider' | ||
import { loadProvider } from './load-provider' | ||
|
||
const session = cdp() | ||
|
||
type ScriptCoverage = Awaited<ReturnType<typeof session.send<'Profiler.takePreciseCoverage'>>> | ||
|
||
export default { | ||
async startCoverage() { | ||
await session.send('Profiler.enable') | ||
await session.send('Profiler.startPreciseCoverage', { | ||
callCount: true, | ||
detailed: true, | ||
}) | ||
}, | ||
|
||
async takeCoverage(): Promise<{ result: any[] }> { | ||
const coverage = await session.send('Profiler.takePreciseCoverage') | ||
const result: typeof coverage.result = [] | ||
|
||
// Reduce amount of data sent over rpc by doing some early result filtering | ||
for (const entry of coverage.result) { | ||
if (filterResult(entry)) { | ||
result.push({ | ||
...entry, | ||
url: decodeURIComponent(entry.url.replace(window.location.origin, '')), | ||
}) | ||
} | ||
} | ||
|
||
return { result } | ||
}, | ||
|
||
async stopCoverage() { | ||
await session.send('Profiler.stopPreciseCoverage') | ||
await session.send('Profiler.disable') | ||
}, | ||
|
||
async getProvider(): Promise<V8CoverageProvider> { | ||
return loadProvider() | ||
}, | ||
} | ||
|
||
function filterResult(coverage: ScriptCoverage['result'][number]): boolean { | ||
if (!coverage.url.startsWith(window.location.origin)) { | ||
return false | ||
} | ||
|
||
if (coverage.url.includes('/node_modules/')) { | ||
return false | ||
} | ||
|
||
if (coverage.url.includes('__vitest_browser__')) { | ||
return false | ||
} | ||
|
||
if (coverage.url.includes('__vitest__/assets')) { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,58 @@ | ||
import type { Profiler } from 'node:inspector' | ||
import * as coverage from './takeCoverage' | ||
import inspector, { type Profiler } from 'node:inspector' | ||
import { provider } from 'std-env' | ||
import type { V8CoverageProvider } from './provider' | ||
import { loadProvider } from './load-provider' | ||
|
||
const session = new inspector.Session() | ||
|
||
export default { | ||
startCoverage(): void { | ||
return coverage.startCoverage() | ||
session.connect() | ||
session.post('Profiler.enable') | ||
session.post('Profiler.startPreciseCoverage', { | ||
callCount: true, | ||
detailed: true, | ||
}) | ||
}, | ||
|
||
takeCoverage(): Promise<{ result: Profiler.ScriptCoverage[] }> { | ||
return coverage.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 }) | ||
}) | ||
|
||
if (provider === 'stackblitz') { | ||
resolve({ result: [] }) | ||
} | ||
}) | ||
}, | ||
|
||
stopCoverage(): void { | ||
return coverage.stopCoverage() | ||
session.post('Profiler.stopPreciseCoverage') | ||
session.post('Profiler.disable') | ||
session.disconnect() | ||
}, | ||
|
||
async getProvider(): Promise<V8CoverageProvider> { | ||
// to not bundle the provider | ||
const name = './provider.js' | ||
const { V8CoverageProvider } = (await import( | ||
name | ||
)) as typeof import('./provider') | ||
return new V8CoverageProvider() | ||
return loadProvider() | ||
}, | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// to not bundle the provider | ||
const name = './provider.js' | ||
|
||
export async function loadProvider() { | ||
const { V8CoverageProvider } = (await import(/* @vite-ignore */ name)) as typeof import('./provider') | ||
|
||
return new V8CoverageProvider() | ||
} |
Oops, something went wrong.