Search code examples
javascriptunit-testingmocha.js

What does 'pending' test mean in Mocha, and how can I make it pass/fail?


I am running my tests and noticed:

18 passing (150ms)
1 pending

I haven't seen this before. Previously test either passed, or failed. Timeouts caused failures. I can see which test is failing because it's also blue. But it has a timeout on it. Here's a simplified version:

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!`)
}
  • What does 'pending' mean? How could a test be 'pending' when Mocha has exited and node is no longer running?
  • Why is this test not timing out?
  • How can I make the test pass or fail?

Thanks!


Solution

  • The test had a callback (ie, an actual function, not done) but refactoring the code solved the issue. The issue was how code that expects error should run:

    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!`)
      }
    });