MCPcopy Create free account
hub / github.com/vitest-dev/vitest / parseStacktrace

Function parseStacktrace

packages/utils/src/source-map.ts:229–297  ·  view source on GitHub ↗
(
  stack: string,
  options: StackTraceParserOptions = {},
)

Source from the content-addressed store, hash-verified

227}
228
229export function parseStacktrace(
230 stack: string,
231 options: StackTraceParserOptions = {},
232): ParsedStack[] {
233 const { ignoreStackEntries = stackIgnorePatterns } = options
234 let stacks = !CHROME_IE_STACK_REGEXP.test(stack)
235 ? parseFFOrSafariStackTrace(stack)
236 : parseV8Stacktrace(stack)
237
238 // remove vi.defineHelper's internal stacks
239 const helperIndex = stacks.findLastIndex(s =>
240 // this covers cases such as
241 // "__VITEST_HELPER__"
242 // "__VITEST_HELPER__ [as <object method name>]"
243 // "async*__VITEST_HELPER__" (firefox)
244 // "async __VITEST_HELPER__" (webkit)
245 s.method.includes('__VITEST_HELPER__'),
246 )
247 if (helperIndex >= 0) {
248 stacks = stacks.slice(helperIndex + 1)
249 }
250
251 return stacks.map((stack) => {
252 if (options.getUrlId) {
253 stack.file = options.getUrlId(stack.file)
254 }
255
256 const map = options.getSourceMap?.(stack.file) as
257 | SourceMapLike
258 | null
259 | undefined
260 if (!map || typeof map !== 'object' || !map.version) {
261 return shouldFilter(ignoreStackEntries, stack.file) ? null : stack
262 }
263
264 const traceMap = new DecodedMap(map, stack.file)
265 if (stack.line <= 0 || stack.column <= 0) {
266 return stack
267 }
268 const position = getOriginalPosition(traceMap, {
269 line: stack.line,
270 // stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed
271 column: stack.column - 1,
272 })
273 if (!position) {
274 return stack
275 }
276
277 const { line, column, source, name } = position
278 let file = source || stack.file
279 if (/\/\w:\//.test(file)) {
280 file = file.slice(1)
281 }
282
283 if (shouldFilter(ignoreStackEntries, file)) {
284 return null
285 }
286

Callers 8

initiateRunnerFunction · 0.90
parseStacktraceMethod · 0.90
parseErrorFunction · 0.90
onFilterStackTraceFunction · 0.90
findTestFileStackTraceFunction · 0.90
onUserConsoleLogFunction · 0.90
printLeaksSummaryFunction · 0.90
parseErrorStacktraceFunction · 0.85

Calls 7

parseV8StacktraceFunction · 0.85
shouldFilterFunction · 0.85
getOriginalPositionFunction · 0.85
getSourceMapMethod · 0.80
filterMethod · 0.65
testMethod · 0.45

Tested by

no test coverage detected