node.js - 未捕获的异常测试 GET 资源
问题描述
我在这里有一个小项目,我创建了简单的测试:
const request = require('supertest');
const bodyParse` = require('body-parser');
const express = require('express');
const app = express()
.use(bodyParser.json());
require('../routes')(app);
test('users',async () => {
await request(app)
.get('/users')
.expect(200);
});
当我执行时,npm test
我得到:
No tests found in tests/users.test.js
1 uncaught exception
npm test
中的定义package.json
:"test": "ava tests/**"
知道为什么我会得到异常而不是“测试通过”吗?
解决方案
该输出中也应该有一个堆栈跟踪,我得到了这个:
❯ npm t
> alpha-node@0.0.0 test /private/var/folders/2_/qczp184x76b2nl034sq5hvxw0000gn/T/tmp.PCqu6DgMB6/node-alpha
> ava tests/**
✖ No tests found in tests/users.test.js
1 uncaught exception
Uncaught exception in tests/users.test.js
/private/var/folders/2_/qczp184x76b2nl034sq5hvxw0000gn/T/tmp.PCqu6DgMB6/node-alpha/node_modules/express/lib/router/index.js:635
TypeError: Cannot read property 'apply' of undefined
node_modules/express/lib/router/index.js:635:15
next (node_modules/express/lib/router/index.js:210:14)
Function.handle (node_modules/express/lib/router/index.js:174:3)
router (node_modules/express/lib/router/index.js:47:12)
Object.<anonymous> (tests/users.test.js:8:1)
例外是阻止测试运行。
推荐阅读
- airflow - GCP Composer/Airflow 调用 Dataflow/beam throwing 错误
- typescript - 如何防止对象被扩展?
- javascript - 如何在引用嵌套函数内部参数的函数内部使用条件语句在 javascript 中返回结果?
- c# - “setup.exe”不是内部或外部命令、可运行程序或批处理文件 - Visual Studio 安装程序
- mysql - MySQL 从 table01 中选择 table02 上不存在的行
- python - Apache Avro - 将序列化数据写入流式传输
- python - 在将 Google Maps API 与 Python 结合使用时,distance_matrix 是否需要任何特定的导入命令?
- swift - 用户文档目录的 URL 为空
- mule - Mule esb:“滚动”日志文件是默认行为吗?
- bash - 在运行 bash 脚本的一个阶段失败后,Jenkins 管道未执行下一阶段