File size: 5,067 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { nextTestSetup } from 'e2e-utils'
import { assertNoRedbox, retry } from 'next-test-utils'

// These stacks are not sourcemapped and therefore not ignore-listed.
// Feel free to update internal frames in assertions.
function normalizeBrowserConsoleStackTrace(trace: unknown) {
  if (typeof trace !== 'string') {
    return trace
  }
  return (
    trace
      // Removes React's internals i.e. incomplete ignore-listing
      .split(
        /at (react-stack-bottom-frame|Object\.react_stack_bottom_frame).*/m
      )[0]
      // Remove the location `()` part in every line of stack trace;
      .replace(/\(.*\)/g, '')
      // Remove the leading spaces in every line of stack trace;
      .replace(/^\s+/gm, '')
      .trim()
  )
}

describe('app-dir - owner-stack', () => {
  const { isTurbopack, next } = nextTestSetup({
    files: __dirname,
  })

  it('should log stitched error for browser uncaught errors', async () => {
    let errorStack: string | undefined
    const browser = await next.browser('/browser/uncaught', {
      beforePageLoad: (page) => {
        page.on('pageerror', (error: unknown) => {
          errorStack = (error as any).stack
        })
      },
    })

    if (!isTurbopack) {
      // Wait for Redbox to settle.
      // TODO: Don't reload when landing on a faulty page.
      // The issue may be that we receive an HMR update at all on landing.
      // This is flaky. Sometimes we miss that the reload happened.
      await retry(
        async () => {
          const logs = await browser.log()
          expect(logs).toEqual(
            expect.arrayContaining([
              expect.objectContaining({
                message:
                  '[Fast Refresh] performing full reload because your application had an unrecoverable error',
              }),
            ])
          )
        },
        1000,
        200
      ).catch(() => {})
    }

    await expect(browser).toDisplayRedbox(`
     {
       "description": "browser error",
       "environmentLabel": null,
       "label": "Runtime Error",
       "source": "app/browser/uncaught/page.js (5:11) @ useThrowError
     > 5 |     throw new Error('browser error')
         |           ^",
       "stack": [
         "useThrowError app/browser/uncaught/page.js (5:11)",
         "useErrorHook app/browser/uncaught/page.js (10:3)",
         "Page app/browser/uncaught/page.js (14:3)",
       ],
     }
    `)

    expect(normalizeBrowserConsoleStackTrace(errorStack))
      .toMatchInlineSnapshot(`
     "Error: browser error
     at useThrowError 
     at useErrorHook 
     at Page"
    `)
  })

  it('should log stitched error for browser caught errors', async () => {
    const browser = await next.browser('/browser/caught')

    await assertNoRedbox(browser)

    const logs = await browser.log()
    const errorLog = logs.find((log) => {
      return log.message.includes('Error: browser error')
    }).message

    await expect(browser).toDisplayCollapsedRedbox(`
     {
       "description": "browser error",
       "environmentLabel": null,
       "label": "Runtime Error",
       "source": "app/browser/caught/page.js (34:11) @ useThrowError
     > 34 |     throw new Error('browser error')
          |           ^",
       "stack": [
         "useThrowError app/browser/caught/page.js (34:11)",
         "useErrorHook app/browser/caught/page.js (39:3)",
         "Thrower app/browser/caught/page.js (29:3)",
         "Inner app/browser/caught/page.js (23:7)",
         "Page app/browser/caught/page.js (43:10)",
       ],
     }
    `)

    expect(normalizeBrowserConsoleStackTrace(errorLog)).toMatchInlineSnapshot(`
     "%o
     %s Error: browser error
     at useThrowError 
     at useErrorHook 
     at Thrower"
    `)
  })

  it('should log stitched error for SSR errors', async () => {
    let errorStack: string | undefined
    const browser = await next.browser('/ssr', {
      beforePageLoad: (page) => {
        page.on('pageerror', (error: unknown) => {
          errorStack = (error as any).stack
        })
      },
    })

    await expect(browser).toDisplayRedbox(`
     {
       "description": "ssr error",
       "environmentLabel": null,
       "label": "Runtime Error",
       "source": "app/ssr/page.js (4:9) @ useThrowError
     > 4 |   throw new Error('ssr error')
         |         ^",
       "stack": [
         "useThrowError app/ssr/page.js (4:9)",
         "useErrorHook app/ssr/page.js (8:3)",
         "Page app/ssr/page.js (12:3)",
       ],
     }
    `)

    expect(normalizeBrowserConsoleStackTrace(errorStack))
      .toMatchInlineSnapshot(`
     "Error: ssr error
     at useThrowError 
     at useErrorHook 
     at Page"
    `)
  })

  it('should capture unhandled promise rejections', async () => {
    const browser = await next.browser('/browser/reject-promise')

    await expect(browser).toDisplayCollapsedRedbox(`
     {
       "description": "string in rejected promise",
       "environmentLabel": null,
       "label": "Runtime Error",
       "source": null,
       "stack": [],
     }
    `)
  })
})