我目前正在使用 next/jest 设置我的环境,使用 msw 和 @testing@library/react。我的测试看起来像:
import { TestComponent } from '../../../components/TestComponent'
import React from 'react'
import { render } from '@testing-library/react'
test('Test TestComponent', function () {
const wrap = render(<TestComponent />)
expect(wrap.container.childElementCount).toBe(2)
})
我的 jest.config.js 看起来像:
const nextJest = require('next/jest')
const createJestConfig = nextJest({
dir: './',
})
const customJestConfig = {
setupFilesAfterEnv: ['<rootDir>/test_utils/setup-env.js'],
moduleDirectories: ['node_modules', '<rootDir>/'],
testEnvironment: 'jest-environment-jsdom',
}
module.exports = createJestConfig(customJestConfig)
我的 setup-env.js 看起来像这样:
import '@testing-library/jest-dom'
import { server } from './server.js'
beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())
我的组件看起来像这样......
import React, { useEffect, useState } from 'react'
import { useSWRFetch } from '../../../hooks'
export const TestComponent = (): JSX.Element => {
const [data, setData] = useState([])
const dataInfo = useSWRFetch({
endpoint: `${process.env.NEXT_PUBLIC_API_URL}/v1/endpoint`,
})
useEffect(() => {
if (dataInfo) {
setDomains(dataInfo.data)
}
}, [dataInfo])
return (
<>
{data &&
data.map((item, idx) => (
<div>
<p>{item.count | 0}</p>
<p>Job</p>
</div>
))}
</>
)
}
但是,当我运行测试时,出现以下错误...
ReferenceError: fetch is not defined
但是,当我重视 dataInfo.data 时,它始终是未定义的。
我有一个 SWR 使用的 fetcher 函数,如下所示:
async function fetcher(...args) {
console.log("BEFORE REQUEST")
const url = args[0]
const token = args[1]
const method = args.length > 2 ? args[2] : 'GET'
const body = args.length > 3 ? { body: JSON.stringify(args[3]) } : {}
const res = await fetch(url, {
method: method,
headers:
method != 'GET'
? {
Authorization: `bearer ${token}`,
'Content-Type': 'application/json',
}
: {
Authorization: `Bearer ${token}`,
},
...body,
})
console.log("AFTER REQUEST")
if (!res.ok) {
const error = new Error('An error occurred while fetching the data.')
// Attach extra info to the error object.
error.info = await res.json()
error.status = res.status
throw error
}
return res.json()
}
function useSWRFetch({ endpoint, token, options = null, condition=true }) {
const { data, error, mutate } = useSWR(token && condition ? [endpoint, token] : null, fetcher, options)
return {
mutate: mutate,
data: data,
isLoading: !error && !data,
error: error,
}
}
但是,它永远不会到达第二个控制台,在请求被拦截后请求之后。