I am currently using node-fetch
and nock
for an express server that sits on top of an angular project.
I have the following middleware that is making a call to an api:
export const middleware = async(response: any) => {
try {
const result = await fetch(url).then(res => res.json())
return response.status(200).json(result);
} catch(err) {
logger.error({eventId: 'get-content', err});
return response.status(500)
}
}
and my test is as follows:
describe('API service', () => {
let response, scope;
beforeEach(() => {
response = {
status(s) { this.statusCode = s; return this; },
json(result) { this.res = result; return this; },
};
})
afterEach(() => {
nock.restore();
})
it('should return a 200 response from successful call api', (done) => {
scope = nock(url)
.get(/.*/)
.reply(200, {data: 'content'})
middleware(response).then(data => {
expect(response.status).toEqual(200);
expect(response.data).toEqual('content');
scope.isDone();
done();
})
})
})
However, nock is not mocking the data
response from the middleware function. Instead, I'd have to use scope
to access its parameters.
The middleware function acts as if nock never mocked its response. Why is this occurring? Am I missing a configuration?
I am serving my tests using karma runner.
I am currently using node-fetch
and nock
for an express server that sits on top of an angular project.
I have the following middleware that is making a call to an api:
export const middleware = async(response: any) => {
try {
const result = await fetch(url).then(res => res.json())
return response.status(200).json(result);
} catch(err) {
logger.error({eventId: 'get-content', err});
return response.status(500)
}
}
and my test is as follows:
describe('API service', () => {
let response, scope;
beforeEach(() => {
response = {
status(s) { this.statusCode = s; return this; },
json(result) { this.res = result; return this; },
};
})
afterEach(() => {
nock.restore();
})
it('should return a 200 response from successful call api', (done) => {
scope = nock(url)
.get(/.*/)
.reply(200, {data: 'content'})
middleware(response).then(data => {
expect(response.status).toEqual(200);
expect(response.data).toEqual('content');
scope.isDone();
done();
})
})
})
However, nock is not mocking the data
response from the middleware function. Instead, I'd have to use scope
to access its parameters.
The middleware function acts as if nock never mocked its response. Why is this occurring? Am I missing a configuration?
I am serving my tests using karma runner.
Share edited Feb 21, 2019 at 1:18 Alejandro asked Feb 21, 2019 at 1:10 AlejandroAlejandro 6372 gold badges9 silver badges26 bronze badges2 Answers
Reset to default 3Nock works by overriding Node's http.request function. Also, it overrides http.ClientRequest too to cover for modules that use it directly.
- https://github./nock/nock#how-does-it-work
Unfortunately it appears fetch
does not make use of the http.request
or http.ClientRequest
meaning the requests are never intercepted by nock
.
A better approach may be to mock fetch
with a library such as fetch-mock
.
I have tried the same & it is working fine for me. Can you check the below piece of code & try.
const fetch = require('node-fetch');
const nock = require('nock');
describe('My Fetch POST Test with Nock', () => {
it('should mock a POST fetch call using nock', async () => {
// Set up the nock mock for the URL and the POST request
const requestBody = { key: 'value' };
const scope = nock('https://api.example.')
.post('/data', requestBody)
.reply(201, { message: 'Created successfully' });
// Perform the fetch call
const response = await fetch('https://api.example./data', {
method: 'POST',
body: JSON.stringify(requestBody),
headers: {
'Content-Type': 'application/json',
},
});
const data = await response.json();
// Ensure all expectations of the nock mock are satisfied
scope.done();
});
});