我有下面的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()
}))
}
}))
2条答案
按热度按时间dphi5xsq1#
将您的mock更新为如下所示可能会达到目的:
您需要模拟事务,以便它使用伪
trx
执行回调。为此,您需要确保trx
对象中的所有函数都返回对它的引用或promise,以便您可以适当地链接它。uinbv5nw2#
我编写了
knex-mock-client
,它允许您使用一个简单的API来模拟真实的数据库,而不是模仿knex实现。使用更改模拟实现