我有一个小项目here我创建了简单的测试:
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)
异常阻止测试运行。
关于node.js - 未捕获的异常测试 GET 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59199972/