knex迁移导致早期的摩卡测试失败

时间:2019-04-05 17:07:35

标签: node.js mocha knex.js

我正在编写一些摩卡测试,在其中我使用beaforeEach()afterEach()after()钩子中的knex初始化postgress数据库。

这是一个示例测试:

const chai = require('chai');
const should = chai.should();

const knex = require('../../src/server/db/connection').knex;

describe('test01', function() {

  beforeEach( async function() {
    try {
      await knex.migrate.rollback();
      //await knex.migrate.latest();
    } catch (err) {
      throw err;
    }
  });

  afterEach(async function() {
    try {
      await knex.migrate.rollback();
    } catch (err) {
      throw err;
    }
  });

  after(async function() {
    try {
      await knex.destroy();
    } catch (err) {
      console.log(err);
      throw err;
    }
  });

  describe.only('check array', function() {
    it('responds with matching records', async function() {
      const users = [1, 2, 3, 4];
      users.should.have.length(3);
    });
  });
});

如果我像上面写的那样运行测试,它将按预期执行。它失败并显示预期的消息:

  test
    1) check arrays


  0 passing (167ms)
  1 failing

  1) test
       check arrays:

      AssertionError: expected [ 1, 2, 3, 4 ] to have a length of 3 but got 4
      + expected - actual

      -4
      +3

      at Context.<anonymous> (test\unit\auth.test.js:120:23)

但是,如果我尝试执行await knex.migrate.latest()行未完成的迁移,则测试在after()挂钩中失败,并且不显示任何错误消息。输出就是这样:

  test
    1) check arrays


  0 passing (838ms)
  1 failing

  2) "after all" hook

一些重要的细节:

  • 我在Windows 10上
  • knex迁移肯定有效,我可以看到表已正确创建和删除。
  • 我尝试放置一些痕迹,但从未发现任何错误。铝钩正确完成。
  • 我尝试使用Docker在Linux中运行相同的代码,但结果完全相同
  • 我尝试使用.then()/ done()样式代替async / await,结果是相同的。

这是我的package.json:

{
  "engines": {
    "node": "10.7.0",
    "npm": "6.2.0"
  },},
  "dependencies": {
    "bcryptjs": "2.4.0",
    "body-parser": "1.17.2",
    "bookshelf": "0.14.2",
    "bookshelf-modelbase": "2.10.4",
    "cloudinary": "1.9.0",
    "dateformat": "2.0.0",
    "debug": "2.2.0",
    "dotenv": "4.0.0",
    "express": "4.13.4",
    "express-jwt": "5.3.0",
    "expressive-hrbac": "^1.0.1",
    "fs-extra": "4.0.0",
    "joi": "13.4.0",
    "jsend": "1.0.2",
    "jsonwebtoken": "7.4.1",
    "knex": "0.16.3",
    "morgan": "1.7.0",
    "multer": "1.3.0",
    "nunjucks": "2.4.2",
    "passport": "0.3.2",
    "passport-local": "1.0.0",
    "passport-stub": "1.1.1",
    "pg": "7.4.3",
  },
  "devDependencies": {
    "chai": "4.2.0",
    "chai-as-promised": "7.1.1",
    "chai-http": "4.2.1",
    "chai-jwt": "2.0.0",
    "console.table": "^0.10.0",
    "eslint": "5.2.0",
    "gulp": "3.9.1",
    "gulp-eslint": "4.0.0",
    "gulp-jscs": "4.0.0",
    "gulp-jshint": "2.0.1",
    "gulp-nodemon": "2.1.0",
    "gulp-plumber": "1.1.0",
    "jshint": "2.9.3",
    "jshint-stylish": "2.2.1",
    "jwt-simple": "0.5.5",
    "minimist": "^1.2.0",
    "mocha": "5.2.0",
    "mocha-eslint": "5.0.0",
    "mocha-jscs": "5.0.1",
    "mocha-jshint": "2.3.1",
    "pg-format": "^1.0.4",
    "run-sequence": "1.2.2",
    "shelljs": "0.8.1",
    "tiny-lr": "0.2.1"
  },
}

谢谢

编辑2019年4月13日

经过一些调试后,我意识到问题出在由chai检查生成的堆栈跟踪的Mocha报告程序处理中。 Mocha假设error.stack是一个字符串,并且在lib / reporters / base.js中尝试用以下方式拆分它:

    // indent stack trace
    stack = stack.replace(/^/gm, '  ');

没有await knex.migrate.latest()行,因为堆栈确实是一个字符串。看起来像这样:

    at chai.request.post.end (test\integration\routes.user.test.js:152:27)
    at Test.Request.callback (node_modules\superagent\lib\node\index.js:716:12)
    at parser (node_modules\superagent\lib\node\index.js:916:18)
    at IncomingMessage.res.on (node_modules\superagent\lib\node\parsers\json.js:19:7)
    at endReadableNT (_stream_readable.js:1132:12)
    at processTicksAndRejections (internal/process/next_tick.js:76:17)

取消对await knex.migrate.latest()行的注释后,堆栈将更改为数组,如下所示:

[ CallSite {},
  CallSite {},
  CallSite {},
  CallSite {},
  CallSite {},
  CallSite {},
  CallSite {} ]

replace()方法引发引发问题的异常。我想在更高版本的摩卡咖啡中,堆栈的处理也类似,从而导致了问题

0 个答案:

没有答案