Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(unit-tests): Silence middleware error logging #10097

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading