Skip to content

Commit

Permalink
fix(setupServer): throw InternalError in listeners as-is
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Apr 26, 2024
1 parent 82d45c7 commit 1faf9a1
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -137,7 +137,7 @@
"@bundled-es-modules/statuses": "^1.0.1",
"@inquirer/confirm": "^3.0.0",
"@mswjs/cookies": "^1.1.0",
"@mswjs/interceptors": "^0.28.3",
"@mswjs/interceptors": "^0.29.0",
"@open-draft/until": "^2.1.0",
"@types/cookie": "^0.6.0",
"@types/statuses": "^2.0.4",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/core/utils/internal/devUtils.ts
Expand Up @@ -29,3 +29,7 @@ export const devUtils = {
warn,
error,
}

export class InternalError extends Error {
//
}
6 changes: 3 additions & 3 deletions src/core/utils/request/onUnhandledRequest.ts
@@ -1,5 +1,5 @@
import { toPublicUrl } from './toPublicUrl'
import { devUtils } from '../internal/devUtils'
import { InternalError, devUtils } from '../internal/devUtils'

export interface UnhandledRequestPrint {
warning(): void
Expand Down Expand Up @@ -33,7 +33,7 @@ export async function onUnhandledRequest(
devUtils.error('Error: %s', unhandledRequestMessage)

// Throw an exception to halt request processing and not perform the original request.
throw new Error(
throw new InternalError(
devUtils.formatMessage(
'Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.',
),
Expand All @@ -49,7 +49,7 @@ export async function onUnhandledRequest(
break

default:
throw new Error(
throw new InternalError(
devUtils.formatMessage(
'Failed to react to an unhandled request: unknown strategy "%s". Please provide one of the supported strategies ("bypass", "warn", "error") or a custom callback function as the value of the "onUnhandledRequest" option.',
strategy,
Expand Down
8 changes: 7 additions & 1 deletion src/node/SetupServerCommonApi.ts
Expand Up @@ -15,7 +15,7 @@ import { SetupApi } from '~/core/SetupApi'
import { handleRequest } from '~/core/utils/handleRequest'
import type { RequestHandler } from '~/core/handlers/RequestHandler'
import { mergeRight } from '~/core/utils/internal/mergeRight'
import { devUtils } from '~/core/utils/internal/devUtils'
import { InternalError, devUtils } from '~/core/utils/internal/devUtils'
import type { SetupServerCommon } from './glossary'

export const DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {
Expand Down Expand Up @@ -68,6 +68,12 @@ export class SetupServerCommonApi
return
})

this.interceptor.on('unhandledException', ({ error }) => {
if (error instanceof InternalError) {
throw error
}
})

this.interceptor.on(
'response',
({ response, isMockedResponse, request, requestId }) => {
Expand Down
Expand Up @@ -68,12 +68,7 @@ test('errors on unhandled request when using the "error" value', async () => {

const requestError = await makeRequest()

expect(requestError.message).toBe('Failed to fetch')
/**
* @note Undici wraps fetch rejections in a generic "Failed to fetch" error,
* forwarding the actual rejection in the "error.cause" property.
*/
expect(requestError.cause).toEqual(
expect(requestError).toEqual(
new Error(
'[MSW] Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.',
),
Expand Down
50 changes: 50 additions & 0 deletions test/node/rest-api/response/response-cookies.test.ts
@@ -0,0 +1,50 @@
/**
* @vitest-environment node
*/
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'

const server = setupServer()

beforeAll(() => {
server.listen()
})

afterEach(() => {
server.resetHandlers()
})

afterAll(() => {
server.close()
})

it('supports mocking a response cookie', async () => {
server.use(
http.get('*/resource', () => {
return new HttpResponse(null, {
headers: {
'Set-Cookie': 'a=1',
},
})
}),
)

const response = await fetch('http://localhost/resource')
expect(response.headers.get('Set-Cookie')).toBe('a=1')
})

it('supports mocking multiple response cookies', async () => {
server.use(
http.get('*/resource', () => {
return new HttpResponse(null, {
headers: [
['Set-Cookie', 'a=1'],
['Set-Cookie', 'b=2'],
],
})
}),
)

const response = await fetch('http://localhost/resource')
expect(response.headers.get('Set-Cookie')).toBe('a=1, b=2')
})

0 comments on commit 1faf9a1

Please sign in to comment.