Skip to content

Commit 2776b43

Browse files
test: also check source code for node:internal related errors (#62542)
* Still check if the origin line thrown nodejs errors could still display source code * Need to separate the different error into different test otherwise the previous error will be preserved Closes NEXT-2597 --------- Co-authored-by: Balázs Orbán <[email protected]>
1 parent 47d2dfd commit 2776b43

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

test/development/acceptance-app/ReactRefreshLogBox.test.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -839,17 +839,17 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox app %s', () => {
839839
await cleanup()
840840
})
841841

842-
test('useless frames are hidden in stack trace', async () => {
842+
test('should hide unrelated frames in stack trace with unknown anonymous calls', async () => {
843843
const { session, browser, cleanup } = await sandbox(
844844
next,
845845
new Map([
846846
[
847847
'app/page.js',
848+
// TODO: repro stringify (<anonymous>)
848849
outdent`
849850
export default function Page() {
850851
const e = new Error("Boom!");
851852
e.stack += \`
852-
// REVIEW: how to reliably test the presence of these stack frames?
853853
at stringify (<anonymous>)
854854
at <unknown> (<anonymous>)
855855
at foo (bar:1:1)\`;
@@ -869,23 +869,37 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox app %s', () => {
869869
expect(texts).not.toContain('<unknown>\n<anonymous>')
870870
expect(texts).toContain('foo\nbar (1:1)')
871871

872-
// Test that node:internal errors should be hidden
872+
await cleanup()
873+
})
873874

874-
next.patchFile(
875-
'app/page.js',
876-
// Node.js will throw an error about the invalid URL since this is a server component
877-
outdent`
878-
export default function Page() {
879-
new URL("/", "invalid");
880-
}`
875+
test('should hide unrelated frames in stack trace with node:internal calls', async () => {
876+
const { session, browser, cleanup } = await sandbox(
877+
next,
878+
new Map([
879+
[
880+
'app/page.js',
881+
// Node.js will throw an error about the invalid URL since this is a server component
882+
outdent`
883+
export default function Page() {
884+
new URL("/", "invalid");
885+
}`,
886+
],
887+
])
881888
)
882889

883890
expect(await session.hasRedbox()).toBe(true)
884891
await expandCallStack(browser)
885-
callStackFrames = await browser.elementsByCss(
892+
893+
// Should still show the errored line in source code
894+
const source = await session.getRedboxSource()
895+
expect(source).toContain('app/page.js')
896+
expect(source).toContain(`new URL("/", "invalid")`)
897+
898+
await expandCallStack(browser)
899+
const callStackFrames = await browser.elementsByCss(
886900
'[data-nextjs-call-stack-frame]'
887901
)
888-
texts = await Promise.all(callStackFrames.map((f) => f.innerText()))
902+
const texts = await Promise.all(callStackFrames.map((f) => f.innerText()))
889903

890904
expect(texts.filter((t) => t.includes('node:internal'))).toHaveLength(0)
891905

test/development/acceptance/ReactRefreshLogBox.test.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -784,17 +784,17 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
784784
await cleanup()
785785
})
786786

787-
test('useless frames are hidden in stack trace for pages error', async () => {
787+
test('should hide unrelated frames in stack trace with unknown anonymous calls', async () => {
788788
const { session, browser, cleanup } = await sandbox(
789789
next,
790790
new Map([
791791
[
792792
'pages/index.js',
793+
// TODO: repro stringify (<anonymous>)
793794
outdent`
794795
export default function Page() {
795796
const e = new Error("Client error!");
796797
e.stack += \`
797-
// REVIEW: how to reliably test the presence of these stack frames?
798798
at stringify (<anonymous>)
799799
at <unknown> (<anonymous>)
800800
at foo (bar:1:1)\`;
@@ -814,26 +814,38 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
814814
expect(texts).not.toContain('<unknown>\n<anonymous>')
815815
expect(texts).toContain('foo\nbar (1:1)')
816816

817-
// Test that node:internal errors should be hidden
817+
await cleanup()
818+
})
818819

819-
next.patchFile(
820-
'pages/index.js',
821-
// Node.js will throw an error about the invalid URL since it happens server-side
822-
outdent`
820+
test('should hide unrelated frames in stack trace with node:internal calls', async () => {
821+
const { session, browser, cleanup } = await sandbox(
822+
next,
823+
new Map([
824+
[
825+
'pages/index.js',
826+
// Node.js will throw an error about the invalid URL since it happens server-side
827+
outdent`
823828
export default function Page() {}
824829
825830
export function getServerSideProps() {
826831
new URL("/", "invalid");
827832
return { props: {} };
828-
}`
833+
}`,
834+
],
835+
])
829836
)
830-
831837
expect(await session.hasRedbox()).toBe(true)
832838
await expandCallStack(browser)
833-
callStackFrames = await browser.elementsByCss(
839+
840+
// Should still show the errored line in source code
841+
const source = await session.getRedboxSource()
842+
expect(source).toContain('pages/index.js')
843+
expect(source).toContain(`new URL("/", "invalid")`)
844+
845+
const callStackFrames = await browser.elementsByCss(
834846
'[data-nextjs-call-stack-frame]'
835847
)
836-
texts = await Promise.all(callStackFrames.map((f) => f.innerText()))
848+
const texts = await Promise.all(callStackFrames.map((f) => f.innerText()))
837849

838850
expect(texts.filter((t) => t.includes('node:internal'))).toHaveLength(0)
839851

0 commit comments

Comments
 (0)