我有以下 lambda (aws) 代码:
exports.APILambda = (databaseConnection) => {
return function (event, context, callback) {
context.callbackWaitsForEmptyEventLoop = false
const connection = databaseConnection()
connection.then((connection, query, args) => {
connection.query(queries(query, args), args)
.then(result => {
console.log(result)
connection.end()
callback(null, { data: result })
})
.catch(err => {
throw err
})
})
.catch(err => {
logger.error(`${err.stack}`)
callback(err)
})
}
}
databaseConnection是一个 mariaDb 连接,实现为:
function databaseConnection () {
return mariadb.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
port: '3306',
database: 'test'
})
}
我的测试是:
describe('on new query', () => {
it('returns data', async (done) => {
await runLambda(
['person', ['John']],
(query) => {
expect(query).toHaveBeenCalledWith(
expect.arrayContaining(['person', ['John']])
)
}, done)
})
})
对于该测试,我必须编写以下函数:
async function runLambda (args, assertions, done) {
const query = jest.fn(args => {
return Promise.resolve({
'name': 'John',
'lastName': 'Wayne'
})
})
const onNewQuery = APILambda(async () => ({ query }))
const event = {}
const context = {}
await onNewQuery(
event,
context,
(err, result) => {
if (err) done(err)
assertions(query)
done(result)
}
)
}
但是当我运行它时我遇到了麻烦,它不断抛出:TypeError: connection.end is not a function或者说args未定义。
有人可以阐明如何正确地嘲笑这一点吗?
慕仙森
相关分类