'Apollo Client GraphQL Unit Testing return Error: TypeError: (0 , _reactHooks.useQuery) is not a function
I was trying to write unit test with react component from Apollo Client Unit Testing, but I got an error when mocking call to the GraphQL endpoint to some hook functions.
My unit test implement:
import React from 'react';
import MockedProvider from '@apollo/react-testing';
import renderer from 'react-test-renderer';
import { GET_CHARACTER_DETAILS } from '../../containerHooks/useCharacterDetails';
import { UseCharacterDetails } from '../../containerHooks/useCharacterDetails';
const mocks = [
{
request: {
query: GET_CHARACTER_DETAILS,
variables: {
id: 1,
},
},
result: {
data: {
character: {
id: '1',
name: 'Rick Sanchez',
status: 'Alive',
gender: 'Male',
},
},
},
},
];
it('renders without error', () => {
renderer.create(
<MockedProvider mocks={mocks} addTypename={false}>
<UseCharacterDetails id='1' />
</MockedProvider>
);
});
Here is my Test Component
import gql from 'graphql-tag';
import { useQuery } from '@apollo/react-hooks';
export const GET_CHARACTER_DETAILS = gql`
query CharacterDetails($id: ID!) {
character(id: $id) {
id
name
status
species
type
gender
origin {
name
}
location {
name
}
image
episode {
name
}
created
}
}
`;
export const UseCharacterDetails = ({ id }) => {
const { loading, error, data } = useQuery(GET_CHARACTER_DETAILS, {
variables: { id },
});
return {
data,
error,
loading,
};
};
When I run a test, it throws an error
TypeError: (0 , _reactHooks.useQuery) is not a function
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|