【发布时间】:2026-02-21 17:30:01
【问题描述】:
背景
我正在开发一个使用 Serverless 框架和 serverless-appsync-plugin 构建的项目。我实现了一个端点(AWS Lambda)来处理 Appsync 通过 graphQL 生成的所有请求。端点会将请求路由到相应的函数来执行操作。
问题
现在我已经开发了大约 10 多个操作,我想自动化单元测试的过程。为简单起见,我决定在本地将单个端点作为 lambda 运行以进行所有测试(而不是运行 appsync-offline)。
所以,我使用 lambda-local 和 mocha。但是,根据我从 lambda 得到的响应,我无法让测试用例失败。
it('db should have a user of uId: 123', async function () {
lambdaLocal.execute({
event: makeUserEvent({userId: '123'}),
callbackWaitsForEmptyEventLoop: false,
lambdaPath,
callback: function(err, data) {
if(err) {
console.log(1)
expect.fail(null, null, 'You are not supposed to be here') //should fail here
} else {
console.log(2)
// some checking here, may fail or may not fail
expect.fail(null, null, 'return fail if the userId is 234') //should fail here too
}
}
});
console.log(3)
})
在这两种情况下,我都希望它失败,callback('failed', null) 或 callback(null, 'success') 的测试用例都不会失败。
那么,让 lambda-local 测试用例失败的正确方法是什么?
【问题讨论】:
标签: unit-testing lambda aws-lambda mocha.js