From d9a375782818d43491b64ac119e6fbf0af3c3507 Mon Sep 17 00:00:00 2001 From: Miras Kenzhegaliyev Date: Wed, 21 Dec 2022 21:55:11 +0200 Subject: [PATCH 1/2] Refactor getComponentName function There is no need to throw Error object just to get error stack, it's enough to just create Error object and use it. Additionally, Error.prototype.stack is non-standard so there might be a situation where stack is undefined, so I added test for this case. --- src/utils.test.ts | 6 ++++++ src/utils.ts | 33 +++++++++++++++------------------ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/utils.test.ts b/src/utils.test.ts index 2a13e4a..43447c6 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -23,6 +23,12 @@ describe('utils', () => { it('gets component name', () => { expect(getComponentName()).toEqual('_callCircusTest') }) + + it('returns empty string if error.stack is not supported', () => { + const customError = new Error('error without stack') + delete customError.stack + expect(getComponentName(customError)).toEqual('') + }) }) describe('getPrinter', () => { diff --git a/src/utils.ts b/src/utils.ts index c5b37d9..1a6c071 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 String(m[1] || m[2]) } export function getRenderFunctionProps( From f432a2d355a8394dc1232f5c5a4a452f3f314ff5 Mon Sep 17 00:00:00 2001 From: Miras Kenzhegaliyev Date: Fri, 23 Dec 2022 11:37:37 +0200 Subject: [PATCH 2/2] Add test when stack format is not supported - Added test where error.stack has some format that getComponentName doesn't expect - Changed the return value from "undefined" to "" in such situations --- src/utils.test.ts | 9 ++++++++- src/utils.ts | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/utils.test.ts b/src/utils.test.ts index 43447c6..12adead 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -24,11 +24,18 @@ describe('utils', () => { expect(getComponentName()).toEqual('_callCircusTest') }) - it('returns empty string if error.stack is not supported', () => { + 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', () => { diff --git a/src/utils.ts b/src/utils.ts index 1a6c071..9570abf 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -78,9 +78,9 @@ export function getComponentName( re.exec(error.stack) re.exec(error.stack) - const m = re.exec(error.stack) ?? [] + const m = re.exec(error.stack) - return String(m[1] || m[2]) + return m ? String(m[1] || m[2]) : '' } export function getRenderFunctionProps(