-
Notifications
You must be signed in to change notification settings - Fork 532
How to properly cleanup test database? #1484
Comments
This depends on the database. With NeDB yes, the most reliable way would be removing the data folder by adding |
@daffl Yes I'm already doing that:
But how am I supposed to deal with it when I have a single test to run?
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 |
This also why I have the 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? |
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);
});
}); |
Yes, I will maybe do it. But this assume additional repetitive code to produce, I would like to find a more lazy way. 👍 |
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. |
Comment/Problem
Following this article, I wrote this simple service test:
It works the first time, but not the second time because the user is already on database:
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.
The text was updated successfully, but these errors were encountered: