mocha: Uncaught TypeError: Cannot read property 'currentRetry' of undefined

Tested on Mocha 2.5.3.

When uncaught error is thrown during “before” phase additional “Uncaught TypeError: Cannot read property ‘currentRetry’ of undefined” error is thrown from mocha.

Simple test case:

'use strict';

describe('test', function () {
    before(function () {
        require('http').createServer().listen(1);
    });

    it('something', function () {
    });
});

output:

  test
    1) "before all" hook

  2) Uncaught error outside test suite

  0 passing (27ms)
  2 failing

  1) test "before all" hook:
     Uncaught Error: listen EACCES 0.0.0.0:1
      at Object.exports._errnoException (util.js:953:11)
      at exports._exceptionWithHostPort (util.js:976:20)
      at Server._listen2 (net.js:1240:19)
      at listen (net.js:1289:10)
      at Server.listen (net.js:1385:5)
      at Context.<anonymous> (test/test.js:5:40)

  2) test Uncaught error outside test suite:
     Uncaught TypeError: Cannot read property 'currentRetry' of undefined

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 19 (11 by maintainers)

Commits related to this issue

Most upvoted comments

@ljharb @1999 A PR was sent (#2439) that needs some review.

Hey guys! 🙂

Just a quick thought: If this is in the context of async setup code, shouldn’t we then use the done callback to tell Mocha about that? 🤔

I mean something like this:

'use strict';

describe('test', function () {
    before(function (done) {       // <--------------- added the `done` argument
        setImmediate(function () {
            throw Error('error');
            done();
        });
    });

    it('something', function () {
    });
});

In this case it the failure message seems to be more clear:

    ~/tmp/mocha ɀ  mocha 2315.js

  test
    1) "before all" hook

  0 passing (20ms)
  1 failing

  1) test "before all" hook:
     Uncaught Error: error
      at Error (native)
      at Immediate._onImmediate (2315.js:6:19)


    ~/tmp/mocha ɀ  mocha --version
2.5.3
    ~/tmp/mocha ɀ  node --version
v6.2.2
    ~/tmp/mocha ɀ  

Which makes me think that this may be categorized as a slight misuse instead of a bug. What do you guys think? 🤓

@boneskull @ksuszka @1999

I’m still running into this issue although it has said to be fixed. I’m running mocha 6.2.2 though. Any help?

      describe('sign up as a user when an account for that user already exists', () => {
            before(async () => {
                await createTestUser();
            });
            it.only('should not let me signup as a user if an account with that email exists.', done => {
                const { email, authKey } = user;

                chai.request(app)
                    .post('/auth/user/signup')
                    .type('application/json')
                    .send({ email, authKey})
                    .end(function(err, res) {
                        expect(res).to.have.status(200);
                        assert.deepEqual(res.body, {
                            message: 'That username already exists.',
                        });

                        done();
                    });
            });

            after(async () => {
                  await deleteTestUserByEmail(user.email) 
            });
        });

Error

1) Auth Routes Tests
       /auth/signup route
         sign up as a user when an account for that user already exists
           "before all" hook:
     Uncaught TypeError: Cannot read property 'email' of null
      at /Users/aaa/git/webapp_ps/tests/routes/authRoutes.test.js:9:3126
      at /Users/aaa/git/webapp_ps/node_modules/mongoose/lib/model.js:4791:16
      at /Users/aaa/git/webapp_ps/node_modules/mongoose/lib/query.js:4389:12
      at model.Query.Query._completeOne (node_modules/mongoose/lib/query.js:2073:12)
      at Immediate.<anonymous> (node_modules/mongoose/lib/query.js:2135:10)
      at Immediate.<anonymous> (node_modules/mquery/lib/utils.js:116:16)
      at processImmediate (internal/timers.js:456:21)

Function being called in before:

const createTestUser = async () => {
    try {

        const newUser = new User();
        userId = newUser._id;
        newUser.email = user.email;
        newUser.hashedPassword = await newUser.hashPassword(
            user.authKey.hashHex);
        newUser.type = user.type;
        newUser.mukSalt = user.mukSalt;
        newUser.authSalt = user.authSalt;
        await newUser.save();
        return userId;
    }catch(e) {
        console.log('>>>>>>>>>>>>Error', e)
    }
};

Assume user exists as I can console log it and the test is running and using it.

Any ideas?

@ljharb If I had to guess, somewhere between 0 and 7 days from now.