Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

How to properly cleanup test database? #1484

Closed
soullivaneuh opened this issue May 26, 2020 · 6 comments
Closed

How to properly cleanup test database? #1484

soullivaneuh opened this issue May 26, 2020 · 6 comments

Comments

@soullivaneuh
Copy link
Contributor

Comment/Problem

Following this article, I wrote this simple service test:

describe('\'users\' service', () => {
  it('registered the service', () => {
    const service = app.service('users');
    expect(service).toBeTruthy();
  });

  it('creates a user', async () => {
    const user = await app.service('users').create({
      email: 'test@example.com',
      password: 'VerySecretP@assw0rd',
    });

    expect(user.password).not.toEqual('VerySecretP@assw0rd'');
  });
});

It works the first time, but not the second time because the user is already on database:

 FAIL  test/services/users.test.ts
  ● 'users' service › creates a user

    Can't insert key test@example.com, it violates the unique constraint

      at _AVLTree.Object.<anonymous>._AVLTree.insert (node_modules/binary-search-tree/lib/avltree.js:273:19)
      at AVLTree.Object.<anonymous>.AVLTree.insert (node_modules/binary-search-tree/lib/avltree.js:307:27)
      at Index.Object.<anonymous>.Index.insert (node_modules/nedb/lib/indexes.js:77:15)
      at Datastore.Object.<anonymous>.Datastore.addToIndexes (node_modules/nedb/lib/datastore.js:180:29)
      at Datastore.Object.<anonymous>.Datastore._insertInCache (node_modules/nedb/lib/datastore.js:402:10)
      at Datastore.Object.<anonymous>.Datastore._insert (node_modules/nedb/lib/datastore.js:348:10)
      at node_modules/nedb/lib/executor.js:40:13
      at Object.process (node_modules/async/lib/async.js:731:21)
      at next (node_modules/async/lib/async.js:728:27)
      at Immediate.<anonymous> (node_modules/async/lib/async.js:24:16)

I can make a workaround by removing the data before running the test suite again but this is still pretty annoying when you want to launch a specific test multiple times.

What is the best practice to automatically cleanup the data after each test? I didn't find any documentation about that.

@daffl
Copy link
Member

daffl commented May 26, 2020

This depends on the database. With NeDB yes, the most reliable way would be removing the data folder by adding shx rm -rf data/ to your test script.

@soullivaneuh
Copy link
Contributor Author

@daffl Yes I'm already doing that:

"clean": "shx rm -rf test/data/",
"jest": "npm run clean && NODE_ENV=test npm run seed && NODE_ENV=test jest --coverage --detectOpenHandles --forceExit"

But how am I supposed to deal with it when I have a single test to run?

npx jest test/services/users.test.ts 

It would be great to have something that "reset" the created data of each test.

This would be the NodeJS equivalent of this great PHP tool : https://packagist.org/packages/dama/doctrine-test-bundle

@soullivaneuh
Copy link
Contributor Author

This also why I have the npm run seed command to generate data before testing.

I would like to get rid of it and see each test load data independently and cleanup.

Linked to memory database would be the best.

Do you see my point of view?

@daffl
Copy link
Member

daffl commented Jun 1, 2020

I do but in my experience it is not a great idea to test against a different database than the one you are running in production or trying to be smarter about bootstrapping a test database. If you are not happy with cleaning up and seeding it on every test run you can still make sure that each test cleans up its own data:

describe('\'users\' service', () => {
  it('registered the service', () => {
    const service = app.service('users');
    expect(service).toBeTruthy();
  });

  it('creates a user', async () => {
    const user = await app.service('users').create({
      email: 'test@example.com',
      password: 'VerySecretP@assw0rd',
    });

    expect(user.password).not.toEqual('VerySecretP@assw0rd'');

    await app.service('users').remove(user._id);
  });
});

@soullivaneuh
Copy link
Contributor Author

Yes, I will maybe do it. But this assume additional repetitive code to produce, I would like to find a more lazy way. 👍

@daffl
Copy link
Member

daffl commented Aug 8, 2020

Imo, tests are the one place where repetitive code is ok. I've seen many a time when someone tried to be lazy and the test code became unmaintainable.

@daffl daffl closed this as completed Aug 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants