Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...