Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/browser-playwright/src/commands/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function parseLocation(context: BrowserCommandContext, stack?: string): ParsedSt
if (!stack) {
return
}
const parsedStacks = context.project.browser!.parseStacktrace(stack)
const parsedStacks = context.project.browser!.parseStacktrace(stack, { firstOnly: true })
return parsedStacks[0]
}

Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/node/commands/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const _recordBrowserTrace: BrowserCommand<
if (!entry.stack || entry.location) {
return entry
}
const stacks = project.browser!.parseStacktrace(entry.stack)
const stacks = project.browser!.parseStacktrace(entry.stack, { firstOnly: true })
if (stacks[0]) {
entry.location = stacks[0]
}
Expand Down
95 changes: 56 additions & 39 deletions packages/utils/src/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface StackTraceParserOptions {
getSourceMap?: (file: string) => unknown
getUrlId?: (id: string) => string
frameFilter?: (error: TestError, frame: ParsedStack) => boolean | void
firstOnly?: boolean
}

const CHROME_IE_STACK_REGEXP = /^\s*at .*(?:\S:\d+|\(native\))/m
Expand Down Expand Up @@ -248,51 +249,67 @@ export function parseStacktrace(
stacks = stacks.slice(helperIndex + 1)
}

return stacks
.map((stack) => {
if (options.getUrlId) {
stack.file = options.getUrlId(stack.file)
if (options.firstOnly) {
for (const stack of stacks) {
const mapped = mapStackFrame(stack, options, ignoreStackEntries)
if (mapped) {
return [mapped]
}
}
return []
}

const map = options.getSourceMap?.(stack.file) as SourceMapLike | null | undefined
if (!map || typeof map !== 'object' || !map.version) {
return shouldFilter(ignoreStackEntries, stack.file) ? null : stack
}
return stacks
.map((stack) => mapStackFrame(stack, options, ignoreStackEntries))
.filter((s) => s != null)
}

const traceMap = new DecodedMap(map, stack.file)
if (stack.line <= 0 || stack.column <= 0) {
return stack
}
const position = getOriginalPosition(traceMap, {
line: stack.line,
// stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed
column: stack.column - 1,
})
if (!position) {
return stack
}
function mapStackFrame(

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just extracted a loop. See with "hide whitespace" https://github.com/vitest-dev/vitest/pull/11613/changes?w=1

stack: ParsedStack,
options: StackTraceParserOptions,
ignoreStackEntries: (string | RegExp)[],
): ParsedStack | null {
if (options.getUrlId) {
stack.file = options.getUrlId(stack.file)
}

const { line, column, source, name } = position
let file = source || stack.file
if (/\/\w:\//.test(file)) {
file = file.slice(1)
}
const map = options.getSourceMap?.(stack.file) as SourceMapLike | null | undefined
if (!map || typeof map !== 'object' || !map.version) {
return shouldFilter(ignoreStackEntries, stack.file) ? null : stack
}

if (shouldFilter(ignoreStackEntries, file)) {
return null
}
const traceMap = new DecodedMap(map, stack.file)
if (stack.line <= 0 || stack.column <= 0) {
return stack
}
const position = getOriginalPosition(traceMap, {
line: stack.line,
// stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed
column: stack.column - 1,
})
if (!position) {
return stack
}

if (line != null && column != null) {
return {
line,
column: column + 1,
file,
method: name || stack.method,
}
}
return stack
})
.filter((s) => s != null)
const { line, column, source, name } = position
let file = source || stack.file
if (/\/\w:\//.test(file)) {
file = file.slice(1)
}

if (shouldFilter(ignoreStackEntries, file)) {
return null
}

if (line != null && column != null) {
return {
line,
column: column + 1,
file,
method: name || stack.method,
}
}
return stack
}

function shouldFilter(ignoreStackEntries: (string | RegExp)[], file: string): boolean {
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/types/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ export interface ProjectBrowser {
close: () => Promise<void>
initBrowserProvider: (project: TestProject) => Promise<void>
closeBrowserProvider: (options?: BrowserProviderCloseOptions) => Promise<void>
parseStacktrace: (stack: string) => ParsedStack[]
parseStacktrace: (stack: string, options?: StackTraceParserOptions) => ParsedStack[]
parseErrorStacktrace: (error: TestError, options?: StackTraceParserOptions) => ParsedStack[]
registerCommand: <K extends keyof BrowserCommands>(
name: K,
Expand Down