Skip to content

Commit 42c87d7

Browse files
authored
fix: enable Browser Debug when using project configurations (#702)
1 parent 1eaddb5 commit 42c87d7

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

packages/extension/src/debug.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,13 @@ export async function debugTests(
198198
},
199199
).then(
200200
(fullfilled) => {
201+
log.info('[DEBUG] Browser debugger started')
201202
metadata.rpc.onBrowserDebug(fullfilled).catch(() => {})
202203
if (fullfilled) {
203-
log.info('[DEBUG] Secondary debug launch config started')
204+
log.info('[DEBUG] Browser debugger attached')
204205
}
205206
else {
206-
log.error('[DEBUG] Secondary debug launch config failed')
207+
log.error('[DEBUG] Browser debugger failed to attach')
207208
}
208209
},
209210
(error) => {

packages/worker/src/index.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export async function initVitest(
1616
const meta = data.meta
1717
const reporter = new VSCodeReporter({
1818
setupFilePaths: meta.setupFilePaths,
19+
debug: data.debug,
1920
})
2021

2122
let stdout: Writable | undefined
@@ -78,6 +79,9 @@ export async function initVitest(
7879
meta.pnpLoader,
7980
]
8081
: [],
82+
inspect: typeof data.debug === 'object'
83+
? `${data.debug.host}:${data.debug.port}`
84+
: undefined,
8185
}
8286
const vitest = await vitestModule.createVitest(
8387
'test',
@@ -122,19 +126,6 @@ export async function initVitest(
122126
} as any,
123127
}
124128
},
125-
configureVitest(context) {
126-
const options = context.project.config.browser
127-
if (options?.enabled && typeof data.debug === 'object') {
128-
context.project.config.setupFiles.push(meta.setupFilePaths.browserDebug)
129-
context.vitest.config.inspector = {
130-
enabled: true,
131-
port: data.debug.port,
132-
host: data.debug.host,
133-
waitForDebugger: false,
134-
}
135-
context.project.config.inspector = context.vitest.config.inspector
136-
}
137-
},
138129
api: {
139130
vitest: {
140131
experimental: {

packages/worker/src/reporter.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RunnerTaskResultPack, UserConsoleLog } from 'vitest'
2-
import type { VitestWorkerRPC, WorkerInitMetadata } from 'vitest-vscode-shared'
2+
import type { VitestWorkerRPC, WorkerInitMetadata, WorkerRunnerOptions } from 'vitest-vscode-shared'
33
import type {
44
BrowserCommand,
55
Reporter,
@@ -15,20 +15,25 @@ import { ExtensionWorker } from './worker'
1515

1616
interface VSCodeReporterOptions {
1717
setupFilePaths: WorkerInitMetadata['setupFilePaths']
18+
debug: WorkerRunnerOptions['debug']
1819
}
1920

2021
export class VSCodeReporter implements Reporter {
2122
public rpc!: VitestWorkerRPC
2223
private vitest!: VitestCore
2324

2425
private setupFilePaths: WorkerInitMetadata['setupFilePaths']
26+
private debug: WorkerRunnerOptions['debug']
2527

2628
constructor(options: VSCodeReporterOptions) {
2729
this.setupFilePaths = options.setupFilePaths
30+
this.debug = options.debug
2831
}
2932

3033
onInit(vitest: VitestCore) {
3134
this.vitest = vitest
35+
this.configureBrowserDebugging(vitest)
36+
3237
vitest.projects.forEach((project) => {
3338
this.ensureSetupFileIsAllowed(project.vite.config)
3439
})
@@ -142,6 +147,19 @@ export class VSCodeReporter implements Reporter {
142147
})
143148
}
144149

150+
configureBrowserDebugging(vitest: VitestCore) {
151+
//
152+
// Note: This is too late to enable the inspector itself, but we can still add setup files
153+
//
154+
if (this.debug !== undefined && typeof this.debug === 'object') {
155+
vitest.projects.forEach((project) => {
156+
if (project.config.browser?.enabled) {
157+
project.config.setupFiles.push(this.setupFilePaths.browserDebug)
158+
}
159+
})
160+
}
161+
}
162+
145163
toJSON() {
146164
return {}
147165
}

0 commit comments

Comments
 (0)