Skip to content

Conversation

@xiaohuoni
Copy link
Member

@xiaohuoni xiaohuoni commented Nov 4, 2024

约定存在 path.join(cwd, 'mock/browser.ts'); 文件为浏览器端渲染 Mock 服务

import { setupWorker } from 'msw/browser';
import { handlers } from './handlers';

export const worker = setupWorker(...handlers);

其中 handlers 内容,均用 msw 的写法实现。

import { HttpResponse, graphql, http } from 'msw';

export const handlers = [
  http.get('/user', () => {
    return HttpResponse.json({
      firstName: 'John',
      lastName: 'Maverick',
    });
  }),
  graphql.query('ListMovies', () => {
    return HttpResponse.json({
      data: {
        movies: [
          {
            title: 'The Lord of The Rings',
          },
          {
            title: 'The Matrix',
          },
          {
            title: 'Star Wars: The Empire Strikes Back',
          },
        ],
      },
    });
  }),
];

public 目录下的 mockServiceWorker.js 文件,可以使用 npx msw init public 自动生成。
先在 prepare 里面实现,后续框架增加 hook 之后再调整。

ant-design-pro 的 mock 的写法,提供额外的转换方法兼容,类似 umimock2msw.ts

@xiaohuoni xiaohuoni changed the title Feat mock msw feat: mock msw Nov 4, 2024
@sorrycc sorrycc force-pushed the master branch 3 times, most recently from 6ad94db to a89ddff Compare November 14, 2024 06:21
@sorrycc sorrycc force-pushed the master branch 3 times, most recently from 30efd07 to 5a25649 Compare December 6, 2024 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants