-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(browser): support --inspect-brk
#6434
Merged
sheremet-va
merged 1 commit into
vitest-dev:main
from
AriPerkkio:feat/browser-inspect-brk
Sep 8, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 @@ | ||
import { expect, test } from "vitest"; | ||
|
||
test("sum", () => { | ||
expect(1 + 1).toBe(2) | ||
}) |
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,13 @@ | ||
import { defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
server: { port: 5199 }, | ||
test: { | ||
watch: false, | ||
browser: { | ||
provider: "playwright", | ||
name: "chromium", | ||
headless: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import type { InspectorNotification } from 'node:inspector' | ||
import { expect, test, vi } from 'vitest' | ||
import WebSocket from 'ws' | ||
|
||
import { runVitestCli } from '../../test-utils' | ||
|
||
type Message = Partial<InspectorNotification<any>> | ||
|
||
const IS_PLAYWRIGHT_CHROMIUM = process.env.BROWSER === 'chromium' && process.env.PROVIDER === 'playwright' | ||
const REMOTE_DEBUG_URL = '127.0.0.1:9123' | ||
|
||
test.runIf(IS_PLAYWRIGHT_CHROMIUM || !process.env.CI)('--inspect-brk stops at test file', async () => { | ||
const { vitest, waitForClose } = await runVitestCli( | ||
'--root', | ||
'fixtures/inspect', | ||
'--browser', | ||
'--no-file-parallelism', | ||
'--inspect-brk', | ||
REMOTE_DEBUG_URL, | ||
) | ||
|
||
await vitest.waitForStdout(`Debugger listening on ws://${REMOTE_DEBUG_URL}`) | ||
|
||
const url = await vi.waitFor(() => | ||
fetch(`http://${REMOTE_DEBUG_URL}/json/list`) | ||
.then(response => response.json()) | ||
.then(json => json[0].webSocketDebuggerUrl)) | ||
|
||
const { receive, send } = await createChannel(url) | ||
|
||
const paused = receive('Debugger.paused') | ||
send({ method: 'Debugger.enable' }) | ||
send({ method: 'Runtime.enable' }) | ||
|
||
await receive('Runtime.executionContextCreated') | ||
send({ method: 'Runtime.runIfWaitingForDebugger' }) | ||
|
||
const { params } = await paused | ||
const scriptId = params.callFrames[0].functionLocation.scriptId | ||
|
||
// Verify that debugger paused on test file | ||
const { result } = await send({ method: 'Debugger.getScriptSource', params: { scriptId } }) | ||
|
||
expect(result.scriptSource).toContain('test("sum", () => {') | ||
expect(result.scriptSource).toContain('expect(1 + 1).toBe(2)') | ||
|
||
send({ method: 'Debugger.resume' }) | ||
|
||
await vitest.waitForStdout('Test Files 1 passed (1)') | ||
await waitForClose() | ||
}) | ||
|
||
async function createChannel(url: string) { | ||
const ws = new WebSocket(url) | ||
|
||
let id = 1 | ||
let listeners = [] | ||
|
||
ws.onmessage = (message) => { | ||
const response = JSON.parse(message.data.toString()) | ||
listeners.forEach(listener => listener(response)) | ||
} | ||
|
||
async function receive(methodOrId?: string | { id: number }): Promise<Message> { | ||
const { promise, resolve, reject } = withResolvers() | ||
listeners.push(listener) | ||
ws.onerror = reject | ||
|
||
function listener(message) { | ||
const filter = typeof methodOrId === 'string' ? { method: methodOrId } : { id: methodOrId.id } | ||
|
||
const methodMatch = message.method && message.method === filter.method | ||
const idMatch = message.id && message.id === filter.id | ||
|
||
if (methodMatch || idMatch) { | ||
resolve(message) | ||
listeners = listeners.filter(l => l !== listener) | ||
ws.onerror = undefined | ||
} | ||
else if (!filter.id && !filter.method) { | ||
resolve(message) | ||
} | ||
} | ||
|
||
return promise | ||
} | ||
|
||
async function send(message: Message): Promise<any> { | ||
const currentId = id++ | ||
const json = JSON.stringify({ ...message, id: currentId }) | ||
|
||
const receiver = receive({ id: currentId }) | ||
ws.send(json) | ||
|
||
return receiver | ||
} | ||
|
||
await new Promise((resolve, reject) => { | ||
ws.onerror = reject | ||
ws.on('open', resolve) | ||
}) | ||
|
||
return { receive, send } | ||
} | ||
|
||
function withResolvers() { | ||
let reject: (error: unknown) => void | ||
let resolve: (response: Message) => void | ||
|
||
const promise: Promise<Message> = new Promise((...args) => { | ||
[resolve, reject] = args | ||
}) | ||
|
||
return { promise, resolve, reject } | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The requirement for
--no-file-parallelism
when used with--browser
was removed in #6433 but during this PR I noticed that the flag actually affects browser mode as well. We need it in order to successfully set breakpoint to the first test file.