在使用 chai-http 和 Jest 进行测试时,我如何 return 来自 api 的响应
How can I return the response from my api when testing with chai-http and Jest
我正在尝试使用以下代码测试我的节点 api 索引端点
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)
})
})
- 我对 'index route test' 的失败测试通过了,但它应该会失败(已解决)。
- 接收到的响应未定义(我在控制台中登录)。
我的输出如下:
输出
> 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?
如何return回复?
通过将应用 url 传递给服务器变量,我没有得到任何响应。因此,我将语法更改为 ES6 导入而不是 require,并使用应用程序模块而不是应用程序服务器的 url,如 chai-http 文档 here
所以我的代码结构转变为
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()
})
})
})
这样,我可以捕获对 运行 检查的响应。我的测试通过了并且是必需的。输出为:
> 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?
我正在尝试使用以下代码测试我的节点 api 索引端点
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)
})
})
- 我对 'index route test' 的失败测试通过了,但它应该会失败(已解决)。
- 接收到的响应未定义(我在控制台中登录)。
我的输出如下:
输出
> 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?
如何return回复?
通过将应用 url 传递给服务器变量,我没有得到任何响应。因此,我将语法更改为 ES6 导入而不是 require,并使用应用程序模块而不是应用程序服务器的 url,如 chai-http 文档 here
所以我的代码结构转变为
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()
})
})
})
这样,我可以捕获对 运行 检查的响应。我的测试通过了并且是必需的。输出为:
> 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?