Jest: Jest and Mongoose not working well together

Created on 2 Mar 2017  ·  13Comments  ·  Source: facebook/jest

Using the latest version of Jest (19.0.2) and Mongoose (4.8.5), the following code is breaking when ran by jest:

const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
  email: {
    type: String,
    unique: true,
  },
});

const User = mongoose.model('User', userSchema);

User.on('index', function(err) {
  console.log('Index creation errored?', err);
});

mongoose.connect('localhost/test123');

Expected result (when ran by Node.js (7.6.0)):

$ node test.js
Index creation errored? undefined

Actual result (when ran by Jest):

$ ./node_modules/.bin/jest --no-cache test.js
 FAIL  ./test.js
  ● Test suite failed to run

    Your test suite must contain at least one test.

      at onResult (node_modules/jest/node_modules/jest-cli/build/TestRunner.js:192:18)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.277s
Ran all test suites matching "test.js".
  console.log test.js:13
    Index creation errored? Error {
      name: 'MongoError',
      message: 'Index keys cannot be empty.',
      ok: 0,
      errmsg: 'Index keys cannot be empty.',
      code: 67,
      codeName: 'CannotCreateIndex' }

I am using the default jest configuration (=> automocking is disabled).

Related: https://github.com/Automattic/mongoose/issues/5033

Needs More Info

Most helpful comment

Try changing testEnvironment to node then

All 13 comments

What does the test look like? Which OS do you use? can you provide a repro?

@thymikee - the test file looks like the code snippet above and it reproduces the problem. I can wrap it inside describe, it and/or beforeAll - the result is the same, database indexes not being created (it should create a unique index for the email field).

I am using MacOS Sierra 10.12.3 (latest). And a clean npm install.

You must wrap it with test or it, otherwise it's not a test. Please provide a repository with repro, otherwise there's little we can do to triage this issue.

I'll create a repository, wrapping it back into a test (I unwrapped it for the sake of simplicity).

@thymikee - there you go: https://github.com/jozsi/bug-jest-mongoose

This is not a bug in Mongoose, nor in Jest. Your test is just wrong.

Please take a look at resources on how test Mongoose. I believe there's no good article on how to pair it with Jest, but there's something for Jasmine: http://www.mattritter.me/?p=1 and a lot for Mocha (which should work for Jest too without many changes), so there's a lot to pick ideas from. Good luck! :)

@thymikee - I am not trying to test the index creation. I do need the index to be created in order for some real world tests to work, but it doesn't because the creation fails in under the jest environment, while works under node, mocha or any other environment.

I'll update the repo with a test that's just not wrong:

  • disable index autocreation
  • manually trigger index creation and test it specifically

Try changing testEnvironment to node then

@thymikee - thank you, setting testEnvironment to node worked!

So I need to provide a separate environment for the Node.js and React parts of my app (I suppose it would be the best to run React tests under jsdom). What could be the reason Mongoose fails with jsdom, and shouldn't it work with it? Would I experience this issue with other Node.js modules as well?

Yea, this is a pain right now and we're working on multi-config runner to solve this exact problem.
I don't know really why jsdom bugs Mongosse, but you can open an issue in jsdom repo and ask there (and come back with the answers)!

@jozsi we landed this PR https://github.com/facebook/jest/pull/2859 so it will be possible to change test environment on the file basis in the next release 🙂

@thymikee - thanks for heads-up!

Meanwhile I've updated the test and opened a jsdom issue - maybe it will resolve the environment inconsistency. Will keep you posted once I hear from them or Mongoose devs.

Mongoose has been fixed (starting v4.9.1) to work correctly with Jest.

The underlying node-mongodb-native driver (fixed in v2.2.25) was conflicting with jsdom when it comes to the global.toString() function: https://github.com/mongodb/node-mongodb-native/pull/1494

Was this page helpful?
0 / 5 - 0 ratings