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?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…