forked from redwoodjs/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(middeware): Allow registration with async functions (redwoodjs#10413
- Loading branch information
Showing
3 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- fix(middeware): Allow registration with async functions (#10413) by @dac09 |
95 changes: 95 additions & 0 deletions
95
packages/vite/src/middleware/createMiddlewareRouter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import type { ViteDevServer } from 'vite' | ||
import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
||
import { createMiddlewareRouter } from './register' | ||
|
||
vi.mock('@redwoodjs/project-config', async () => { | ||
return { | ||
getPaths: () => { | ||
return { | ||
web: { | ||
base: '/proj/web', | ||
dist: '/proj/web/dist', | ||
distEntryServer: '/proj/web/dist/entry-server.mjs', | ||
entryServer: '/proj/web/entry-server.tsx', | ||
}, | ||
} | ||
}, | ||
} | ||
}) | ||
|
||
const distRegisterMwMock = vi.fn() | ||
vi.mock('/proj/web/dist/entry-server.mjs', () => { | ||
return { | ||
registerMiddleware: distRegisterMwMock, | ||
} | ||
}) | ||
|
||
describe('createMiddlewareRouter', () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks() | ||
}) | ||
|
||
it('Should load using vite dev server, and load from entry-server source', async () => { | ||
const mockVite = { | ||
ssrLoadModule: vi.fn().mockReturnValue({ | ||
registerMiddleware: () => { | ||
return [] | ||
}, | ||
}), | ||
} as unknown as ViteDevServer | ||
|
||
await createMiddlewareRouter(mockVite) | ||
|
||
expect(mockVite.ssrLoadModule).toHaveBeenCalledWith( | ||
'/proj/web/entry-server.tsx', | ||
) | ||
}) | ||
|
||
it('Should load import from distEntryServer for prod', async () => { | ||
await createMiddlewareRouter() | ||
expect(distRegisterMwMock).toHaveBeenCalled() | ||
}) | ||
|
||
it('(async) should return a router with middleware handlers', async () => { | ||
// Testing async case | ||
distRegisterMwMock.mockResolvedValue([ | ||
[vi.fn(), '/bazinga'], | ||
[vi.fn(), '/kittens'], | ||
vi.fn(), | ||
]) | ||
|
||
const result = await createMiddlewareRouter() | ||
|
||
expect(result.prettyPrint()).toMatchInlineSnapshot(` | ||
"└── (empty root node) | ||
├── / | ||
│ ├── bazinga (GET) | ||
│ └── kittens (GET) | ||
└── * (GET) | ||
" | ||
`) | ||
expect(distRegisterMwMock).toHaveBeenCalled() | ||
}) | ||
|
||
it('(sync) should return a router with middleware handlers', async () => { | ||
// Testing sync case | ||
distRegisterMwMock.mockReturnValue([ | ||
[vi.fn(), '/bazinga'], | ||
[vi.fn(), '/kittens'], | ||
vi.fn(), | ||
]) | ||
|
||
const result = await createMiddlewareRouter() | ||
|
||
expect(result.prettyPrint()).toMatchInlineSnapshot(` | ||
"└── (empty root node) | ||
├── / | ||
│ ├── bazinga (GET) | ||
│ └── kittens (GET) | ||
└── * (GET) | ||
" | ||
`) | ||
expect(distRegisterMwMock).toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters