带有Express、Jest和SuperTest的Node.js永不失败
我是新开玩笑和TDD的。请帮帮我! 我使用supertest来请求API,但即使服务器关闭,测试也不会失败。我尝试过使用return或async wait,但没有解决 我在Node.js项目中有以下结构:带有Express、Jest和SuperTest的Node.js永不失败,node.js,express,jestjs,tdd,supertest,Node.js,Express,Jestjs,Tdd,Supertest,我是新开玩笑和TDD的。请帮帮我! 我使用supertest来请求API,但即使服务器关闭,测试也不会失败。我尝试过使用return或async wait,但没有解决 我在Node.js项目中有以下结构: nodemodules src controllers users-controller.js index.js routes.js server.js test user.test.js package.json package.js
nodemodules
src
controllers
users-controller.js
index.js
routes.js
server.js
test
user.test.js
package.json
package.json:
"scripts": {
"test": "jest",
"lint": "eslint src/** test/** --fix",
"start": "node src/server.js",
"jest-watch": "jest --watch"
},
"devDependencies": {
"eslint": "^6.8.0",
"jest": "^25.3.0",
"supertest": "^4.0.2"
},
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1"
}
src/server.js:
const app = require('./index')
app.listen(3001, () => {
console.log('Server running on port 3001')
})
src/index.js:
const express = require('express')
const routes = require('./routes')
const app = express()
app.use(express.json())
app.use(routes)
module.exports = app
src/routes.js
const routes = require('express').Router()
const UserController = require('./controllers/users-controller')
routes.get('/', (req, res) => { res.status(200).send() })
routes.get('/users', UserController.findAll)
routes.post('/users', UserController.create)
module.exports = routes
src/controllers/user-controller.js
module.exports = {
findAll(req, res) {
const users = [
{ name: 'John Doe', mail: 'john@mail.com' }
]
return res.status(200).json(users)
},
create(req, res) {
return res.status(201).json(req.body)
}
}}
test/user.test.js:
const request = require('supertest')
const app = require('../src/index')
test('Should list users', () => {
return request(app).get('/users')
.then(res => {
expect(res.status).toBe(200)
expect(res.body).toHaveLength(1)
expect(res.body[0]).toHaveProperty('name', 'John Doe')
})
})
test('Should insert a user', async () => {
await request(app).post('/users')
.send({ name: 'Walter Mitty', email: 'walter@mail.com' })
.then(res => {
expect(res.status).toBe(201)
expect(res.body.name).toBe('Walter Mitty')
})
})
结果总是一样的:
PASS test / user.test.js
✓ Should list users. (16ms)
✓ Should insert a user. (13ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 0.456s, estimated 1s
Ran all test suites related to changed files.
Watch Usage: Press w to show more.
哦,我明白问题是什么了,虽然不是真正的问题,但这是玩笑和超级测试的正常行为。我会向你解释的。如果Express应用程序未侦听,则不会影响玩笑或超级测试行为。为什么?仅仅因为您传递给supertest
request(app)
的app
运行应用程序的独立进程,并且当测试完成时,该应用程序也会完成。换句话说,Supertest在不同的进程上运行Express应用程序,进行测试并完成该进程
如果您期望另一个响应代码,例如400,则此测试将失败。以下测试肯定会失败:
test('Should list users', async () => {
// First assertion
const response = await request(app)
.get('/users')
.send()
.expect(400) // This will make fail, because the app instance running for test, is working just fine and will return 200, if you expect a different code, so the test will fail
// Second assertion
expect(response.body[0]).toHaveProperty('name', 'John Doe')
})
这是玩笑和超级测试的正常行为。它在另一个独立的进程中运行Express应用程序,只是为了运行测试。Express主进程是否仍在运行或是否已停止并不重要。哦,我知道问题出在哪里了,尽管这不是真正的问题,但这是Jest和Supertest的正常行为。我会向你解释的。如果Express应用程序未侦听,则不会影响玩笑或超级测试行为。为什么?仅仅因为您传递给supertest
request(app)
的app
运行应用程序的独立进程,并且当测试完成时,该应用程序也会完成。换句话说,Supertest在不同的进程上运行Express应用程序,进行测试并完成该进程
如果您期望另一个响应代码,例如400,则此测试将失败。以下测试肯定会失败:
test('Should list users', async () => {
// First assertion
const response = await request(app)
.get('/users')
.send()
.expect(400) // This will make fail, because the app instance running for test, is working just fine and will return 200, if you expect a different code, so the test will fail
// Second assertion
expect(response.body[0]).toHaveProperty('name', 'John Doe')
})
这是玩笑和超级测试的正常行为。它在另一个独立的进程中运行Express应用程序,只是为了运行测试。Express主进程是否仍在运行或是否已停止并不重要。您好!哪些测试应该失败?为什么?你好,andresmunozit!都是。服务器(快车)已关闭!你好哪些测试应该失败?为什么?你好,andresmunozit!都是。服务器(快车)已关闭!“没用,”安德烈斯蒙诺兹特说。我只给你的重构建议留下了一个测试,结果完全一样。console.log显示数组:[{name:'johndoe',mail:'john@mail.com“}”。我已经编辑了我的答案,试图让它足够正确和清晰。希望有帮助。:)哇,原谅我。我想我不是很清楚。我正在使用supertest向服务器发出请求。即使服务器关闭,返回的状态仍然是200。测试没有失败。当您运行测试脚本
npm run test
时,Jest将运行并仅使用在src/index.js
中导出的应用程序,对其运行测试并停止。当你说“Express没有运行”时,你是说这个脚本“start”:“node src/server.js”
没有运行,但这并不重要。Jest在不同的脚本上运行,因此在不同的独立进程上执行测试并退出。Jest不需要运行start
脚本来完成他的任务,因为它在不同的进程上完成任务,甚至使用不同的脚本。在Jest的网站上,它明确表示“通过在自己的进程中运行独立的测试来并行化它们,以最大限度地提高性能”,这是行不通的,@Andresumonozit说。我只给你的重构建议留下了一个测试,结果完全一样。console.log显示数组:[{name:'johndoe',mail:'john@mail.com“}”。我已经编辑了我的答案,试图让它足够正确和清晰。希望有帮助。:)哇,原谅我。我想我不是很清楚。我正在使用supertest向服务器发出请求。即使服务器关闭,返回的状态仍然是200。测试没有失败。当您运行测试脚本npm run test
时,Jest将运行并仅使用在src/index.js
中导出的应用程序,对其运行测试并停止。当你说“Express没有运行”时,你是说这个脚本“start”:“node src/server.js”
没有运行,但这并不重要。Jest在不同的脚本上运行,因此在不同的独立进程上执行测试并退出。Jest不需要运行start
脚本来完成任务,因为它在不同的进程上完成任务,甚至使用不同的脚本。在Jest的网站上,它明确表示“通过在自己的进程中运行独立的测试来并行化测试,以最大限度地提高性能”