Skip to content

[Question] Database integration tests #807

@fenos

Description

@fenos

Hi, thanks for this brilliant test runner. Once i read about it, I just went ahead to install it and write tests for my new library.

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions