jbrumwell / mock-knex

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

Error when running migrations #130

Closed ZeRego closed 2 years ago

ZeRego commented 2 years ago

I'm trying to use 'mock-knex' for my unit tests but I can't figure out how I can migrate and seed the mocked database.

I keep getting an error when knex is checking the 'knex_migrations_lock' table.

Error message:

Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
    at /node_modules/knex/lib/migrations/migrate/table-creator.js:29:15
    at Object.listCompleted (/node_modules/knex/lib/migrations/migrate/migration-list-resolver.js:12:3)

Code I have:

import knex from 'knex';
import mockDb from 'mock-knex';

const database = knex({
    client: 'pg',
    migrations: {
        directory: './src/database/migrations',
        tableName: 'knex_migrations',
        extension: 'ts',
        loadExtensions: ['.ts'],
    },
    seeds: {
        directory: './src/database/seeds/development',
        extension: 'ts',
        loadExtensions: ['.ts'],
    },
    debug: true,
});

describe('DashboardModel', () => {
    beforeAll(async () => {
        mockDb.mock(database);
        await database.migrate.rollback();
        await database.migrate.latest();
        await database.seed.run();
    });
    afterAll(() => {
        mockDb.unmock(database);
    });

    test('example', async () => {
        ...
    });
});

Am I missing something in the configuration, should I be doing the migration/seed differently ? or is this a limitation/bug ?

jbrumwell commented 2 years ago

@ZeRego are you trying to test your migrations? With mock knex you don't have a physical database running behind it allows you to use query listeners to respond with the data that is expected from the database.

ZeRego commented 2 years ago

Yup, just realised that. Thanks for the reply. Closing this issue.