NodeJs / Koa - 单元测试中未定义错误负载

NodeJs / Koa - Error payload not defined inside unit tests

我需要帮助解决一些问题:我使用 https://github.com/mjhea0/node-koa-api 作为基础来开发我的 API。

在routes/movies.js文件中可以看到他使用了:

router.get(`${BASE_URL}/:id`, async (ctx) => {
  try {
    const movie = await queries.getSingleMovie(ctx.params.id);
    if (movie.length) {
      ctx.body = {
        status: 'success',
        data: movie
      };
    } else {
      ctx.status = 404;
      ctx.body = {
        status: 'error',
        message: 'That movie does not exist.'
      };
    }
  } catch (err) {
    console.log(err)
  }
})

制作 GET /movies/id 路由和内部单元测试:

describe('GET /api/v1/movies/:id', () => {
  it('should throw an error if the movie does not exist', (done) => {
    chai.request(server)
    .get('/api/v1/movies/9999999')
    .end((err, res) => {
      // there should an error
      should.exist(err);
      // there should be a 404 status code
      res.status.should.equal(404);
      // the response should be JSON
      res.type.should.equal('application/json');
      // the JSON response body should have a
      // key-value pair of {"status": "error"}
      res.body.status.should.eql('error');
      // the JSON response body should have a
      // key-value pair of {"message": "That movie does not exist."}
      res.body.message.should.eql('That movie does not exist.');
      done();
    });
  });
});

测试错误的id。

我的路由器中有完全相同的代码,但用于我自己的实体 'guilds' :

router.get('/api/v1/guilds/:id', async (ctx) => {
  try {
    const guild = await queries.getOneGuild(ctx.params.id);
    if (guild.length) {
      ctx.body = {
        status: 'success',
        data: guild,
      };
    } else {
      ctx.status = 404;
      ctx.body = {
        status: 'error',
        message: 'That guild does not exist.',
      };
    }
  } catch (err) {
    console.log(err);
  }
});

我的 tests/routes.guilds.test.js 与引用的 github 存储库相同:

describe('GET /api/v1/guilds/:id', () => {
  it('should throw an error if the guild does not exist', (done) => {
    chai.request(server)
     .get('/api/v1/guilds/9999999')
     .end((err, res) => {
       console.log(err);
       should.exist(err);
       // there should be a 404 status code
       res.status.should.equal(404);
       // the response should be JSON
       res.type.should.equal('application/json');
       // the JSON response body should have a
       // key-value pair of {"status": "error"}
       res.body.status.should.eql('error');
       // the JSON response body should have a
       // key-value pair of {"message": "That guild does not exist."}
       res.body.message.should.eql('That guild does not exist.');
       done();
     });
  });
});

我的问题是那一行:

// there should an error
should.exist(err);

在引用的 GitHub 中:err 被设置为一个值,而在我的代码中 err 是未定义的...

我不明白为什么引用的 GitHub 代码作为错误设置为

中的大数据数组
.end((err, res) => {})

而我的甚至没有定义。特别是因为我做同样的事情...

这是我的第一个 Koa / Node.js API 所以我可能错过了一些东西。

提前感谢您的任何意见!

编辑:忘记说了。

ctx.status = 404;
ctx.body = {
  status: 'error',
  message: 'Guild not found.'
};

实际有效,我可以在 Mocha 的单元测试中看到它们的值:

.end((err, res) => {
  console.log(res.status); // Prints 404
  console.log(res.body.message); // Prints 'Guild not found'
});

似乎 Koa / Koa-router 在引用的 Github 存储库中自动将其视为错误并设置了一些东西,而在我的代码中它没有...

编辑2:因为给出了第一个答案,我测试删除引用的GitHub源代码中的try catch块,他们的单元测试仍然有效,所以它与try无关赶上块。

router.get(`${BASE_URL}/:id`, async (ctx) => {
  const movie = await queries.getSingleMovie(ctx.params.id);
  if (movie.length) {
     ctx.body = {
       status: 'success',
       data: movie
     };
   } else {
     ctx.status = 404;
     ctx.body = {
       status: 'error',
       message: 'That movie does not exist.'
     };
   }
});

关于单元测试的相同行为:Err 仍然设置在 routes.movies.test.js

我发现了问题,这个 github 存储库使用 chai-http@3.0.0 而我使用的是 chai-http@4.2.0

在 chai-http@4.0.0 中,如果请求成功,他们会删除 err 参数。

仅保留 chai-http@4.2.0 并删除 should.exists(err)

即可解决问题

感谢:https://github.com/tgriesser/knex/issues/1855