首页 > 解决方案 > 未捕获的异常测试 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/**"

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

标签: node.jsexpresstestingjestjs

解决方案


该输出中也应该有一个堆栈跟踪,我得到了这个:

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

例外是阻止测试运行。


推荐阅读