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

Blank DB for every single test #173

Closed
PzYon opened this issue Oct 4, 2019 · 6 comments
Closed

Blank DB for every single test #173

PzYon opened this issue Oct 4, 2019 · 6 comments

Comments

@PzYon
Copy link

PzYon commented Oct 4, 2019

I would like to have a clean DB for every single unit test.

I'm using more or less the code from your README. However I'm using beforeEach and afterEach instead of *All. I would have expected the database to be clear after closing the connection in one unit test and creating a new one in the next, but apparently this is not the case.

When rerunning the tests, the first test always starts from a "clean" database. How can I achieve this for every test? At the moment, to achieve what I want, I drop the tables in afterEach, but I'm sure there must be a better approach? You can view me code here.

@aymericbouzy
Copy link

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.

@vladholubiev
Copy link
Member

vladholubiev commented Oct 17, 2019

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

PzYon added a commit to PzYon/engraved_legacy that referenced this issue Oct 17, 2019
@aymericbouzy
Copy link

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.

@RusseII
Copy link

RusseII commented Nov 5, 2019

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

@aymericbouzy
Copy link

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()
})

@MarcLopezAvila
Copy link

MarcLopezAvila commented Nov 10, 2019

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),
    }));
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants