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.
Nock works by overriding Node's http.request function. Also, it overrides http.ClientRequest too to cover for modules that use it directly.
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.