msw 2.7.2 → 2.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +45 -80
  2. package/lib/core/{GraphQLHandler-D4_CxZQj.d.mts → GraphQLHandler-BNMGdWQe.d.mts} +1 -0
  3. package/lib/core/{GraphQLHandler-Pox7fIFM.d.ts → GraphQLHandler-D1CSV926.d.ts} +1 -0
  4. package/lib/core/graphql.d.mts +1 -1
  5. package/lib/core/graphql.d.ts +1 -1
  6. package/lib/core/handlers/GraphQLHandler.d.mts +1 -1
  7. package/lib/core/handlers/GraphQLHandler.d.ts +1 -1
  8. package/lib/core/handlers/GraphQLHandler.js.map +1 -1
  9. package/lib/core/handlers/GraphQLHandler.mjs.map +1 -1
  10. package/lib/core/index.d.mts +2 -1
  11. package/lib/core/index.d.ts +2 -1
  12. package/lib/core/index.js +2 -0
  13. package/lib/core/index.js.map +1 -1
  14. package/lib/core/index.mjs +2 -0
  15. package/lib/core/index.mjs.map +1 -1
  16. package/lib/core/isCommonAssetRequest.d.mts +20 -0
  17. package/lib/core/isCommonAssetRequest.d.ts +20 -0
  18. package/lib/core/isCommonAssetRequest.js +42 -0
  19. package/lib/core/isCommonAssetRequest.js.map +1 -0
  20. package/lib/core/isCommonAssetRequest.mjs +22 -0
  21. package/lib/core/isCommonAssetRequest.mjs.map +1 -0
  22. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +1 -1
  23. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +1 -1
  24. package/lib/core/utils/request/onUnhandledRequest.js +3 -3
  25. package/lib/core/utils/request/onUnhandledRequest.js.map +1 -1
  26. package/lib/core/utils/request/onUnhandledRequest.mjs +3 -3
  27. package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -1
  28. package/lib/core/utils/url/getAbsoluteUrl.js +1 -1
  29. package/lib/core/utils/url/getAbsoluteUrl.js.map +1 -1
  30. package/lib/core/utils/url/getAbsoluteUrl.mjs +1 -1
  31. package/lib/core/utils/url/getAbsoluteUrl.mjs.map +1 -1
  32. package/lib/iife/index.js +24 -4
  33. package/lib/iife/index.js.map +1 -1
  34. package/lib/mockServiceWorker.js +1 -1
  35. package/package.json +1 -1
  36. package/src/browser/utils/getAbsoluteWorkerUrl.test.ts +1 -3
  37. package/src/core/handlers/GraphQLHandler.ts +1 -0
  38. package/src/core/index.ts +1 -0
  39. package/src/core/isCommonAssetRequest.ts +45 -0
  40. package/src/core/utils/request/onUnhandledRequest.test.ts +65 -10
  41. package/src/core/utils/request/onUnhandledRequest.ts +6 -10
  42. package/src/core/utils/url/getAbsoluteUrl.test.ts +29 -11
  43. package/src/core/utils/url/getAbsoluteUrl.ts +1 -2
@@ -8,7 +8,7 @@
8
8
  * - Please do NOT serve this file on production.
9
9
  */
10
10
 
11
- const PACKAGE_VERSION = '2.7.2'
11
+ const PACKAGE_VERSION = '2.7.4'
12
12
  const INTEGRITY_CHECKSUM = '00729d72e3b82faf54ca8b9621dbb96f'
13
13
  const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
14
14
  const activeClientIds = new Set()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "2.7.2",
3
+ "version": "2.7.4",
4
4
  "description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
5
5
  "main": "./lib/core/index.js",
6
6
  "module": "./lib/core/index.mjs",
@@ -1,6 +1,4 @@
1
- /**
2
- * @vitest-environment jsdom
3
- */
1
+ // @vitest-environment jsdom
4
2
  import { getAbsoluteWorkerUrl } from './getAbsoluteWorkerUrl'
5
3
 
6
4
  const rawLocation = window.location
@@ -72,6 +72,7 @@ export type GraphQLResponseBody<BodyType extends DefaultBodyType> =
72
72
  | {
73
73
  data?: BodyType | null
74
74
  errors?: readonly Partial<GraphQLError>[] | null
75
+ extensions?: Record<string, any>
75
76
  }
76
77
  | null
77
78
  | undefined
package/src/core/index.ts CHANGED
@@ -62,6 +62,7 @@ export * from './HttpResponse'
62
62
  export * from './delay'
63
63
  export { bypass } from './bypass'
64
64
  export { passthrough } from './passthrough'
65
+ export { isCommonAssetRequest } from './isCommonAssetRequest'
65
66
 
66
67
  // Validate environmental globals before executing any code.
