jbrumwell / mock-knex

A mock knex adapter for simulating a database during testing
MIT License
239 stars 71 forks source link

Problems using with Ava #90

Closed throrin19 closed 5 years ago

throrin19 commented 5 years ago

Hello,

I try to use your module with Ava to test the database part of my project but I have a problem :

Ava execute all tests in parallel, and, with this processing, the tracker.on('query') capture query of other test randomly.

jbrumwell commented 5 years ago

Would you be able to use the sql part of the query for uniqueness?

throrin19 commented 5 years ago

I pass ava in --serial and it works fine with mock-knex :)

jbrumwell commented 5 years ago

Glad to hear it @throrin19 :)

throrin19 commented 5 years ago

For example, this is my test to try if My model helper build correctly the queries :

const test          = require('ava');
const tracker       = require('mock-knex').getTracker();

test.beforeEach(() => {
    tracker.install();
});

test.afterEach(() => {
    tracker.uninstall();
});

test('fetchPage without pagination', async (t) => {
    const users = [
        userMock(),
        userMock(),
    ];

    tracker.on('query', (query) => {
        t.is(query.method, 'select');
        t.is(query.sql, 'select `user`.* from `user`');
        query.response(users);
    });

    const response = await modelHelper.fetchPage({
        model   : 'User',
        module  : 'users',
    });

    t.is(response.length, users.length);
    t.falsy(response.data);
    t.not(typeof response.count, 'number');
});