MCPcopy
hub / github.com/vitest-dev/vitest / formatTitle

Function formatTitle

packages/runner/src/suite.ts:997–1057  ·  view source on GitHub ↗
(template: string, items: any[], idx: number)

Source from the content-addressed store, hash-verified

995}
996
997function formatTitle(template: string, items: any[], idx: number) {
998 if (template.includes('%#') || template.includes('%$')) {
999 // '%#' match index of the test case
1000 template = template
1001 .replace(/%%/g, '__vitest_escaped_%__')
1002 .replace(/%#/g, `${idx}`)
1003 .replace(/%\$/g, `${idx + 1}`)
1004 .replace(/__vitest_escaped_%__/g, '%%')
1005 }
1006 const count = template.split('%').length - 1
1007
1008 if (template.includes('%f')) {
1009 const placeholders = template.match(/%f/g) || []
1010 placeholders.forEach((_, i) => {
1011 if (isNegativeNaN(items[i]) || Object.is(items[i], -0)) {
1012 // Replace the i-th occurrence of '%f' with '-%f'
1013 let occurrence = 0
1014 template = template.replace(/%f/g, (match) => {
1015 occurrence++
1016 return occurrence === i + 1 ? '-%f' : match
1017 })
1018 }
1019 })
1020 }
1021
1022 const isObjectItem = isObject(items[0])
1023 function formatAttribute(s: string) {
1024 return s.replace(/\$([$\w.]+)/g, (_, key: string) => {
1025 const isArrayKey = /^\d+$/.test(key)
1026 if (!isObjectItem && !isArrayKey) {
1027 return `$${key}`
1028 }
1029 const arrayElement = isArrayKey ? objectAttr(items, key) : undefined
1030 const value = isObjectItem ? objectAttr(items[0], key, arrayElement) : arrayElement
1031 return objDisplay(value, {
1032 truncate: runner?.config?.chaiConfig?.truncateThreshold,
1033 })
1034 })
1035 }
1036
1037 let output = ''
1038 let i = 0
1039 handleRegexMatch(
1040 template,
1041 formatRegExp,
1042 // format "%"
1043 (match) => {
1044 if (i < count) {
1045 output += format(match[0], items[i++])
1046 }
1047 else {
1048 output += match[0]
1049 }
1050 },
1051 // format "$"
1052 (nonMatch) => {
1053 output += formatAttribute(nonMatch)
1054 },

Callers 2

createSuiteFunction · 0.85
createTaskCollectorFunction · 0.85

Calls 6

isNegativeNaNFunction · 0.90
isObjectFunction · 0.90
formatFunction · 0.90
handleRegexMatchFunction · 0.85
formatAttributeFunction · 0.85
matchMethod · 0.45

Tested by

no test coverage detected