Getting error in React TypeScript test case with mock data. I got the following error:

TS2345: Argument of type '(req: any, res: any, ctx: any) => any' is not assignable to parameter of type 'HttpResponseResolver<PathParams, DefaultBodyType, undefined>'.

Test case code is

import { http } from 'msw'; 
export const handlers = [
http.get("https://jsonplaceholder.typicode.com/users", (req, res, ctx) => {
    return res(
        ctx.status(200),
        ctx.json([
            {
                name: 'Leanne Graham'
            },
            {
                name: 'Ervin Howell'
            },
            {
                name: 'Clementine Bauch'
            },
        ])
    )
  })
];
1

There are 1 best solutions below

0
Lin Du On

It seems you are using MSW v2+, please see the v2 documentation Respond with a mocked response, the correct way is:

import { http, HttpResponse } from 'msw';

export const handlers = [
  http.get(
    'https://jsonplaceholder.typicode.com/users',
    ({ request, params, requestId }) => {
      return HttpResponse.json(
        [
          {
            name: 'Leanne Graham',
          },
          {
            name: 'Ervin Howell',
          },
          {
            name: 'Clementine Bauch',
          },
        ],
        { status: 200 }
      );
    }
  ),
];

package version:

"msw": "^2.2.1",