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

Jest and Mongoose not working well together #3045

Closed
jozsi opened this issue Mar 2, 2017 · 14 comments
Closed

Jest and Mongoose not working well together #3045

jozsi opened this issue Mar 2, 2017 · 14 comments

Comments

@jozsi
Copy link

jozsi commented Mar 2, 2017

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: Automattic/mongoose#5033

@thymikee
Copy link
Collaborator

thymikee commented Mar 2, 2017

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

@jozsi
Copy link
Author

jozsi commented Mar 2, 2017

@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.

@thymikee
Copy link
Collaborator

thymikee commented Mar 2, 2017

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.

@jozsi
Copy link
Author

jozsi commented Mar 2, 2017

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

@jozsi
Copy link
Author

jozsi commented Mar 2, 2017

@thymikee
Copy link
Collaborator

thymikee commented Mar 3, 2017

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 thymikee closed this as completed Mar 3, 2017
@jozsi
Copy link
Author

jozsi commented Mar 3, 2017

@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

@thymikee
Copy link
Collaborator

thymikee commented Mar 3, 2017

Try changing testEnvironment to node then

@jozsi
Copy link
Author

jozsi commented Mar 3, 2017

@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?

@thymikee
Copy link
Collaborator

thymikee commented Mar 3, 2017

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)!

@thymikee
Copy link
Collaborator

thymikee commented Mar 6, 2017

@jozsi we landed this PR #2859 so it will be possible to change test environment on the file basis in the next release 🙂

@jozsi
Copy link
Author

jozsi commented Mar 6, 2017

@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.

@jozsi
Copy link
Author

jozsi commented Mar 19, 2017

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: mongodb/node-mongodb-native#1494

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants