|
| 1 | +import type { InspectorNotification } from 'node:inspector' |
| 2 | +import { expect, test, vi } from 'vitest' |
| 3 | +import WebSocket from 'ws' |
| 4 | + |
| 5 | +import { runVitestCli } from '../../test-utils' |
| 6 | + |
| 7 | +type Message = Partial<InspectorNotification<any>> |
| 8 | + |
| 9 | +const IS_PLAYWRIGHT_CHROMIUM = process.env.BROWSER === 'chromium' && process.env.PROVIDER === 'playwright' |
| 10 | +const REMOTE_DEBUG_URL = '127.0.0.1:9123' |
| 11 | + |
| 12 | +test.runIf(IS_PLAYWRIGHT_CHROMIUM || !process.env.CI)('--inspect-brk stops at test file', async () => { |
| 13 | + const { vitest, waitForClose } = await runVitestCli( |
| 14 | + '--root', |
| 15 | + 'fixtures/inspect', |
| 16 | + '--browser', |
| 17 | + '--no-file-parallelism', |
| 18 | + '--inspect-brk', |
| 19 | + REMOTE_DEBUG_URL, |
| 20 | + ) |
| 21 | + |
| 22 | + await vitest.waitForStdout(`Debugger listening on ws://${REMOTE_DEBUG_URL}`) |
| 23 | + |
| 24 | + const url = await vi.waitFor(() => |
| 25 | + fetch(`http://${REMOTE_DEBUG_URL}/json/list`) |
| 26 | + .then(response => response.json()) |
| 27 | + .then(json => json[0].webSocketDebuggerUrl)) |
| 28 | + |
| 29 | + const { receive, send } = await createChannel(url) |
| 30 | + |
| 31 | + const paused = receive('Debugger.paused') |
| 32 | + send({ method: 'Debugger.enable' }) |
| 33 | + send({ method: 'Runtime.enable' }) |
| 34 | + |
| 35 | + await receive('Runtime.executionContextCreated') |
| 36 | + send({ method: 'Runtime.runIfWaitingForDebugger' }) |
| 37 | + |
| 38 | + const { params } = await paused |
| 39 | + const scriptId = params.callFrames[0].functionLocation.scriptId |
| 40 | + |
| 41 | + // Verify that debugger paused on test file |
| 42 | + const { result } = await send({ method: 'Debugger.getScriptSource', params: { scriptId } }) |
| 43 | + |
| 44 | + expect(result.scriptSource).toContain('test("sum", () => {') |
| 45 | + expect(result.scriptSource).toContain('expect(1 + 1).toBe(2)') |
| 46 | + |
| 47 | + send({ method: 'Debugger.resume' }) |
| 48 | + |
| 49 | + await vitest.waitForStdout('Test Files 1 passed (1)') |
| 50 | + await waitForClose() |
| 51 | +}) |
| 52 | + |
| 53 | +async function createChannel(url: string) { |
| 54 | + const ws = new WebSocket(url) |
| 55 | + |
| 56 | + let id = 1 |
| 57 | + let listeners = [] |
| 58 | + |
| 59 | + ws.onmessage = (message) => { |
| 60 | + const response = JSON.parse(message.data.toString()) |
| 61 | + listeners.forEach(listener => listener(response)) |
| 62 | + } |
| 63 | + |
| 64 | + async function receive(methodOrId?: string | { id: number }): Promise<Message> { |
| 65 | + const { promise, resolve, reject } = withResolvers() |
| 66 | + listeners.push(listener) |
| 67 | + ws.onerror = reject |
| 68 | + |
| 69 | + function listener(message) { |
| 70 | + const filter = typeof methodOrId === 'string' ? { method: methodOrId } : { id: methodOrId.id } |
| 71 | + |
| 72 | + const methodMatch = message.method && message.method === filter.method |
| 73 | + const idMatch = message.id && message.id === filter.id |
| 74 | + |
| 75 | + if (methodMatch || idMatch) { |
| 76 | + resolve(message) |
| 77 | + listeners = listeners.filter(l => l !== listener) |
| 78 | + ws.onerror = undefined |
| 79 | + } |
| 80 | + else if (!filter.id && !filter.method) { |
| 81 | + resolve(message) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return promise |
| 86 | + } |
| 87 | + |
| 88 | + async function send(message: Message): Promise<any> { |
| 89 | + const currentId = id++ |
| 90 | + const json = JSON.stringify({ ...message, id: currentId }) |
| 91 | + |
| 92 | + const receiver = receive({ id: currentId }) |
| 93 | + ws.send(json) |
| 94 | + |
| 95 | + return receiver |
| 96 | + } |
| 97 | + |
| 98 | + await new Promise((resolve, reject) => { |
| 99 | + ws.onerror = reject |
| 100 | + ws.on('open', resolve) |
| 101 | + }) |
| 102 | + |
| 103 | + return { receive, send } |
| 104 | +} |
| 105 | + |
| 106 | +function withResolvers() { |
| 107 | + let reject: (error: unknown) => void |
| 108 | + let resolve: (response: Message) => void |
| 109 | + |
| 110 | + const promise: Promise<Message> = new Promise((...args) => { |
| 111 | + [resolve, reject] = args |
| 112 | + }) |
| 113 | + |
| 114 | + return { promise, resolve, reject } |
| 115 | +} |
0 commit comments