From f07b7ef41bb2c203178e723c859269e945c327df Mon Sep 17 00:00:00 2001 From: Tobbe Lundberg Date: Sat, 2 Mar 2024 17:04:46 +0100 Subject: [PATCH 1/2] chore(unit-tests): Silence middleware error logging --- .../src/middleware/invokeMiddleware.test.ts | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/middleware/invokeMiddleware.test.ts b/packages/vite/src/middleware/invokeMiddleware.test.ts index 7547f5a14f60..748a43eb99c2 100644 --- a/packages/vite/src/middleware/invokeMiddleware.test.ts +++ b/packages/vite/src/middleware/invokeMiddleware.test.ts @@ -1,4 +1,5 @@ -import { describe, expect, test } from 'vitest' +import type { MockInstance } from 'vitest' +import { describe, expect, test, vi } from 'vitest' import { defaultAuthProviderState } from '@redwoodjs/auth' @@ -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 () => { From fb3ad45c23796a42f8158bf9bf35451601b0f5ba Mon Sep 17 00:00:00 2001 From: Tobbe Lundberg Date: Sat, 2 Mar 2024 17:09:45 +0100 Subject: [PATCH 2/2] Add missing imports --- packages/vite/src/middleware/invokeMiddleware.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/middleware/invokeMiddleware.test.ts b/packages/vite/src/middleware/invokeMiddleware.test.ts index 748a43eb99c2..c5befa070c50 100644 --- a/packages/vite/src/middleware/invokeMiddleware.test.ts +++ b/packages/vite/src/middleware/invokeMiddleware.test.ts @@ -1,5 +1,5 @@ import type { MockInstance } from 'vitest' -import { describe, expect, test, vi } from 'vitest' +import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest' import { defaultAuthProviderState } from '@redwoodjs/auth'