使用Supertest测试Express应用时Jest报500内部服务器错误
Jest + Supertest测试Express应用时出现500内部服务器错误
我在使用Jest结合Supertest测试Express应用时遇到500 Internal Server Error,但应用在浏览器或Postman中可正常运行,怀疑问题出在测试配置但无法定位具体原因。
运行npm test时的错误信息如下:
$ npm test > kino-bio-projekt@0.0.0 test > cross-env NODE_OPTIONS=--experimental-vm-modules jest (node:16440) ExperimentalWarning: VM Modules is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) FAIL tests/movies.test.js × Home page shows list of movies and navigation links (31 ms) ● Home page shows list of movies and navigation links expected 200 "OK", got 500 "Internal Server Error" 33 | ); 34 | > 35 | const response = await request(app).get('/').expect('Content-Type', /html/).expect(200); | ^ 36 | 37 | expect(response.text).toMatch('Pulp Fiction'); 38 | expect(response.text).toMatch('Fire Walk With Me'); at Object.expect (tests/movies.test.js:35:79) ---- at Test._assertStatus (node_modules/supertest/lib/test.js:252:14) at node_modules/supertest/lib/test.js:308:13 at Test._assertFunction (node_modules/supertest/lib/test.js:285:13) at Test.assert (node_modules/supertest/lib/test.js:164:23) at Server.localAssert (node_modules/supertest/lib/test.js:120:14)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 0.617 s, estimated 1 s
Ran all test suites.
已进行的排查操作:
- 正常启动应用后,访问http://localhost:5080可正常显示电影列表;
- 添加全局错误处理器,但测试时控制台未输出错误日志;
- 确认模拟的
fetchAllMovies已被调用并返回正确数据; - 简化测试为仅请求'/',仍出现500错误。
内容的提问来源于stack exchange,提问作者brannar3
相关产品推荐
相关产品推荐

