Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

node.js - How can I return the response from my api when testing with chai-http and Jest

I am trying to test my node api index endpoint with the code below

index.test.js

const chai = require('chai')
const chaiHttp = require('chai-http')
const server = 'http://localhost:8000'

chai.use(chaiHttp)

describe('set up test', () => {
 it('set up test', () => {
   expect(1).toEqual(1)
 })
})

describe('index route test', () => {
 it('index route test', (done) => {
  const { res } = chai
    .request(server)
    .get('/')
    .end((res) => {
      expect(res.statusCode).toBe(200)
      done()
    })
  console.log(res)
 })

})

  1. My fail test for 'index route test' passes yet it's supposed to fail(SOLVED).
  2. The Received response is undefined(which I logged in the console).

My output is as below:

Output

> jest --forceExit || true

FAIL  tests/index.test.js
 set up test
   ? set up test (6 ms)
 index route test
   ? gives welcome message (49 ms)

 ● index route test ? gives welcome message

   expect(received).toBe(expected) // Object.is equality

   Expected: 200
   Received: undefined

     18 |       .get('/')
     19 |       .end((res) => {
   > 20 |         expect(res.statusCode).toBe(200)
        |                                ^
     21 |         done()
     22 |       })
     23 |     console.log(res)

     at tests/index.test.js:20:32
     at Test.Object.<anonymous>.Request.callback (node_modules/superagent/lib/node/index.js:728:3)
     at ClientRequest.<anonymous> (node_modules/superagent/lib/node/index.js:647:10)

 console.log
   undefined

     at Object.<anonymous> (tests/index.test.js:23:13)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        1.63 s, estimated 2 s
Ran all test suites.
Force exiting Jest: Have you considered using `--detectOpenHandles` to detect async operations that kept running after all tests finished?

How can I return the response?

question from:https://stackoverflow.com/questions/65598586/how-can-i-return-the-response-from-my-api-when-testing-with-chai-http-and-jest

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I wasn't getting any response by passing the app url to the server variable. So I changed my syntax to ES6 import instead of the require and used the app module rather than the url for the application server as in the chai-http documentation here

So my code structure transitioned to

import chai from 'chai'
import chaiHttp from 'chai-http'
import server from '../app'

chai.use(chaiHttp)

describe('set up test', () => {
  it('set up test', () => {
   expect(1).toEqual(1)
  })
})

describe('index route test', () => {
  it('gives welcome message', (done) => {
    chai
     .request(server)
     .get('/')
     .then((res) => {
       expect(res.statusCode).toBe(200)
       done()
     })
   })
})

This way, I can capture the response to run checks on it. And my tests pass and required. The output is:

> jest --forceExit || true

PASS  tests/index.test.js
 set up test
   ? set up test (4 ms)
 index route test
   ? gives welcome message (77 ms)

 console.log
   Application Server is up and running on port 8000

     at Server.<anonymous> (app.js:43:11)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        3.189 s
Ran all test suites.
Force exiting Jest: Have you considered using `--detectOpenHandles` to detect async operations that kept running after all tests finished?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...