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

MongoError differ between mongodb-memory-server and vanilla mongod #78

Closed
lirbank opened this issue Sep 18, 2018 · 5 comments
Closed

MongoError differ between mongodb-memory-server and vanilla mongod #78

lirbank opened this issue Sep 18, 2018 · 5 comments

Comments

@lirbank
Copy link

lirbank commented Sep 18, 2018

First off, I totally love mongodb-memory-server - it makes testing models and GraphQL APIs a breeze!

If I log the errmsg prop of a MongoError like this:

try {
  await col.insertOne(doc);
} catch (error) {
  if (error typeof MongoError && error.code === 11000) {
    console.log(error.errmsg);
  }
}

I get this with mongodb-memory-server (Jest):

E11000 duplicate key error dup key: { : "YF2dqu11Z9mj", : "test" }

And this with a vanilla mongod process (Node):

E11000 duplicate key error collection: myDatabase.myCollection index: organizationId_1_name_1 dup key: { : "YF2dqu11Z9mj", : "test" }

I parse errmsg to figure out which index clashed so I can provide a more detailed error message to the client. But since the index part of errmsg is not available in the MongoError from mongodb-memory-server I can't write any tests for it.

Is there any way around this? Could it be that Jest is the reason for this?

@nodkz
Copy link
Collaborator

nodkz commented Sep 19, 2018

I think that this difference in error messages depends on mongodb version.

If your testing and production versions of mongodb are the same, then you need to try make storageEngine the same. By default it is in memory.

So if it helps, please put here your solution for future googlers.

PS. You may spin app for some tests regular mongo instance, but for other tests - default in memory for spead.

@lirbank
Copy link
Author

lirbank commented Sep 19, 2018

Ok, cool! I have the same version but not the same engine. I'm going to try that. Keep you posted.

@lirbank
Copy link
Author

lirbank commented Sep 19, 2018

Confirming that changing the engine to wiredTiger (same as for production) gives the correct errmsg .

new MongodbMemoryServer({
  binary: {
    version: '4.0.2',
  },
  instance: {
    // Available engines:
    // storageEngines: [ 'devnull', 'ephemeralForTest', 'mmapv1', 'wiredTiger' ],
    storageEngine: 'wiredTiger',
  },
});

@lirbank lirbank closed this as completed Sep 19, 2018
@lirbank
Copy link
Author

lirbank commented Apr 22, 2019

For future googlers:

I've published a package that enables you to set the storage engine per test file in Jest. It also enables you to keep a single mongodb-memory-server server running between tests when you run Jest in watch mode, and other good stuff.

https://www.npmjs.com/package/jest-environment-mongodb (docs here)
https://www.npmjs.com/package/jest-environment-mongodb-wiredtiger
https://www.npmjs.com/package/jest-environment-mongodb-ephemeral

@nodkz
Copy link
Collaborator

nodkz commented Apr 22, 2019

@lirbank wow great!

Please open Pull Request for README.md - add links to your packages under Jest section.
Thanks!

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

2 participants