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.
Aucun commentaire:
Enregistrer un commentaire