67
68
  // This ensures that the library gives user-friendly errors
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Determines if the given request is a static asset request.
3
+ * Useful when deciding which unhandled requests to ignore.
4
+ * @note Despite being ignored, you can still intercept and mock
5
+ * static assets by creating request handlers for them.
6
+ *
7
+ * @example
8
+ * import { isCommonAssetRequest } from 'msw'
9
+ *
10
+ * await worker.start({
11
+ * onUnhandledRequest(request, print) {
12
+ * if (!isCommonAssetRequest(request)) {
13
+ * print.warning()
14
+ * }
15
+ * }
16
+ * })
17
+ */
18
+ export function isCommonAssetRequest(request: Request): boolean {
19
+ const url = new URL(request.url)
20
+
21
+ // Ignore certain protocols.
22
+ if (url.protocol === 'file:') {
23
+ return true
24
+ }
25
+
26
+ // Ignore static assets hosts.
27
+ if (/(fonts\.googleapis\.com)/.test(url.hostname)) {
28
+ return true
29
+ }
30
+
31
+ // Ignore node modules served over HTTP.
32
+ if (/node_modules/.test(url.pathname)) {
33
+ return true
34
+ }
35
+
36
+ // Ignore internal Vite requests, like "/@vite/client".
37
+ if (url.pathname.includes('@vite')) {
38
+ return true
39
+ }
40
+
41
+ // Ignore common static assets.
42
+ return /\.(s?css|less|m?jsx?|m?tsx?|html|ttf|otf|woff|woff2|eot|gif|jpe?g|png|avif|webp|svg|mp4|webm|ogg|mov|mp3|wav|ogg|flac|aac|pdf|txt|csv|json|xml|md|zip|tar|gz|rar|7z)$/i.test(
43
+ url.pathname,
44
+ )
45
+ }
@@ -1,6 +1,4 @@
1
- /**
2
- * @vitest-environment jsdom
3
- */
1
+ // @vitest-environment jsdom
4
2
  import {
5
3
  onUnhandledRequest,
6
4
  UnhandledRequestCallback,
@@ -90,7 +88,7 @@ test('supports the "error" request strategy', async () => {
90
88
  })
91
89
 
92
90
  test('supports a custom callback function', async () => {
93
- const callback = vi.fn<Parameters<UnhandledRequestCallback>>((request) => {
91
+ const callback = vi.fn<UnhandledRequestCallback>((request) => {
94
92
  console.warn(`callback: ${request.method} ${request.url}`)
95
93
  })
96
94
  const request = new Request(new URL('/user', 'http://localhost:3000'))
@@ -109,12 +107,10 @@ test('supports a custom callback function', async () => {
109
107
  })
110
108
 
111
109
  test('supports calling default strategies from the custom callback function', async () => {
112
- const callback = vi.fn<Parameters<UnhandledRequestCallback>>(
113
- (request, print) => {
114
- // Call the default "error" strategy.
115
- print.error()
116
- },
117
- )
110
+ const callback = vi.fn<UnhandledRequestCallback>((request, print) => {
111
+ // Call the default "error" strategy.
112
+ print.error()
113
+ })
118
114
  const request = new Request(new URL('http://localhost/api'))
119
115
  await expect(onUnhandledRequest(request, callback)).rejects.toThrow(
120
116
  `[MSW] Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.`,
@@ -171,3 +167,62 @@ test('prints with an absolute URL and search params', async () => {
171
167
  fixtures.warningWithoutSuggestions(`https://mswjs.io/api?foo=boo`),
172
168
  )
173
169
  })
170
+
171
+ test('ignores common static assets when using the "warn" strategy', async () => {
172
+ await Promise.allSettled([
173
+ onUnhandledRequest(
174
+ new Request(new URL('https://example.com/main.css')),
175
+ 'warn',
176
+ ),
177
+ onUnhandledRequest(
178
+ new Request(new URL('https://example.com/index.mjs')),
179
+ 'warn',
180
+ ),
181
+ onUnhandledRequest(
182
+ new Request(new URL('https://example.com/node_modules/abc-123')),
183
+ 'warn',
184
+ ),
185
+ onUnhandledRequest(
186
+ new Request(new URL('https://fonts.googleapis.com/some-font')),
187
+ 'warn',
188
+ ),
189
+ ])
190
+
191
+ expect(console.warn).not.toHaveBeenCalled()
192
+ })
193
+
194
+ test('ignores common static assets when using the "error" strategy', async () => {
195
+ await Promise.allSettled([
196
+ onUnhandledRequest(
197
+ new Request(new URL('https://example.com/main.css')),
198
+ 'error',
199
+ ),
200
+ onUnhandledRequest(
201
+ new Request(new URL('https://example.com/index.mjs')),
202
+ 'error',
203
+ ),
204
+ onUnhandledRequest(
205
+ new Request(new URL('https://example.com/node_modules/abc-123')),
206
+ 'error',
207
+ ),
208
+ onUnhandledRequest(
209
+ new Request(new URL('https://fonts.googleapis.com/some-font')),
210
+ 'error',
211
+ ),
212
+ ])
213
+
214
+ expect(console.error).not.toHaveBeenCalled()
215
+ })
216
+
217
+ test('exposes common static assets to the explicit callback', async () => {
218
+ let callbackRequest!: Request
219
+ await onUnhandledRequest(
220
+ new Request(new URL('https://example.com/main.css')),
221
+ (request) => {
222
+ callbackRequest = request
223
+ },
224
+ )
225
+
226
+ expect(callbackRequest).toBeInstanceOf(Request)
227
+ expect(callbackRequest.url).toBe('https://example.com/main.css')
228
+ })
@@ -1,5 +1,6 @@
1
1
  import { toPublicUrl } from './toPublicUrl'
2
2
  import { InternalError, devUtils } from '../internal/devUtils'
3
+ import { isCommonAssetRequest } from '../../isCommonAssetRequest'
3
4
 
4
5
  export interface UnhandledRequestPrint {
5
6
  warning(): void
@@ -71,15 +72,10 @@ export async function onUnhandledRequest(
71
72
  return
72
73
  }
73
74
 
74
- /**
75
- * @note Ignore "file://" requests.
76
- * Those often are an implementation detail of modern tooling
77
- * that fetches modules via HTTP. Developers don't issue those
78
- * requests and so they mustn't be warned about them.
79
- */
80
- if (url.protocol === 'file:') {
81
- return
75
+ // Ignore common static asset requests when using a built-in strategy.
76
+ // There's a slight overhead here because this utility will create a request URL
77
+ // instance again despite us having done so previously in this function.
78
+ if (!isCommonAssetRequest(request)) {
79
+ applyStrategy(strategy)
82
80
  }
83
-
84
- applyStrategy(strategy)
85
81
  }
@@ -1,29 +1,47 @@
1
- /**
2
- * @vitest-environment jsdom
3
- */
1
+ // @vitest-environment jsdom
4
2
  import { getAbsoluteUrl } from './getAbsoluteUrl'
5
3
 
6
- it('rebases a relative URL against the current "baseURI" (default)', () => {
7
- expect(getAbsoluteUrl('/reviews')).toEqual('http://localhost/reviews')
4
+ const rawLocation = window.location
5
+
6
+ afterAll(() => {
7
+ Object.defineProperty(window, 'location', {
8
+ value: rawLocation,
9
+ })
10
+ })
11
+
12
+ it('resolves a relative URL against the current location (default)', () => {
13
+ expect(getAbsoluteUrl('/reviews')).toBe('http://localhost/reviews')
14
+ })
15
+
16
+ it('supports relative URLs starting with search parameters', () => {
17
+ Object.defineProperty(window, 'location', {
18
+ value: {
19
+ href: 'http://localhost/nested',
20
+ },
21
+ })
22
+
23
+ expect(getAbsoluteUrl('?resourceId=abc-123')).toBe(
24
+ 'http://localhost/nested?resourceId=abc-123',
25
+ )
8
26
  })
9
27
 
10
- it('rebases a relative URL against a custom base URL', () => {
11
- expect(getAbsoluteUrl('/user', 'https://api.github.com')).toEqual(
28
+ it('resolves a relative URL against a custom base URL', () => {
29
+ expect(getAbsoluteUrl('/user', 'https://api.github.com')).toBe(
12
30
  'https://api.github.com/user',
13
31
  )
14
32
  })
15
33
 
16
34
  it('returns a given absolute URL as-is', () => {
17
- expect(getAbsoluteUrl('https://api.mswjs.io/users')).toEqual(
35
+ expect(getAbsoluteUrl('https://api.mswjs.io/users')).toBe(
18
36
  'https://api.mswjs.io/users',
19
37
  )
20
38
  })
21
39
 
22
40
  it('returns an absolute URL given a relative path without a leading slash', () => {
23
- expect(getAbsoluteUrl('users')).toEqual('http://localhost/users')
41
+ expect(getAbsoluteUrl('users')).toBe('http://localhost/users')
24
42
  })
25
43
 
26
44
  it('returns a path with a pattern as-is', () => {
27
- expect(getAbsoluteUrl(':api/user')).toEqual('http://localhost/:api/user')
28
- expect(getAbsoluteUrl('*/resource/*')).toEqual('*/resource/*')
45
+ expect(getAbsoluteUrl(':api/user')).toBe('http://localhost/:api/user')
46
+ expect(getAbsoluteUrl('*/resource/*')).toBe('*/resource/*')
29
47
  })
@@ -16,8 +16,7 @@ export function getAbsoluteUrl(path: string, baseUrl?: string): string {
16
16
 
17
17
  // Resolve a relative request URL against a given custom "baseUrl"
18
18
  // or the document baseURI (in the case of browser/browser-like environments).
19
- const origin =
20
- baseUrl || (typeof document !== 'undefined' && document.baseURI)
19
+ const origin = baseUrl || (typeof location !== 'undefined' && location.href)
21
20
 
22
21
  return origin
23
22
  ? // Encode and decode the path to preserve escaped characters.