【问题标题】:Uncaught exception testing GET resource未捕获的异常测试 GET 资源
【发布时间】:2020-03-30 16:04:25
【问题描述】:

我有一个小项目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 testpackage.json 中的定义:"test": "ava tests/**"

知道为什么我会得到异常而不是“测试通过”吗?

【问题讨论】:

    标签: node.js express testing jestjs


    【解决方案1】:

    该输出中也应该有一个堆栈跟踪,我明白了:

    ❯ 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)
    

    异常是阻止测试运行。

    【讨论】:

    • 谢谢马克。我编辑了这个问题 - 我从一个使用 jest 而不是 ava 的项目中复制了测试。节点测试初学者在这里。目标是测试 GET 资源 users。我看到了异常,您知道使用express 不会出现此错误的正确方法是什么吗?
    猜你喜欢
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    相关资源
    最近更新 更多