在Mocha中,“待定”测试是什么意思,如何使其通过/失败?

mik*_*ana 5 javascript unit-testing mocha.js

我正在运行测试,发现:

18 passing (150ms)
1 pending
Run Code Online (Sandbox Code Playgroud)

我以前没看过 先前的测试通过或失败。超时导致故障。我可以看到哪个测试失败了,因为它也是蓝色的。但是它有一个超时。这是一个简化的版本:

test(`Errors when bad thing happens`), function(){
  try {
    var actual = doThing(option)        
  } catch (err) {
    assert(err.message.includes('invalid'))
  }
  throw new Error(`Expected an error and didn't get one!`)
}
Run Code Online (Sandbox Code Playgroud)
  • “待定”是什么意思?当Mocha退出并且节点不再运行时,如何将测试“挂起”?
  • 为什么此测试不超时?
  • 如何使测试通过或失败?

谢谢!

had*_*dar 8

当我遇到这个问题时,挂起的错误是当我用跳过定义描述测试时,忘记删除它,如下所示:

describe.skip('padding test', function () {
   it('good test', function () {
       expect(true).to.equal(true);
   })
});
Run Code Online (Sandbox Code Playgroud)

并运行它,我得到了输出

Pending test 'good test'
Run Code Online (Sandbox Code Playgroud)

当我在描述测试中删除跳过标志时,它再次工作..

  • 还可以使用“xit()”或“xdescribe()”跳过测试或描述。 (2认同)

Jon*_*der 7

当您无意中it提前关闭测试方法时,Mocha最终会将测试显示为“待处理” ,例如:

// Incorrect -- arguments of the it method are closed early
it('tests some functionality'), () => {
  // Test code goes here...
};
Run Code Online (Sandbox Code Playgroud)

it方法的参数应包括测试功能定义,如:

// Correct
it('tests some functionality', () => {
  // Test code goes here...
});
Run Code Online (Sandbox Code Playgroud)


Mag*_*gus 6

许多测试框架中的一个未决测试是运行程序决定不运行的测试。有时是因为测试被标记为跳过。有时因为测试只是 TODO 的占位符。

对于 Mocha,文档说待定测试是没有任何回调的测试。

你确定你在看好的测试吗?


mik*_*ana 1

测试有一个回调(即,一个实际的函数,未完成),但重构代码解决了问题。问题是预期错误的代码应该如何运行:

test('Errors when bad thing happens', function() {
  var gotExpectedError = false;
  try {
    var actual = doThing(option)       
  } catch (err) {
    if ( err.message.includes('Invalid') ) {
      gotExpectedError = true
    }
  }
  if ( ! gotExpectedError ) {   
    throw new Error(`Expected an error and didn't get one!`)
  }
});
Run Code Online (Sandbox Code Playgroud)