expect.extend 로 toMatchSchema 를 추가한다. 매처는 Response 의 json 을 꺼내 schema.safeParseAsync 로 검사하고 성공 여부를 pass 로 돌려준다. 비동기 매처라 타입 선언의 반환은 R 이 아니라 Promise<void> 이고, 테스트에서 await expect(res).toMatchSchema(schema) 로 기다려야 한다.

-- expect.extend: Response 의 본문이 Zod 스키마에 맞는가
toMatchSchema :: Response -> Schema -> IO MatcherResult
toMatchSchema res schema = do
  body <- json res
  r    <- safeParseAsync schema body
  pure MatcherResult { pass = success r, message = "" }

-- declare module 'vitest' { interface Matchers<R, T> { toMatchSchema: (s) => Promise<void> } }
-- 비동기 매처: 반환 타입은 R 이 아니라 Promise<void>, 테스트에서 await
-- await expect(todoResponse).toMatchSchema(todoSchema)

출처

`#317`