[Error: Nock] Mocking "Random User Generator API" HTTP Requests #2487
-
HI, I have built a React App to consume the Random User Generator API, and create a unit testing with React Testing Library, and Jest. Another hand, I used
Then I copy the const headers = {
accept: 'application/json, text/plain, */*',
referer: 'http://localhost/',
'user-agent':
'Mozilla/5.0 (darwin) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/20.0.3',
'accept-language': 'en',
origin: 'http://localhost',
'accept-encoding': 'gzip, deflate',
};
// Mocking the API response `?count=${count}`
nock('https://randomuser.me')
.get('/api')
.query({ count })
.reply(200, { results }, headers); But it did not work. Then, I have tried const headers = {
accept: 'application/json, text/plain, */*',
referer: 'http://localhost/',
'user-agent':
'Mozilla/5.0 (darwin) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/20.0.3',
'accept-language': 'en',
origin: 'http://localhost',
'accept-encoding': 'gzip, deflate',
};
// Mocking the API response `?count=${count}`
nock('https://randomuser.me')
.defaultReplyHeaders({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
})
.get('/api')
.query({ count })
.reply(200, { results }, headers);
}); It still not working. I created a Stackblitz if you want to fork and run the tests. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
[Update] I added a command on
However, the
I am passing the query({ count }) or code block complete: const count = 2;
// ... more code
const headers = {
accept: 'application/json, text/plain, */*',
referer: 'http://localhost/',
'user-agent':
'Mozilla/5.0 (darwin) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/20.0.3',
'accept-language': 'en',
origin: 'http://localhost',
'accept-encoding': 'gzip, deflate',
};
// Mocking the API response: https://randomuser.me/api?count=2
nock('https://randomuser.me')
.defaultReplyHeaders({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
})
.get('/api')
.query({ count })
.reply(200, { results }, headers); I can't understand it didn't work or this error. |
Beta Was this translation helpful? Give feedback.
Yes, you're right!
Also, I figured out that my hook had an error with the url
Thank you so much @mastermatt!