MSW Source

website·Indexed 39 days ago

https://source.mswjs.io

A library for generating request handlers for Mock Service Worker (MSW) from various sources. It provides utilities such as fromOpenApi to generate handlers from OpenAPI documents and fromTraffic to generate handlers from HAR files.

Tokens
5.3K
Snippets
36
Records
49
Agent score
53%

What's inside MSW Source

  1. Overview of MSW Source for request handler generation

    Source is a library designed to generate request handlers for Mock Service Worker (MSW) from supported inputs such as OpenAPI documents or HAR files. It is particularly useful for migrating large test suites to MSW or creating regression tests for network scenarios by converting captured network traffic (e.g., from an *.har file) into reliable, reproducible MSW request handlers.
  2. Generate MSW request handlers using Source

    Source is a library designed to automate the creation of request handlers for Mock Service Worker (MSW). It is particularly useful for migrating large test suites to MSW or creating regression tests for network scenarios. Instead of writing handlers by hand, Source allows developers to generate them from supported inputs such as OpenAPI documents or HAR files. This enables reliable, reproducible network behavior by replaying captured network traffic (e.g., from a .har file) within a test suite.
  3. Generate MSW request handlers using Source

    Source is a library designed to automate the creation of request handlers for Mock Service Worker (MSW). It is particularly useful for migrating large test suites to MSW or creating regression tests for network scenarios. Instead of writing handlers by hand, Source allows developers to generate them from supported inputs such as OpenAPI documents or HAR files. This enables reliable, reproducible network behavior by replaying captured network traffic (e.g., from a .har file) within a test suite.
  4. Generate request handlers using Source

    Source is a library designed to generate request handlers from various external sources, such as HAR files or OpenAPI documents. It is primarily used to replay recorded network traffic when mocking or debugging applications.
  5. Integrate generated handlers into a Node.js server

    Once request handlers are generated via @msw/source, they must be passed to the MSW server setup to begin intercepting network requests in a Node.js process.
    import { setupServer } from 'msw/node'
    import { handlers } from './handlers'
     
    const server = setupServer(...handlers)
    server.listen()
  6. Handle multiple content types in mocked responses

    If a response defines multiple content types, the first one listed in the specification is used by default. You can request a specific content type by setting the Accept request header. Wildcards like * and application/* are supported. If multiple types are listed in the Accept header, the first one found in the specification is used.
    await fetch('/resource', {
      headers: {
        Accept: 'application/xml',
      },
    }).then((res) => res.json())
  7. Record network activity as a HAR file in Firefox

    To record your application's network traffic in Firefox for use with MSW request handler generation, use the browser's DevTools to export a .har file.
    1. Open DevTools (⌘ + ⌥ + I on MacOS; Ctrl + Shift + I on Windows/Linux).
    2. Click on the ”⇅ Network” tab.
    3. Click on the ⚙ icon next to the throttling settings.
    4. Choose “Save all as HAR”.
    5. Save the HAR file to your local system.
  8. Select response content types via Accept header

    When a response defines multiple content types, the first one listed in the specification is used by default. You can request a specific content type by setting the Accept request header. Wildcards like * and application/* are supported. If multiple types are provided in the Accept header, the first matching type found in the specification is used.
    await fetch('/resource', {
      headers: {
        Accept: 'application/xml',
      },
    }).then((res) => res.json())
  9. Create a custom request handler generation utility

    You can generate request handlers from any source (XYZ). While MSW Source may not provide a designated API for every specific format, you can build your own request generation utility using @msw/source as a reference for implementation.
  10. Generate MSW request handlers from an OpenAPI document

    Use the fromOpenApi() function from @msw/source/open-api to generate request handlers. This function is asynchronous because parsing the OpenAPI document is an asynchronous operation. It supports both OpenAPI 2.0 and 3.0 documents. If your specification is not in JSON format, it must be converted to JSON before being passed to the function.
    import { fromOpenApi } from '@msw/source/open-api'
    import spec from './api.spec.json'
     
    const handlers = await fromOpenApi(spec)
  11. Generate MSW request handlers from an OpenAPI document

    Use the fromOpenApi() function from @msw/source/open-api to generate request handlers based on an OpenAPI 2.0 or 3.0 specification. This operation is asynchronous. The function accepts either a JSON file import or an inline JSON object. If your specification is not in JSON format, it must be converted to JSON before use.
    import { fromOpenApi } from '@msw/source/open-api'
    import spec from './api.spec.json'
     
    const handlers = await fromOpenApi(spec)
  12. Generate MSW request handlers from a HAR file

    Use the fromTraffic() function from @msw/source/traffic to convert a network archive (*.har file) into MSW request handlers. Since HAR files are JSON, they can be imported directly. If your environment does not support direct JSON imports, parse the HAR file into a JavaScript object before passing it to fromTraffic.
    import { fromTraffic } from '@msw/source/traffic'
    import traffic from './api.har'
     
    const handlers = fromTraffic(traffic)