elliotf / mocha-mongoose

test helpers for using mongodb with mocha
MIT License
107 stars 21 forks source link

`Error: timeout of 2000ms exceeded.` for `test.js` in existing project #17

Closed bartvanderwal closed 8 years ago

bartvanderwal commented 8 years ago

Great repo!

However, can you tell me what I'm doing wrong here? I added the tests from /example/test.js file from the README.md into a new /test/test.js file to some test project. But when I then run the test I get an error:

Barts-MacBook-Pro:hottowel bart$ npm test test/test.js

> hottowel@0.0.0 test /Users/bart/Desktop/Code/hottowel
> mocha --recursive "test/test.js"

  Example spec for a model
    1) "before each" hook: clearDB for "can be saved"

  0 passing (2s)
  1 failing

  1)  "before each" hook: clearDB for "can be saved":
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

It fails in the first test, or even before it, in the before when opening connection. Increasing timeout makes no difference.

When I clone this exact repo instead and run the test, it works fine (using the same mongodb url; (mongodb://localhost/<somedb>):

Barts-MacBook-Pro:mocha-mongoose bart$ npm test example/test.js

> mocha-mongoose@1.1.1 test /Users/bart/Desktop/Code/mocha-mongoose
> mocha --recursive "example/test.js"

  Example spec for a model
    ✓ can be saved
    ✓ can be listed
    ✓ can clear the DB on demand

  3 passing (145ms)

What am I doing wrong? I'd like to add mongoose unit tests to a production app, but am pretty sure I'll get the same error there.

Note: I also checked that all required packages of mocha-mongoose were in the other project with same version:

chai@3.4.1
├── kerberos@0.0.17 extraneous
├── mocha@2.3.4
├── mongodb@2.1.3
├── mongoose@4.3.4
└── nodemon@0.9.17
```bash
elliotf commented 8 years ago

I'm not sure, but https://github.com/elliotf/mocha-mongoose/blob/4039af6a980ca5983c3a103d1689503db4a1c3ad/example/test.js#L6 might be the problem. It works in the repo because require('../index') will resolve to https://github.com/elliotf/mocha-mongoose/blob/4039af6a980ca5983c3a103d1689503db4a1c3ad/index.js which is the mocha-mongoose library.

If you copy it to a different place, require('../index') will likely yield something other than mocha-mongoose. If you change require('../index') to require('mocha-mongoose') (after installing mocha-mongoose) then the test should work correctly.

bartvanderwal commented 8 years ago

Thanks for the quick reply @elliotf! :+1:

I got my hopes up, but checked my code and it doesn't use index, so they went down somewhat. As I mentioned I had copied the code from the README.md instead of the actual test.js file. There alread mocha-mongoose is used instead of index in the require.

However I then decided to copy over the test.js and do your fix. And now it works! Evidently there is some difference between the file. I'm now gonna play Sherlock and do some diffing to see what it is, cause I can't spot it with naked eye.

bartvanderwal commented 8 years ago

Well, I don't know. There is no difference, README.md is fine!

I did see that I had renamed the Dummy model in the test code to Seller. I just renamed it again after I had it working in the original situation. Now it's still working after, so I think I missed something with that small refactoring causing it to behave like that.

Anyway thanks for the quick help!

elliotf commented 8 years ago

You're very welcome! Please feel free to close this ticket if/when you like.