-
TodoI would like to confirm that the test I am implementing works with the following configuration. 🙏🏻
ProblemThe return value is My codequery EmployeesBelongingToGroup($groupId: ID!) {
employeesBelongingToGroup(groupId: $groupId) {
id
email
name
image
belongings {
id
belongingType
belongingId
}
} request handler fow msw // src/mocks/handlers.ts
import type {
EmployeesBelongingToGroupQuery,
EmployeesBelongingToGroupQueryVariables,
} from '@generated/graphql'
import { anEmployee } from '@generated/graphql-mocks'
import { graphql } from 'msw'
export const handlers = [
graphql.query<
EmployeesBelongingToGroupQuery,
EmployeesBelongingToGroupQueryVariables
>('EmployeesBelongingToGroup', (req, res, ctx) => {
const { groupId } = req.variables
return res(
ctx.data({
employeesBelongingToGroup: [
anEmployee({ groupId }), // anEmployee is dummy data
anEmployee({ groupId }),
],
})
)
}), set up msw's server // src/mocks/server.ts
import { setupServer } from 'msw/node'
import { handlers } from './handlers'
export const server = setupServer(...handlers) set up vitest file // src/test/setup.ts
import '@testing-library/jest-dom'
import { afterAll, afterEach, beforeAll } from 'vitest'
import { server } from '../mocks/server'
beforeAll(() => {
server.listen({ onUnhandledRequest: 'error' })
server.printHandlers() // for debug
})
afterEach(() => server.resetHandlers())
afterAll(() => server.close()) custom hooks to be tested // src/pages/EmployeesSearch/hooks/useFetchEmployees.ts
import { useEmployeesBelongingToGroupQuery } from '@generated/graphql'
import { useEffect } from 'react'
export const useFetchEmployees = (groupId: number) => {
const [{ data }, refetch] = useEmployeesBelongingToGroupQuery({
variables: { groupId: String(groupId) },
pause: true,
})
useEffect(() => {
if (groupId != -1) refetch()
}, [groupId])
return [data?.employeesBelongingToGroup] test file! import { useFetchEmployees } from './useFetchEmployees'
import { renderHook } from '@testing-library/react-hooks'
import type { FC } from 'react'
import React from 'react'
import { createClient, Provider } from 'urql'
import { useFetchEmployees } from './useFetchEmployees'
const client = createClient({
url: 'http://localhost:3000/api',
})
export const MockProvider: FC = ({ children }) => (
<Provider value={client}>{children}</Provider>
)
describe('useFetchEmployees', () => {
it('returns data when groupId != -1', async () => {
const { result, waitForNextUpdate } = renderHook(
() => useFetchEmployees(1),
{
wrapper: MockProvider,
}
)
await waitForNextUpdate()
const [data] = result.current
expect(data).toBeDefined() // simple test
})
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hey, @kazu-2020. Do you see the list of handlers printed during your test run? If not, your setup file where you call If you see the list of handlers, do you see an error that a certain request is not handled? If yes, then the predicate criteria for your GraphQL query do not match the actual query. Double-check if the query name is correct. If not, debug the output of URQL during your test run. You can do that via One more thing: certain GraphQl clients are picky when it comes to including client meta data in the response body. Disable MSW and check the shape of the original response from URQL. Does it contain certain properties your mocked response doesn't? Pay attention to things like |
Beta Was this translation helpful? Give feedback.
#1192 (reply in thread)