1- import type { File , Task , TaskResultPack , VitestRunner } from '@vitest/runner'
1+ import type { File , Suite , Task , TaskResultPack , VitestRunner } from '@vitest/runner'
22import type { ResolvedConfig , WorkerGlobalState } from 'vitest'
33import type { VitestExecutor } from 'vitest/execute'
4+ import { NodeBenchmarkRunner , VitestTestRunner } from 'vitest/runners'
5+ import { loadDiffConfig , loadSnapshotSerializers , takeCoverageInsideWorker } from 'vitest/browser'
6+ import { TraceMap , originalPositionFor } from 'vitest/utils'
47import { importId } from '../utils'
58import { VitestBrowserSnapshotEnvironment } from './snapshot'
69import { rpc } from './rpc'
@@ -28,6 +31,7 @@ export function createBrowserRunner(
2831 return class BrowserTestRunner extends runnerClass implements VitestRunner {
2932 public config : ResolvedConfig
3033 hashMap = browserHashMap
34+ public sourceMapCache = new Map < string , any > ( )
3135
3236 constructor ( options : BrowserRunnerOptions ) {
3337 super ( options . config )
@@ -48,6 +52,22 @@ export function createBrowserRunner(
4852 }
4953 }
5054
55+ onBeforeRunSuite = async ( suite : Suite | File ) => {
56+ await Promise . all ( [
57+ super . onBeforeRunSuite ?.( suite ) ,
58+ ( async ( ) => {
59+ if ( 'filepath' in suite ) {
60+ const map = await rpc ( ) . getBrowserFileSourceMap ( suite . filepath )
61+ this . sourceMapCache . set ( suite . filepath , map )
62+ const snapshotEnvironment = this . config . snapshotOptions . snapshotEnvironment
63+ if ( snapshotEnvironment instanceof VitestBrowserSnapshotEnvironment ) {
64+ snapshotEnvironment . addSourceMap ( suite . filepath , map )
65+ }
66+ }
67+ } ) ( ) ,
68+ ] )
69+ }
70+
5171 onAfterRunFiles = async ( files : File [ ] ) => {
5272 const [ coverage ] = await Promise . all ( [
5373 coverageModule ?. takeCoverage ?.( ) ,
@@ -75,7 +95,7 @@ export function createBrowserRunner(
7595
7696 if ( this . config . includeTaskLocation ) {
7797 try {
78- await updateFilesLocations ( files )
98+ await updateFilesLocations ( files , this . sourceMapCache )
7999 }
80100 catch ( _ ) { }
81101 }
@@ -112,13 +132,6 @@ export async function initiateRunner(
112132 if ( cachedRunner ) {
113133 return cachedRunner
114134 }
115- const [
116- { VitestTestRunner, NodeBenchmarkRunner } ,
117- { takeCoverageInsideWorker, loadDiffConfig, loadSnapshotSerializers } ,
118- ] = await Promise . all ( [
119- importId ( 'vitest/runners' ) as Promise < typeof import ( 'vitest/runners' ) > ,
120- importId ( 'vitest/browser' ) as Promise < typeof import ( 'vitest/browser' ) > ,
121- ] )
122135 const runnerClass
123136 = config . mode === 'test' ? VitestTestRunner : NodeBenchmarkRunner
124137 const BrowserRunner = createBrowserRunner ( runnerClass , mocker , state , {
@@ -141,14 +154,9 @@ export async function initiateRunner(
141154 return runner
142155}
143156
144- async function updateFilesLocations ( files : File [ ] ) {
145- const { loadSourceMapUtils } = ( await importId (
146- 'vitest/utils' ,
147- ) ) as typeof import ( 'vitest/utils' )
148- const { TraceMap, originalPositionFor } = await loadSourceMapUtils ( )
149-
157+ async function updateFilesLocations ( files : File [ ] , sourceMaps : Map < string , any > ) {
150158 const promises = files . map ( async ( file ) => {
151- const result = await rpc ( ) . getBrowserFileSourceMap ( file . filepath )
159+ const result = sourceMaps . get ( file . filepath ) || await rpc ( ) . getBrowserFileSourceMap ( file . filepath )
152160 if ( ! result ) {
153161 return null
154162 }
0 commit comments