DEV Community

Discussion on: HTTP mocking for your Tests in React

Collapse
 
harkinj profile image
harkinj

Great article - does the nock 'mock' only last for the duration of the single test ? e..g

it('renders without crashing', () => {
const scope = nock('myapi.com')
.get('/products')
.reply(200, { products: [{ id: 1, name: 'nocked data' }] },
{
'Access-Control-Allow-Origin': '*',
'Content-type': 'application/json'
});
const div = document.createElement('div');
ReactDOM.render(, div);
ReactDOM.unmountComponentAtNode(div);
});

if I added another test after that would the call to myapi.com/products go through nock without having to set it up?
Thanks
Jojm

Collapse
 
softchris profile image
Chris Noring

It was a while ago since I wrote this..
If my memory serves me correct I believe a nock is considered used as soon as it is hit.
So you need to define a new nock definition... I'm gonna test it to make sure.. Thanks for the question