NodeJS Jest模拟Knex事务处理

3ks5zfa0  于 2022-12-03  发布在  Node.js
关注(0)|答案(2)|浏览(184)

我有下面的lambda处理程序来进行单元测试。它使用了一个库@org/aws-connection,其中有一个函数mysql.getIamConnection,它只返回一个knex连接。
编辑:我在帖子的底部添加了mysql.getIamConnection函数
编辑:如果可能的话,我希望只使用Jest来进行测试,除非它变得太复杂
index.js

const {mysql} = require('@org/aws-connection');

exports.handler = async (event) => {
  const connection = await mysql.getIamConnection()

  let response = {
    statusCode: 200,
    body: {
      message: 'Successful'
    }
  }

  try {
    for(const currentMessage of event.Records){
      let records = JSON.parse(currentMessage.body);
      
      await connection.transaction(async (trx) => {
        await trx
            .table('my_table')
            .insert(records)
            .then(() =>
                console.log(`Records inserted into table ${table}`))
            .catch((err) => {
                console.log(err)
                throw err
            })
      })
    }
  } catch (e) {
    console.error('There was an error while processing', { errorMessage: e})

    response = {
      statusCode: 400,
      body: e
    }
  } finally {
    connection.destroy()
  }
  return response
}

我已经写了一些单元测试,我能够模拟connection.transaction函数,但是我在trx.select.insert.then.catch函数上遇到了麻烦。
下面是我的测试文件index.test.js

import { handler } from '../src';
const mocks = require('./mocks');

jest.mock('@org/aws-connection', () => ({
    mysql: {
        getIamConnection: jest.fn(() => ({
            transaction: jest.fn(() => ({
                table: jest.fn().mockReturnThis(),
                insert: jest.fn().mockReturnThis()
            })),
            table: jest.fn().mockReturnThis(),
            insert: jest.fn().mockReturnThis(),
            destroy: jest.fn().mockReturnThis()
        }))
    }
}))

describe('handler', () => {
    test('test handler', async () =>{
      const response = await handler(mocks.eventSqs)
      expect(response.statusCode).toEqual(200)
    });
});

此测试可以部分工作,但它根本不覆盖trx部分。

await trx
        .table('my_table')
        .insert(records)
        .then(() =>
            console.log(`Records inserted into table ${table}`))
        .catch((err) => {
            console.log(err)
            throw err
        })

如何设置我的mock @org/aws-connection,以便它也包含trx函数?
编辑:mysql.getIamConnection

async function getIamConnection (secretId, dbname) {
    const secret = await getSecret(secretId)

    const token = await getToken(secret)

    let knex
    console.log(`Initialzing a connection to ${secret.proxyendpoint}:${secret.port}/${dbname} as ${secret.username}`)
    knex = require('knex')(
        {
            client: 'mysql2',
            connection: {
                host: secret.proxyendpoint,
                user: secret.username,
                database: dbname,
                port: secret.port,
                ssl: 'Amazon RDS',
                authPlugins: {
                    mysql_clear_password: () => () => Buffer.from(token + '\0')
                },
                connectionLimit: 1
            }
        }
    )

    return knex
}

解决方案

@qaismakani的答案对我很有效。我写的稍微有点不同,但回调是关键。对任何感兴趣的人来说,这里是我的最终解决方案

const mockTrx = {
    table: jest.fn().mockReturnThis(),
    insert: jest.fn().mockResolvedValue()
}

jest.mock('@org/aws-connection', () => ({
    mysql: {
        getIamConnection: jest.fn(() => ({
            transaction: jest.fn((callback) => callback(mockTrx)),
            destroy: jest.fn().mockReturnThis()
        }))
    }
}))
dphi5xsq

dphi5xsq1#

将您的mock更新为如下所示可能会达到目的:

const { mysql } = require("@org/aws-connection");

jest.mock("@org/aws-connection", () => ({
    mySql: {
        getIamConnection: jest.fn()
    }
}));

const mockTrx = {
    table: jest.fn().mockReturnThis(),
    insert: jest.fn().mockResolveValue() // Resolve any data here
};

mysql.getIamConnection.mockReturnValue({
    transaction: jest.fn((callback) => callback(mockTrx)),
});

您需要模拟事务,以便它使用伪trx执行回调。为此,您需要确保trx对象中的所有函数都返回对它的引用或promise,以便您可以适当地链接它。

uinbv5nw

uinbv5nw2#

我编写了knex-mock-client,它允许您使用一个简单的API来模拟真实的数据库,而不是模仿knex实现。
使用更改模拟实现

import { handler } from "../src";
import { getTracker } from "knex-mock-client";
const mocks = require("./mocks");

jest.mock("@org/aws-connection", () => {
  const knex = require("knex");
  const { MockClient } = require("knex-mock-client");
  return {
    mysql: {
      getIamConnection: () => knex({ client: MockClient }),
    },
  };
});

describe("handler", () => {
  test("test handler", async () => {
    const tracker = getTracker();
    tracker.on.insert("my_table").responseOnce([23]); // setup's a mock response when inserting into my_table

    const response = await handler(mocks.eventSqs);
    expect(response.statusCode).toEqual(200);
  });
});

相关问题