Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

before hook with promises: TypeError: Cannot read property 'call' of undefined #3080

Closed
caub opened this issue Oct 23, 2017 · 4 comments
Closed

Comments

@caub
Copy link

caub commented Oct 23, 2017

When running mocha test

// test/index.js
const assert = require('assert');
const delay = (x,t) => new Promise(r => setTimeout(r, t, x));

before(delay('lol', 1000));

describe('Great test', () => {
	it('should pass', () => {
		assert(1);
	});

	it('should pass again', async() => {
		const v = await delay('ok', 200);
		assert.equal(v, 'ok');
	});
});

The output is:

  1) "before all" hook: [object Promise]

  0 passing (19ms)
  1 failing

  1) "before all" hook: [object Promise]:
     TypeError: Cannot read property 'call' of undefined

mocha version: 4.0.1

possibly related to #2706, #2315

@caub
Copy link
Author

caub commented Oct 23, 2017

fixed with

before(async () => {
	await delay('lol', 1000);
});

@caub caub closed this as completed Oct 23, 2017
@ScottFreeCode
Copy link
Contributor

Can also be fixed with:

before(function() {
  return delay(...)
})

or

before(delay.bind(null, ...))

(We could really use better error messages in the event that objects such as promises are given where Mocha expects callback functions. I thought we have an existing issue about that, but I haven't found it in a search so far...)

@afrancht
Copy link

afrancht commented Jan 29, 2020

I'm still getting this error:

      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)

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

Any ideas?

@caub
Copy link
Author

caub commented Jan 29, 2020

Why do you post this here? it's a totally different error

email is certainly null at the time this line executes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants