node.js - 未捕获的异常测试 GET 资源

标签 node.js express testing jestjs

我有一个小项目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/

相关文章:

node.js - Graphql/Apollo 错误有时输出数据 : null sometimes data: { function: null}

javascript - nodejs 不读取 JSON

javascript - 如何在 express、couchDB 和 node.js 中使用 session

java - 我可以为这个程序编写什么断言测试用例?

testing - FIFO信号量测试

node.js - Ubuntu 上的 Nodejs 和 npm 安装

javascript - 在 Node.js 中限制请求

JavaScript/Node.js : Why prototype. __proto__ = prototype 而不是 new

node.js - 如何生成刷新 token ?

c++ - C/C++调试原理/核心话题