Skip to content

Commit f8ab8fc

Browse files
committed
fix unit tests
1 parent a0448ff commit f8ab8fc

File tree

2 files changed

+22
-36
lines changed

2 files changed

+22
-36
lines changed

packages/blitz-next/src/error-boundary-hook.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ test("handleError forwards along async errors", async () => {
7070
//
7171
// React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary."
7272
// `)
73-
expect(consoleError).toHaveBeenCalledTimes(3)
73+
expect(consoleError).toHaveBeenCalledTimes(1)
7474
consoleError.mockClear()
7575

7676
// can recover
@@ -116,7 +116,7 @@ test("can pass an error to useErrorHandler", async () => {
116116
//
117117
// React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary."
118118
// `)
119-
expect(consoleError).toHaveBeenCalledTimes(3)
119+
expect(consoleError).toHaveBeenCalledTimes(1)
120120
consoleError.mockClear()
121121

122122
// can recover

packages/blitz-next/src/error-boundary.test.tsx

+20-34
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ test("standard use-case", () => {
7777
const {unmount} = render(<App />)
7878

7979
userEvent.type(screen.getByRole("textbox", {name: /username/i}), "fail")
80-
81-
const [[actualError], [componentStack]] = consoleError.mock.calls
82-
expect(firstLine(actualError as string)).toMatchInlineSnapshot(
83-
`"Error: Uncaught [Error: 💥 CABOOM 💥]"`,
84-
)
80+
const calls = consoleError.mock.calls[0]
81+
//@ts-expect-error - it's a mock
82+
expect(calls[1]).toMatchInlineSnapshot("[Error: 💥 CABOOM 💥]")
8583
expect(cleanStack(componentStack)).toMatchInlineSnapshot(`
8684
"Error: Uncaught [Error: 💥 CABOOM 💥]
8785
at reportException
@@ -149,7 +147,7 @@ test("fallbackRender prop", () => {
149147
}
150148

151149
const {unmount} = render(<App />)
152-
expect(consoleError).toHaveBeenCalledTimes(3)
150+
expect(consoleError).toHaveBeenCalledTimes(1)
153151
consoleError.mockClear()
154152

155153
// the render prop API allows a single action to reset the app state
@@ -168,42 +166,31 @@ test("simple fallback is supported", () => {
168166
<span>child</span>
169167
</ErrorBoundary>,
170168
)
171-
expect(consoleError).toHaveBeenCalledTimes(3)
169+
expect(consoleError).toHaveBeenCalledTimes(1)
172170
consoleError.mockClear()
173171
expect(screen.getByText(/oh no/i)).to.exist
174172
expect(screen.queryByText(/child/i)).to.not.exist
175173
unmount()
176174
})
177175

178-
test("withErrorBoundary HOC", () => {
176+
test.only("withErrorBoundary HOC", () => {
179177
const consoleError = console.error as MockedFunction<(args: unknown[]) => void>
180178

181179
const onErrorHandler = vi.fn()
182180
const Boundary = withErrorBoundary(
183181
() => {
184182
throw new Error("💥 CABOOM 💥")
185183
},
186-
{FallbackComponent: ErrorFallback, onError: onErrorHandler},
184+
{
185+
FallbackComponent: ErrorFallback,
186+
onError: onErrorHandler,
187+
},
187188
)
188189
const {unmount} = render(<Boundary />)
189-
190-
const [[actualError], [componentStack]] = consoleError.mock.calls
191-
const firstLineOfError = firstLine(actualError as string)
192-
expect(firstLineOfError).toMatchInlineSnapshot(`"Error: Uncaught [Error: 💥 CABOOM 💥]"`)
193-
expect(cleanStack(componentStack)).toMatchInlineSnapshot(`
194-
"Error: Uncaught [Error: 💥 CABOOM 💥]
195-
at reportException
196-
at innerInvokeEventListeners
197-
at invokeEventListeners
198-
at HTMLUnknownElementImpl._dispatch
199-
at HTMLUnknownElementImpl.dispatchEvent
200-
at HTMLUnknownElement.dispatchEvent
201-
at Object.invokeGuardedCallbackDev
202-
at invokeGuardedCallback
203-
at beginWork\$1
204-
at performUnitOfWork "
205-
`)
206-
expect(consoleError).toHaveBeenCalledTimes(3)
190+
const calls = consoleError.mock.calls[0]
191+
//@ts-expect-error - it's a mock
192+
expect(calls[1]).toMatchInlineSnapshot("[Error: 💥 CABOOM 💥]")
193+
expect(consoleError).toHaveBeenCalledTimes(1)
207194
consoleError.mockClear()
208195

209196
const [error, onErrorComponentStack] = (onErrorHandler.mock.calls as [[Error, string]])[0]
@@ -265,7 +252,6 @@ test("requires either a fallback, fallbackRender, or FallbackComponent", () => {
265252
let unmount: undefined | (() => void)
266253
expect(() => {
267254
const result = render(
268-
// @ts-expect-error we're testing the runtime check of missing props here
269255
<ErrorBoundary>
270256
<Bomb />
271257
</ErrorBoundary>,
@@ -318,7 +304,7 @@ test("supports automatic reset of error boundary when resetKeys change", () => {
318304
// blow it up
319305
userEvent.click(screen.getByText("toggle explode"))
320306
expect(screen.getByRole("alert")).to.exist
321-
expect(consoleError).toHaveBeenCalledTimes(3)
307+
expect(consoleError).toHaveBeenCalledTimes(1)
322308
consoleError.mockClear()
323309

324310
// recover via try again button
@@ -333,7 +319,7 @@ test("supports automatic reset of error boundary when resetKeys change", () => {
333319
// blow it up again
334320
userEvent.click(screen.getByText("toggle explode"))
335321
expect(screen.getByRole("alert")).to.exist
336-
expect(consoleError).toHaveBeenCalledTimes(3)
322+
expect(consoleError).toHaveBeenCalledTimes(1)
337323
consoleError.mockClear()
338324

339325
// recover via resetKeys change
@@ -348,7 +334,7 @@ test("supports automatic reset of error boundary when resetKeys change", () => {
348334
// blow it up again
349335
userEvent.click(screen.getByText("toggle explode"))
350336
expect(screen.getByRole("alert")).to.exist
351-
expect(consoleError).toHaveBeenCalledTimes(3)
337+
expect(consoleError).toHaveBeenCalledTimes(1)
352338
consoleError.mockClear()
353339

354340
// toggles adding an extra resetKey to the array
@@ -358,7 +344,7 @@ test("supports automatic reset of error boundary when resetKeys change", () => {
358344
expect(handleResetKeysChange).toHaveBeenCalledWith([true], [true, true])
359345
handleResetKeysChange.mockClear()
360346
expect(screen.getByRole("alert")).to.exist
361-
expect(consoleError).toHaveBeenCalledTimes(3)
347+
expect(consoleError).toHaveBeenCalledTimes(1)
362348
consoleError.mockClear()
363349

364350
// toggle explode back to false
@@ -369,7 +355,7 @@ test("supports automatic reset of error boundary when resetKeys change", () => {
369355
expect(handleResetKeysChange).toHaveBeenCalledWith([true, true], [false, true])
370356
expect(screen.getByRole("alert")).to.exist
371357
handleResetKeysChange.mockClear()
372-
expect(consoleError).toHaveBeenCalledTimes(3)
358+
expect(consoleError).toHaveBeenCalledTimes(1)
373359
consoleError.mockClear()
374360

375361
// toggle extra resetKey
@@ -411,7 +397,7 @@ test("supports reset via resetKeys right after error is triggered on component m
411397

412398
// it blows up on render
413399
expect(screen.queryByRole("alert", {})).to.exist
414-
expect(consoleError).toHaveBeenCalledTimes(3)
400+
expect(consoleError).toHaveBeenCalledTimes(1)
415401
consoleError.mockClear()
416402

417403
// recover via "toggle explode" button

0 commit comments

Comments
 (0)