Search code examples
node.jsmockingknex.jsnode-oracledb

conn.commitAsync is not a function error when running as transaction


When I run below code I keep getting this error:

TypeError: conn.commitAsync is not a function
    at Oracle_Transaction.commit (/home/foo/node_modules/knex/lib/dialects/oracledb/transaction.js:14:18)
    at Function.transactor.commit (/home/foo/node_modules/knex/lib/transaction.js:277:40)
    at /home/foo/node_modules/knex/lib/transaction.js:191:35

I don't seem to have an issue if I run without a transaction, so I am assuming it is the implicit commits that is choked on. This runs fine regularly btw, it is only when I attempt to mock it fails.

Am I missing something? Is it the connection which is not mocked, how do I mock it, if that is the issue?

import * as knex from 'knex';

export const pool: any = knex({
    acquireConnectionTimeout: 60000,
    client: 'oracledb',
    connection: {
        connectString: 'zoo',
        password: 'foo',
        user: 'bar',
    },
    debug: true,
    pool: {
        acquireTimeoutMillis: 60000, 10),
        idleTimeoutMillis: 60000,
        max: 10,
        min: 10,
    },
});

    await pool.transaction((trx: any) => {
        return Promise.all([
            row = this.insertOne(trx, id, data),
            this.insertTwo(trx, id, data),
            userData.flag ? this.insertThree(trx, id, data) : {},
            this.insertFour(trx, id, data),
            moreToDo(data),
        ]);
    })
    .catch((err: any) => {
        logger.error(err);
    });

each of the insert functions look something like this

    public insertOne(trx: any,  id: number, data: any) {
    return pool('FOOBAR')
            .transacting(trx)
            .withSchema('FOO')
            .insert([
                {
                    FOO: 'BAR',
                    ID: id,
                    CREATE_DT: pool.raw('sysdate'),
                    LAST_MOD_DT: pool.raw('sysdate'),
                },
            ]);
}

It's mocked like this

import * as mockKnex from 'mock-knex';
mockKnex.mock(pool);
const tracker = mockKnex.getTracker();
tracker.install();
    tracker.on('query', (query, step) => {
        [
            function firstQuery() {
                    {
                        ID: 1234,
                    },
                ]);
            },
            function secondQuery() {
                query.response([
                    {
                        ID: 1234,
                    },
                ]);
            },
            function thirdQuery() {
                query.response([]);
            },
            function fourthQuery() {
                query.response([]);
            },
            function fifthQuery() {
                query.response([]);
            },
        ][step - 1]();
    });
    const results = await sut.inserts(subject);
    expect(results).toEqual(foo);

I have tried using nested .then statements as well, like the knex documentation does it with an explicit commit or rollback, same problem.


Solution

  • For anybody running into the same issue, I was able to get this working by adding a jest.spyOn on the rollback and commit function in the Transaction class in oracledb

    // tslint:disable: no-var-requires
    // tslint:disable: no-require-imports
    // tslint:disable-next-line: variable-name
    const Transaction  = require('../../node_modules/knex/lib/dialects/oracledb/transaction.js');
    
    const rollbackMock = jest.spyOn(Transaction.prototype, 'rollback');
    const commitkMock = jest.spyOn(Transaction.prototype, 'commit');
    

    in the beforeEach()

    jest.resetAllMocks();
    rollbackMock.mockImplementation(() => null);
    commitkMock.mockImplementation(() => null);