Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

Using random variables in mocked apollo react testing generates "Error: No more mocked responses for the query" error

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...