in Education by
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)

1 Answer

0 votes
by
Here is the solution: app.js: import express from 'express'; import axios from 'axios'; const app = express(); app.get('/ping', (req, res) => { axios .get('/health') .then(() => { res.sendStatus(200); }) .catch((err) => { res.sendStatus(503); }); }); export default app; app.test.js: import nock from 'nock'; import request from 'supertest'; import axios from 'axios'; import app from './app'; jest.unmock('axios'); const host = 'http://server.com'; axios.defaults.baseURL = host; describe('Liveness and Readiness', () => { it('Microservice repond statut code 200 when requested on /ping ', (done) => { nock(host) .get('/health') .reply(200); request(app) .get('/ping') .expect(200, done); }); it('Microservice repond statut code 503 when requested on /ping ', (done) => { nock(host) .get('/health') .reply(503); request(app) .get('/ping') .expect(503, done); }); }); Integration test result with 100% coverage: PASS src/stackoverflow/55302401/app.test.js Liveness and Readiness ✓ Microservice repond statut code 200 when requested on /ping (42ms) ✓ Microservice repond statut code 503 when requested on /ping (11ms) ----------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | ----------|----------|----------|----------|----------|-------------------| All files | 100 | 100 | 100 | 100 | | app.js | 100 | 100 | 100 | 100 | | ----------|----------|----------|----------|----------|-------------------| Test Suites: 1 passed, 1 total Tests: 2 passed, 2 total Snapshots: 0 total Time: 4.146s, estimated 9s Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/55302401

Related questions

0 votes
    I've been putzing with trying to call the jobs.Insert bigquery rest api endpoint with node (the jobs. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    The napi_create_date function appeared in Node.js starting with 11.11.0 version. https://nodejs.org/api/n- ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I'm trying to debug my nodejs app using node-inspector. But Google Chrome doesn't show the code. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I have been looking at using the ssh2 module for sftp shipping of logs. However the cloud service hosting ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    so i'm reviewing and practicing making rest api with node mongoose and express. I'm having problem making ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I'm trying to use node to print the Serial from Arduino uno connected to USB I have a file script ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    Quick Summary of my question: Does Gulp 3.6.0 dest() handle glob-base the same way as 4.0.0? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    What I'm trying to do here is use the 2 functions but I don't know how to use both of them ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    What I'm trying to do here is use the 2 functions but I don't know how to use both of them ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I'm used to v3 node botbuilder sdk so I have a middleware where I look at the dialog stack and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    What is a Node?...
asked Nov 20, 2020 in Education by Editorial Staff
0 votes
    I'm trying to make a form-maker with Node.Js and MongoDB but confused about the data structure. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Does server-side rendering with react-loadable work with Webpack 4 and Babel 7? I've been unable to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    Hi I'm using ffmpeg to concatenate several mp4 files that I downloaded. Usually they don't have the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Here is my data: { { "_id" : ObjectId("623a92139e19f99295167786"), "headline" : "headline1", " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
...