使用 async/await 对我的 Express 控制器进行单元测试时玩笑超时
因此,使用 supertest 和 jest,在测试我的简单控制器时,我收到一条超时消息。我不确定我是否在玩笑设置的配置中遗漏了某些内容?
app.ts
import express from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import { UserRoute } from './components/user'
const App = () => {
const app = express()
app.use(bodyParser.json({ limit: "50mb" }))
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }))
app.use(cors())
app.use('/api/auth', UserRoute)
return app
}
export default App
user.controller.ts
export const SignUp = async (req: Request, res: Response, next: NextFunction) => {
res.json({ message: "pass!" });
}
user.route.ts
import { Router } from 'express'
import { SignUp } from './user.controller'
const router = Router()
router
.route('/signup')
.get(SignUp)
export default router
user.controller.spec.ts
import App from '../../../app'
import supertest from 'supertest'
const request = supertest(App);
describe('Testing user controller', () => {
it('testing /signup', async () => {
const res = await request.get("/signup");
expect(res.body.message).toBe("pass!");
})
})
我的笑话超时是 10000,不确定这里缺少什么导致我的控制器不返回?可能与异步/等待有关?
So, using supertest and jest, when testing my simple controller I get a timeout message. I'm not sure if I am missing something from the config for jest set-up?
app.ts
import express from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import { UserRoute } from './components/user'
const App = () => {
const app = express()
app.use(bodyParser.json({ limit: "50mb" }))
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }))
app.use(cors())
app.use('/api/auth', UserRoute)
return app
}
export default App
user.controller.ts
export const SignUp = async (req: Request, res: Response, next: NextFunction) => {
res.json({ message: "pass!" });
}
user.route.ts
import { Router } from 'express'
import { SignUp } from './user.controller'
const router = Router()
router
.route('/signup')
.get(SignUp)
export default router
user.controller.spec.ts
import App from '../../../app'
import supertest from 'supertest'
const request = supertest(App);
describe('Testing user controller', () => {
it('testing /signup', async () => {
const res = await request.get("/signup");
expect(res.body.message).toBe("pass!");
})
})
My jest timeout is 10000, not sure what is missing here for my controller not to return? Potentially to do with async/await?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论