feathersjs / docs

[MOVED] Legacy Feathers documentation
https://crow.docs.feathersjs.com/
MIT License
242 stars 532 forks source link

How to properly cleanup test database? #1484

Closed soullivaneuh closed 4 years ago

soullivaneuh commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

daffl commented 4 years ago

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.