小编Mat*_*w L的帖子

模拟交叉获取

我正在 Next/React 项目上通过 Node 使用 Jest 运行测试。

我也在使用交叉获取。

当我尝试模拟组件的交叉获取时

import crossFetch from 'cross-fetch'
jest.mock('cross-fetch')
Run Code Online (Sandbox Code Playgroud)
        crossFetch.mockResolvedValue({
          status: 200,
          json: () => {{ 
            user : testUser 
          }},
        })
        render(<UserProfile />)
Run Code Online (Sandbox Code Playgroud)

getServerSideProps 中的 API 请求始终返回 500

export async function getServerSideProps({ query: { userId } }) {
  let user = null
  let code = 200
  try {
    let response = await fetch(`https://example.com/users/${userId}`, { method: 'GET' }) 
    let statusCode = response.status
    let data = await response.json()
    if (statusCode !== 200) {
      code = statusCode
    } …
Run Code Online (Sandbox Code Playgroud)

mocking reactjs jestjs next.js

5
推荐指数
1
解决办法
3206
查看次数

标签 统计

jestjs ×1

mocking ×1

next.js ×1

reactjs ×1