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?
Hey @fenos. Could you post this question on Stack Overflow instead and tag it with ava? We like to answer, but if we answer here it will just disappear and nobody else will benefit. Make sure to comment the link to the question here.
@sindresorhus Thank you very much for the interest to help me out, I moved the question to StackOverflow 馃憤
https://stackoverflow.com/questions/37047098/integration-tests-against-a-database-ava
thx @fenos for bring this out, I'm facing very similar problem when try to populate/cleanup data with mongoose before/after test. Didn't see an answer on stackoverflow yet, really looking forward to see @sindresorhus or someone could answer it. Thanks in advance 馃槂
@fenos did you ever figure out a good solution for this issue? Running into the same thing.
@sindresorhus any advice?
Thanks, all!
Since AVA is running each test cases concurrently, any test suites based on same db would may or may not cause unpredictable problems. I'm current running my AVA tests with --serial as reference in document.
Hi guys,
Not much has been said on this recently but I just published a package to make this much easier.
It uses the great mongodb-prebuilt library to run in-memory MongoDB servers and you'll get a new test database for each test case.
Check it out if you need a solution:
One note: in each test case you should probably include a test.after.always function to tear down the DB server and clean up temp files. Your OS should eventually clear them up anyway but it's worth noting.
test.after.always('destroy db', t => {
MongoDBServer.tearDown();
})
That looks pretty great @CImrie! Would you be interested in turning it into a recipe? https://github.com/avajs/ava/tree/master/docs/recipes
@novemberborn Sure. I'll try to have a look over the common formats for your recipes this week and put something together :)
Most helpful comment
@novemberborn Sure. I'll try to have a look over the common formats for your recipes this week and put something together :)