Skip to content

Commit

Permalink
Merge branch 'main' into fix/bundle-browser-build
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Jan 22, 2024
2 parents b8bdb4f + bc0bea6 commit 15180d8
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 706 deletions.
5 changes: 0 additions & 5 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,3 @@ declare module 'babel-minify' {
babelOpts: Record<string, any>,
): { code: string }
}

declare module '@bundled-es-modules/js-levenshtein' {
import levenshtein from 'js-levenshtein'
export default levenshtein
}
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "msw",
"version": "2.1.2",
"version": "2.1.3",
"description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
"main": "./lib/core/index.js",
"module": "./lib/core/index.mjs",
Expand Down Expand Up @@ -113,21 +113,18 @@
"sideEffects": false,
"dependencies": {
"@bundled-es-modules/cookie": "^2.0.0",
"@bundled-es-modules/js-levenshtein": "^2.0.1",
"@bundled-es-modules/statuses": "^1.0.1",
"@mswjs/cookies": "^1.1.0",
"@mswjs/interceptors": "^0.25.14",
"@open-draft/until": "^2.1.0",
"@types/cookie": "^0.6.0",
"@types/js-levenshtein": "^1.1.3",
"@types/statuses": "^2.0.4",
"chalk": "^4.1.2",
"chokidar": "^3.4.2",
"graphql": "^16.8.1",
"headers-polyfill": "^4.0.2",
"inquirer": "^8.2.0",
"is-node-process": "^1.2.0",
"js-levenshtein": "^1.1.6",
"outvariant": "^1.4.2",
"path-to-regexp": "^6.2.0",
"strict-event-emitter": "^0.5.1",
Expand Down
21 changes: 0 additions & 21 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion src/core/utils/handleRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export async function handleRequest(
// If the handler lookup returned nothing, no request handler was found
// matching this request. Report the request as unhandled.
if (!lookupResult.data) {
await onUnhandledRequest(request, handlers, options.onUnhandledRequest)
await onUnhandledRequest(request, options.onUnhandledRequest)
emitter.emit('request:unhandled', { request, requestId })
emitter.emit('request:end', { request, requestId })
handleRequestOptions?.onPassthroughResponse?.(request)
Expand Down
106 changes: 5 additions & 101 deletions src/core/utils/request/onUnhandledRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import {
onUnhandledRequest,
UnhandledRequestCallback,
} from './onUnhandledRequest'
import { HttpHandler, HttpMethods } from '../../handlers/HttpHandler'
import { ResponseResolver } from '../../handlers/RequestHandler'

const resolver: ResponseResolver = () => void 0

const fixtures = {
warningWithoutSuggestions: `\
Expand All @@ -24,18 +20,6 @@ Read more: https://mswjs.io/docs/getting-started/mocks`,
• GET /api
If you still wish to intercept this unhandled request, please create a request handler for it.
Read more: https://mswjs.io/docs/getting-started/mocks`,

warningWithSuggestions: (suggestions: string) => `\
[MSW] Warning: intercepted a request without a matching request handler:
• GET /api
Did you mean to request one of the following resources instead?
${suggestions}
If you still wish to intercept this unhandled request, please create a request handler for it.
Read more: https://mswjs.io/docs/getting-started/mocks`,
}
Expand All @@ -52,7 +36,6 @@ afterEach(() => {
test('supports the "bypass" request strategy', async () => {
await onUnhandledRequest(
new Request(new URL('http://localhost/api')),
[],
'bypass',
)

Expand All @@ -61,22 +44,14 @@ test('supports the "bypass" request strategy', async () => {
})

test('supports the "warn" request strategy', async () => {
await onUnhandledRequest(
new Request(new URL('http://localhost/api')),
[],
'warn',
)
await onUnhandledRequest(new Request(new URL('http://localhost/api')), 'warn')

expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
})

test('supports the "error" request strategy', async () => {
await expect(
onUnhandledRequest(
new Request(new URL('http://localhost/api')),
[],
'error',
),
onUnhandledRequest(new Request(new URL('http://localhost/api')), 'error'),
).rejects.toThrow(
'[MSW] Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.',
)
Expand All @@ -89,7 +64,7 @@ test('supports a custom callback function', async () => {
console.warn(`callback: ${request.method} ${request.url}`)
})
const request = new Request(new URL('/user', 'http://localhost:3000'))
await onUnhandledRequest(request, [], callback)
await onUnhandledRequest(request, callback)

expect(callback).toHaveBeenCalledTimes(1)
expect(callback).toHaveBeenCalledWith(request, {
Expand All @@ -111,7 +86,7 @@ test('supports calling default strategies from the custom callback function', as
},
)
const request = new Request(new URL('http://localhost/api'))
await expect(onUnhandledRequest(request, [], callback)).rejects.toThrow(
await expect(onUnhandledRequest(request, callback)).rejects.toThrow(
`[MSW] Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.`,
)

Expand All @@ -126,86 +101,15 @@ test('supports calling default strategies from the custom callback function', as
})

test('does not print any suggestions given no handlers to suggest', async () => {
await onUnhandledRequest(
new Request(new URL('http://localhost/api')),
[],
'warn',
)

expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
})

test('does not print any suggestions given no handlers are similar', async () => {
await onUnhandledRequest(
new Request(new URL('http://localhost/api')),
[
// None of the defined request handlers match the actual request URL
// to be used as suggestions.
new HttpHandler(HttpMethods.GET, 'https://api.github.com', resolver),
new HttpHandler(HttpMethods.GET, 'https://api.stripe.com', resolver),
],
'warn',
)
await onUnhandledRequest(new Request(new URL('http://localhost/api')), 'warn')

expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
})

test('respects RegExp as a request handler method', async () => {
await onUnhandledRequest(
new Request(new URL('http://localhost/api')),
[new HttpHandler(/^GE/, 'http://localhost/api', resolver)],
'warn',
)

expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
})

test('sorts the suggestions by relevance', async () => {
await onUnhandledRequest(
new Request(new URL('http://localhost/api')),
[
new HttpHandler(HttpMethods.GET, '/', resolver),
new HttpHandler(HttpMethods.GET, 'https://api.example.com/api', resolver),
new HttpHandler(HttpMethods.POST, '/api', resolver),
],
'warn',
)

expect(console.warn).toHaveBeenCalledWith(
fixtures.warningWithSuggestions(`\
• POST /api
• GET /`),
)
})

test('does not print more than 4 suggestions', async () => {
await onUnhandledRequest(
new Request(new URL('http://localhost/api')),
[
new HttpHandler(HttpMethods.GET, '/ap', resolver),
new HttpHandler(HttpMethods.GET, '/api', resolver),
new HttpHandler(HttpMethods.GET, '/api-1', resolver),
new HttpHandler(HttpMethods.GET, '/api-2', resolver),
new HttpHandler(HttpMethods.GET, '/api-3', resolver),
new HttpHandler(HttpMethods.GET, '/api-4', resolver),
],
'warn',
)

expect(console.warn).toHaveBeenCalledWith(
fixtures.warningWithSuggestions(`\
• GET /api
• GET /ap
• GET /api-1
• GET /api-2`),
)
})

test('throws an exception given unknown request strategy', async () => {
await expect(
onUnhandledRequest(
new Request(new URL('http://localhost/api')),
[],
// @ts-expect-error Intentional unknown strategy.
'invalid-strategy',
),
Expand Down
Loading

0 comments on commit 15180d8

Please sign in to comment.