In a React and GraphQL project, I have a mutation which looks like this:
const variables: {
id: faker.random.uuid(),
//other info
}
client.mutate({
mutation: CREATE_PERSON,
variables,
update: updateFct
}).then(result => {
// do something with the result
})
}).catch(error => {
//do something with error
});
and I'm testing it using @apollo/react-testing as described in the official documentation.
The mock I used looks like this:
const mocks = [
{
request: {
query: CREATE_PERSON,
variables: {
id: 'd6d98b88-c866-4496-9bd4-de7ba48d0f52'
// other info
}
},
result: {
data: {
createPerson {
id: 'd6d98b88-c866-4496-9bd4-de7ba48d0f52',
// other info
}
}
}
}
];
I know the source of the problem: the fact that in the production code I'm using a random UUID, when I launch my test I got the famous error:
Error: No more mocked responses for the query...
I'm familiar with solving this error (like taking care of __typename ...etc.) but in this case I know that my problem is related to the use of a random UUID in my variables and I can confirm that by replacing the line
const variables: {
id: faker.random.uuid(),
//other info
}
with
const variables: {
id: 'd6d98b88-c866-4496-9bd4-de7ba48d0f52',
//other info
}
and my test pass (but of course it's not the goal, I need to use a random variable value).
Any feedback or past experience with a such use case?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…