Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.93k stars 3.84k forks source link

Mongoose mocha asynch test #3478

Closed MMMalik closed 9 years ago

MMMalik commented 9 years ago

The following code works fine with mongoose@4.0.0 (i.e. throws an error):

var mongoose = require("mongoose"),
  User = require("../models").User;

mongoose.connect("mongodb://localhost/test");

describe("#create()", function () {
  it("should create a new user", function (done) {
    var u = {
      name: {
        firstName: "foo",
        lastName: "bar"
      },
      email: "email@foo.com",
      password: "hashed12345",
      credentials: "baz"
    };
    User.create(u, function (err, created) {
      throw new Error("foooooooooo");
      done();
    });
  });
});

The same code with mongoose >=4.1.0 hangs mocha:

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

I was running Mocha 2.3.3.

vkarpov15 commented 9 years ago

Should have been fixed re: #3222. I'll double-check. In the meantime, can you clarify which version of mongoose?

MMMalik commented 9 years ago

I have noticed this issue with the latest 4.1.11, so I kept downgrading. With mongoose 4.1.0 still no success, but 4.0.0 works fine. I am currently working with mongoose 3.8.6 and mocha works as I expect.

Ks89 commented 8 years ago

It's still happening with mongoose 4.6.5 and mocha 3.1.2. So frustrating, because I cannot run my tests on both MacOS and travis ci. I'm using mongo db 3.2.10 and I chose 'bluebird' as recommended.

var mongoose = require( 'mongoose' );
mongoose.Promise = require('bluebird');
var dbURI = 'mongodb://localhost/test-db';
mongoose.connect(dbURI);

This problem it's happening with gulp-mocha, but also simply with mocha.

this is my packages.json:

"engines": {
    "node": "~6.7.0",
    "npm": "~3.10.3"
  },
  "dependencies": {
    "async": "^2.1.2",
    "bcrypt-nodejs": "0.0.3",
    "bluebird": "^3.4.6",
    "body-parser": "^1.15.2",
    "compression": "^1.6.2",
    "connect-redis": "^3.1.0",
    "csurf": "^1.9.0",
    "dotenv": "^2.0.0",
    "express": "^4.14.0",
    "express-content-length-validator": "^1.0.0",
    "express-session": "^1.14.0",
    "gulp": "github:gulpjs/gulp#4.0",
    "helmet": "^2.3.0",
    "hpp": "^0.2.1",
    "jsonwebtoken": "^7.0.1",
    "mongoose": "^4.6.5",
    "morgan": "^1.7.0",
    "nodemailer": "^2.6.4",
    "nodemailer-stub-transport": "^1.0.0",
    "passport": "^0.3.2",
    "passport-facebook": "^2.1.1",
    "passport-github": "^1.1.0",
    "passport-google-oauth": "^1.0.0",
    "passport-linkedin-oauth2": "^1.4.1",
    "passport-local": "^1.0.0",
    "passport-twitter": "^1.0.4",
    "redis": "^2.6.2",
    "request": "^2.72.0",
    "serve-favicon": "^2.3.0",
    "underscore": "^1.8.3",
    "winston": "^2.2.0"
  },
  "devDependencies": {
    "browser-sync": "^2.17.5",
    "chai": "^3.5.0",
    "del": "^2.2.1",
    "gulp": "github:gulpjs/gulp#4.0",
    "gulp-babel": "^6.1.2",
    "gulp-cached": "^1.1.0",
    "gulp-concat": "^2.6.0",
    "gulp-eslint": "^3.0.1",
    "gulp-istanbul": "^1.1.1",
    "gulp-jasmine": "^2.4.2",
    "gulp-jshint": "^2.0.1",
    "gulp-mocha": "^3.0.1",
    "gulp-nodemon": "^2.2.1",
    "gulp-npm-files": "^0.1.3",
    "gulp-remember": "^0.3.1",
    "gulp-sourcemaps": "^2.2.0",
    "gulp-util": "^3.0.7",
    "jshint": "^2.9.4",
    "multer": "^1.2.0",
    "nock": "^8.0.0",
    "npm-check": "^5.4.0",
    "protractor": "^4.0.9",
    "rewire": "^2.5.2",
    "supertest": "^2.0.1",
    "through2": "^2.0.1",
    "yargs": "^6.3.0"
  }

Please help me.

Thank u.

Ks89 commented 8 years ago

If you can't re-open this issue I'll create a new one.

Ks89 commented 8 years ago

I don't understand. I updated nodejs to 7.0.0 and it seems to be ok.

vkarpov15 commented 7 years ago

@Ks89 please open a new issue with some more relevant code samples. Your package.json doesn't really help much.

Ks89 commented 7 years ago

Hi @vkarpov15 thank you for your interest.

I forgot to say that I fixed this problem.

When I was running my application with NODE_ENV=development/production everything was ok. Instead, with NODE_ENV=test/ci using MOCHA, there was a problem.

I discovered that the problem was related to express.static (probably), because specifying a wrong path in express.static and running my app with gulp/nodemon, everything was ok. Instead, running my tests with mocha creates this problem (express.static with a wrong path (for instance a path with a compiled angualr2 application)). The result was an exception similar to this one, but created by other reasons.

Probably, this explanation is very bad. I think that this git diff is better: https://github.com/Ks89/My-MEAN-website/commit/2511b6216de1b8752affd6d29fd818a17bc8c041

Which library generates this problem? I really don't know. In my opinion, this is a crazy issue and probably it's not related to mongoose.

Do you have any ideas? Is it a well-known problem?

Thank u, Ks89

vkarpov15 commented 7 years ago

No idea, would have to dig into your code more to figure out what's going on.