GithubHelp home page GithubHelp logo

Comments (6)

RusseII avatar RusseII commented on September 3, 2024 2

I agree and @aymericbouzy I have the same issue. Any ideas on how to fix it?

from jest-mongodb.

MarcLopezAvila avatar MarcLopezAvila commented on September 3, 2024 1

Same issue, I moved to mongodb-memory-server and everything works fine.

const startDB = () => new Promise((resolve) => {
  const mongoServer = new MongoMemoryServer();
  mongoServer
    .getConnectionString()
    .then(mongoUri => resolve({
      mongoServer,
      db: MongoDB.connect(mongoUri),
    }));
});

from jest-mongodb.

aymericbouzy avatar aymericbouzy commented on September 3, 2024

I have the same issue. Why not having a separate database for each test suites (with a auto generated name) ? This kind of setup has worked well for me in the past.

from jest-mongodb.

vladholubiev avatar vladholubiev commented on September 3, 2024

I clean db collections in beforeEach as well. There were a couple of reasons behind keeping current behavior.

Changing default behavior to clean everything before each test doesn't leave a chance to preserve documents between tests if users wish so. If you want to have a clean state, it's a matter of 1 line of code: await db.collection('name').deleteMany({})

But I'm not against this approach by any means, so PRs are welcome:) Meanwhile, I'll add some explanations to the README

from jest-mongodb.

aymericbouzy avatar aymericbouzy commented on September 3, 2024

I can open a separate issue for this, but when running tests in parallel with asynchronous code, cleaning the db in beforeEach does not work.

from jest-mongodb.

aymericbouzy avatar aymericbouzy commented on September 3, 2024

What I do in my own setup (not using this package) is using a fresh db in each test suite. I have something along these lines in my setup file :

beforeAll(async () => {
  await connect(
    `mongodb://localhost:27017/test_${randomToken()}`
  )
})

afterEach(async () => {
  await clear()
})

afterAll(async () => {
  await disconnect()
})

from jest-mongodb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.