Mocha:在嵌套描述中跳过测试

时间:2017-01-23 14:55:41

标签: node.js tdd mocha

我有以下代码:

const assert = require('assert')

describe('server', function() {
  before(function() {
    // HACK: skip the tests in staging environment until we find to provide db in it
    if(process.env.NODE_ENV === 'staging') {
      this.skip();
    }
  });

  it('list request', function() {
    assert.fail('fails wo db')
  })
  describe('detail requests', function() {
    it('some arguments', function() {
      assert.fail('fails wo db')
    })
  })
})

当我运行NODE_ENV='staging' npm test时:

> @ test /Users/kharandz/Projects/mocha-bug
> mocha



  server
    - list request
    detail requests
      1) some arguments


  0 passing (10ms)
  1 pending
  1 failing

  1) server detail requests some arguments:
     AssertionError: 'fails wo db' undefined undefined
      at Context.<anonymous> (test/sample.spec.js:16:14)

但我希望所有测试都被跳过。所以,问题是:

  • 如何在不对每个描述中的代码进行复制的情况下实现预期的行为?
  • 有什么理由让它像这样工作吗?

1 个答案:

答案 0 :(得分:1)

Mocha似乎不支持它,它被视为&#34;功能&#34;。

请参阅:https://github.com/mochajs/mocha/issues/2683

可以通过

解决
before(function () {
  this.test.parent.pending = true;
  this.skip();
});