当 运行 作为事务时,conn.commitAsync 不是函数错误

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

当我 运行 下面的代码时,我不断收到此错误:

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

如果我 运行 没有事务,我似乎没有问题,所以我假设它是被阻塞的隐式提交。这 运行 经常正常,顺便说一句,只有当我尝试模拟它时才会失败。

我错过了什么吗?是不是未模拟的连接,如果这是问题所在,我该如何模拟它?

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);
    });

每个插入函数看起来像这样

    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'),
                },
            ]);
}

被嘲笑成这样

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);

我也尝试过使用嵌套的 .then 语句,就像 knex 文档使用显式提交或回滚一样,同样的问题。

对于任何 运行 遇到相同问题的人,我能够通过在 oracledb[= 中的事务 class 中的回滚和提交功能上添加 jest.spyOn 12=]

// 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');

在 beforeEach()

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