i'm trying to test with jest this route on my node microservice. At the initalisation of my microservice I need to check that another server is alive. The function is working but i'm stuck at testing it with jest.
Here is the code of my node micro-service:
app.get(/ping, (req, res) => {
axios.get('server.com/health')
.then(() => {
res.sendStatus(200);
})
.catch(() => {
res.sendStatus(503);
});
});
And here is the code of my test server :
import nock from 'nock';
import request from 'supertest';
describe('Liveness and Readiness', () => {
beforeEach(() => {
nock('server.com')
.get('/health')
.reply(200);
});
it('Microservice repond statut code 200 when requested on /ping ', () => {
microService.init();
return request(microService.start())
.get('/ping')
.expect(200);
});
I used nock to mock the server i need to check health from.
The error i get is :
expected 200 "OK", got 500 "Internal Server Error"
Without the code of healthcheck (see below) the test are passing.
app.get(/ping, (req, res) => {
res.sendStatus(200);
});
Even without nock (mean it should ping the real server) and i checked it's live it's still not working. It looks like it don't even try to check the health in the test even if it's mocked. I don't know what to check next to make my test work with this particular case.
Also tried moxios on did not succeed with it either.
I'll be grateful for any help from the community on that matter :)
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)