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
13 changes: 13 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ describe('utils', () => {
it('gets component name', () => {
expect(getComponentName()).toEqual('_callCircusTest')
})

it('returns empty string if error.stack is not supported by the browser', () => {
const customError = new Error('error without stack')
delete customError.stack
expect(getComponentName(customError)).toEqual('')
})

it('returns empty string if error.stack has different format', () => {
const customError = new Error('error with unsupported stack')
customError.stack =
'This is custom implementation of stack: calledThis > calledThat'
expect(getComponentName(customError)).toEqual('')
})
})

describe('getPrinter', () => {
Expand Down
33 changes: 15 additions & 18 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,22 @@ export function getGroupLabel(
return `${typeWrapper}${componentNameWrapper}${timeWrapper}`
}

export function getComponentName(): string {
// Tested in the scope of useLog testing
try {
throw new Error('Getting the stack of error to parse it for component name')
} catch (error) {
/* istanbul ignore next */
if (error instanceof Error && error?.stack) {
const re = /(\w+)@|at (\w+) \(/g

re.exec(error.stack ?? '')
re.exec(error.stack ?? '')
const m = re.exec(error.stack ?? '') ?? []

return String(m[1] || m[2])
}

/* istanbul ignore next */
return '' // will be never reached since getComponentName always throws an instance of Error to parse the stack
export function getComponentName(
error = new Error(
'Getting the stack of error to parse it for component name',
),
): string {
if (!error.stack) {
return ''
}

const re = /(\w+)@|at (\w+) \(/g

re.exec(error.stack)
re.exec(error.stack)
const m = re.exec(error.stack)

return m ? String(m[1] || m[2]) : ''
}

export function getRenderFunctionProps<T>(
Expand Down