Mongodb-memory-server: Cannot insert nor find by query

Created on 19 Dec 2018  路  3Comments  路  Source: nodkz/mongodb-memory-server

Hello, I tried running the Jest example from README, it works pretty fine but whenever I try running Model.create() or/and running Model.find({_id: 'some-id'}) I got the following error:

TypeError: Cannot read property 'Decimal128' of null

      at Object.<anonymous> (node_modules/mongoose/lib/types/decimal128.js:13:44)
      at Object.<anonymous> (node_modules/mongoose/lib/utils.js:7:17)
      at Object.<anonymous> (node_modules/mongoose/lib/statemachine.js:8:15)
      at Object.<anonymous> (node_modules/mongoose/lib/internal.js:7:22)

this happens only with finding by the _id attribute but inserting new docs always throw that error and here's the example Im using

import mongoose from 'mongoose';
import MongoMemoryServer from 'mongodb-memory-server';

// May require additional time for downloading MongoDB binaries
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;

let mongoServer;

beforeAll(async () => {
  mongoServer = new MongoMemoryServer();
  const mongoUri = await mongoServer.getConnectionString();
  await mongoose.connect(mongoUri, {useNewUrlParser:true}, (err) => {
    if (err) console.error(err);
  });
});

afterAll(() => {
  mongoose.disconnect();
  mongoServer.stop();
});

describe('...', () => {
  it("...", async () => {
    const User = mongoose.model('User', new mongoose.Schema({ name: String }));
    const newUser = await User.create({ name: 're' });
    expect(newUser).toHaveProperty('name', 're');
  });
});

Most helpful comment

@nodkz its a jest error I have solved it by removing "resetModules": true from my Jest config.

All 3 comments

It's mongoose error (not in this package).
Didn't meet it in our projects, so have no idea how it can be fixed.

Maybe somebody else solve it?
Please share a solution.

@nodkz its a jest error I have solved it by removing "resetModules": true from my Jest config.

apologize for the misunderstanding I thought its something related to here, closing.

Was this page helpful?
0 / 5 - 0 ratings