Skip to content

Commit

Permalink
chore(unit-tests): Silence middleware error logging (#10097)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Mar 2, 2024
1 parent b41ca33 commit 1798cc8
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions packages/vite/src/middleware/invokeMiddleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, test } from 'vitest'
import type { MockInstance } from 'vitest'
import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'

import { defaultAuthProviderState } from '@redwoodjs/auth'

Expand Down Expand Up @@ -32,18 +33,30 @@ describe('Invoke middleware', () => {
})
})

test('returns a MiddlewareResponse, even if middleware throws', async () => {
const throwingMiddleware = () => {
throw new Error('I want to break free')
}
describe('throwing middleware behavior', () => {
let consoleErrorSpy: MockInstance

const [mwRes, authState] = await invoke(
new Request('https://example.com'),
throwingMiddleware
)
beforeAll(() => {
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
})

expect(mwRes).toBeInstanceOf(MiddlewareResponse)
expect(authState).toEqual(defaultAuthProviderState)
afterAll(() => {
consoleErrorSpy.mockRestore()
})

test('returns a MiddlewareResponse, even if middleware throws', async () => {
const throwingMiddleware = () => {
throw new Error('I want to break free')
}

const [mwRes, authState] = await invoke(
new Request('https://example.com'),
throwingMiddleware
)

expect(mwRes).toBeInstanceOf(MiddlewareResponse)
expect(authState).toEqual(defaultAuthProviderState)
})
})

test('returns a MiddlewareResponse, even if middleware returns a Response', async () => {
Expand Down

0 comments on commit 1798cc8

Please sign in to comment.