-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace vitest-fetch-mock with msw (#1592)
* replace vitest-fetch-mock with msw in index.test This replaces vitest-fetch-mock with msw in the index.test.ts file. * Rename getCookies to getRequestCookies * Rename testPath to toAbsoluteURL * Rename TestRequestHandler to MockRequestHandler * Move msw utils to separate fixture * Update v7-beta.test.ts to utilize msw * Update index.bench.js to utilize msw Additionally ensures all clients are setup correctly for GET tests. * remove vitest-fetch-mock dependency * Update testing.md to include short example of msw * Fix failing test * Add MultipleResponse example to api.yaml and run generate-types * Include 'use the selected content' in v7-beta.test.ts
- Loading branch information
Showing
10 changed files
with
1,866 additions
and
524 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
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
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
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
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,124 @@ | ||
import { | ||
http, | ||
HttpResponse, | ||
type JsonBodyType, | ||
type StrictRequest, | ||
type DefaultBodyType, | ||
type HttpResponseResolver, | ||
type PathParams, | ||
type AsyncResponseResolverReturnType, | ||
} from "msw"; | ||
import { setupServer } from "msw/node"; | ||
|
||
/** | ||
* Mock server instance | ||
*/ | ||
export const server = setupServer(); | ||
|
||
/** | ||
* Default baseUrl for tests | ||
*/ | ||
export const baseUrl = "https://api.example.com" as const; | ||
|
||
/** | ||
* Test path helper, returns a an absolute URL based on | ||
* the given path and base | ||
*/ | ||
export function toAbsoluteURL(path: string, base: string = baseUrl) { | ||
// If we have absolute path | ||
if (URL.canParse(path)) { | ||
return new URL(path).toString(); | ||
} | ||
|
||
// Otherwise we want to support relative paths | ||
// where base may also contain some part of the path | ||
// e.g. | ||
// base = https://api.foo.bar/v1/ | ||
// path = /self | ||
// should result in https://api.foo.bar/v1/self | ||
|
||
// Construct base URL | ||
const baseUrlInstance = new URL(base); | ||
|
||
// prepend base url url pathname to path and ensure only one slash between the URL parts | ||
const newPath = `${baseUrlInstance.pathname}/${path}`.replace(/\/+/g, "/"); | ||
|
||
return new URL(newPath, baseUrlInstance).toString(); | ||
} | ||
|
||
export type MswHttpMethod = keyof typeof http; | ||
|
||
export interface MockRequestHandlerOptions< | ||
// Recreate the generic signature of the HTTP resolver | ||
// so the arguments passed to http handlers propagate here. | ||
Params extends PathParams<keyof Params> = PathParams, | ||
RequestBodyType extends DefaultBodyType = DefaultBodyType, | ||
ResponseBodyType extends DefaultBodyType = undefined, | ||
> { | ||
baseUrl?: string; | ||
method: MswHttpMethod; | ||
/** | ||
* Relative or absolute path to match. | ||
* When relative, baseUrl will be used as base. | ||
*/ | ||
path: string; | ||
body?: JsonBodyType; | ||
headers?: Record<string, string>; | ||
status?: number; | ||
|
||
/** | ||
* Optional handler which will be called instead of using the body, headers and status | ||
*/ | ||
handler?: HttpResponseResolver<Params, RequestBodyType, ResponseBodyType>; | ||
} | ||
|
||
/** | ||
* Configures a msw request handler using the provided options. | ||
*/ | ||
export function useMockRequestHandler< | ||
// Recreate the generic signature of the HTTP resolver | ||
// so the arguments passed to http handlers propagate here. | ||
Params extends PathParams<keyof Params> = PathParams, | ||
RequestBodyType extends DefaultBodyType = DefaultBodyType, | ||
ResponseBodyType extends DefaultBodyType = undefined, | ||
>({ | ||
baseUrl: requestBaseUrl, | ||
method, | ||
path, | ||
body, | ||
headers, | ||
status, | ||
handler, | ||
}: MockRequestHandlerOptions<Params, RequestBodyType, ResponseBodyType>) { | ||
let requestUrl = ""; | ||
let receivedRequest: null | StrictRequest<DefaultBodyType> = null; | ||
let receivedCookies: null | Record<string, string> = null; | ||
|
||
const resolvedPath = toAbsoluteURL(path, requestBaseUrl); | ||
|
||
server.use( | ||
http[method]<Params, RequestBodyType, ResponseBodyType>( | ||
resolvedPath, | ||
(args) => { | ||
requestUrl = args.request.url; | ||
receivedRequest = args.request.clone(); | ||
receivedCookies = { ...args.cookies }; | ||
|
||
if (handler) { | ||
return handler(args); | ||
} | ||
|
||
return HttpResponse.json(body, { | ||
status: status ?? 200, | ||
headers, | ||
}) as AsyncResponseResolverReturnType<ResponseBodyType>; | ||
}, | ||
), | ||
); | ||
|
||
return { | ||
getRequestCookies: () => receivedCookies!, | ||
getRequest: () => receivedRequest!, | ||
getRequestUrl: () => new URL(requestUrl), | ||
}; | ||
} |
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
Oops, something went wrong.