Search code examples
ava

Integration Tests Against a Database - AVA


After writing unit tests, i'm facing with Integration tests, which consist of testing the library against a database (rethinkdb).

Each testcase has to be independent between each other, and the database will be cleared after each test, so that they will not give false positives.

Because of AVA architecture is to run tests in parallel, i found out that I can't achieve positive tests in that manner. ex:

test.beforeEach(async function(t) {

  const users = t.context.users = await DB.models.User.save([
    {name: 'jhon',username: 'doe'},
    {name: 'fabri',username: 'fenos'},
    {name: 'will',username: 'red'},
    {name: 'smith',username: 'blue'},
    {name: 'paul',username: 'orange'},
    {name: 'tesla',username: 'ele'},
  ]);

  t.context.tasks = await DB.models.Task.save([
    {title: 'My task1', description: 'My duty1', assignee_id: _.sample(users).id},
    {title: 'My task2', description: 'My duty2', assignee_id: _.sample(users).id},
    {title: 'My task3', description: 'My duty3', assignee_id: _.sample(users).id},
  ]);
});

test.afterEach(async (t) => {
  return await DB.clearDB();
});

I been forced to use the serial function to allow every test to finish and clear the DB in a serial way.

My tests might update or remove of data which may cause false positives if the test share the same data at same time.

If the tests that i'm writing are just reading data, i could just do the operation of seeding and cleaning just as pre and post script as mentioned in the issue #311 and keep the tests in parallel.

I also found very nice, the way i can use the t.context and pass down users / tasks objects into my tests.

Is it this one, the rare case that we are forced to use serial test execution?

How would you tackle this kind of tests using ava?


Solution

  • Yes, test.serial is meant for this.

    Alternatively if you could use a different database for each test that would allow you to run them in parallel.