33

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!

mikemaccana
  • 110,530
  • 99
  • 389
  • 494

4 Answers4

52

A test can end up being shown by Mocha as "pending" when you inadvertently closed the test's it method early, like:

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

The it method's arguments should include the test function definition, like:

// Correct
it('tests some functionality', () => {
  // Test code goes here...
});
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
  • In my case, I accidentally missed supplying a callback to `it`. The test code was indented to look like it was inside the `it`, but was actually just loose in the `describe`. – Carcigenicate Feb 03 '23 at 22:45
10

when I facing with this issue, the pending error was when I define a describe test with skip, and forgot to remove it, like that:

describe.skip('padding test', function () {
   it('good test', function () {
       expect(true).to.equal(true);
   })
});

and did run it, I got the output

Pending test 'good test'

and when I remove the skip flag on the describe test, it does work again..

hadar
  • 824
  • 2
  • 8
  • 10
6

A pending test in many test framework is test that the runner decided to not run. Sometime it's because the test is flagged to be skipped. Sometime because the test is a just a placeholder for a TODO.

For Mocha, the documentation says that a pending test is a test without any callback.

Are you sure you are looking at the good test ?

Magus
  • 14,796
  • 3
  • 36
  • 51
  • Yes - I;m looking at the right test. I can run `mocha -g 'Errors when bad thing happens'` an only that test will run, and it will result in pending. – mikemaccana Mar 01 '18 at 13:44
  • 1
    you can also ignore suites/tests (make them pending) with 'x'. 'xit' or 'xdescribe'. – john_omalley Mar 01 '18 at 14:30
1

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!`)
  }
});
mikemaccana
  • 110,530
  • 99
  • 389
  • 494