javascript - 由 AWS SDK 引起的环境被破坏后尝试导入的笑话
问题描述
我的应用程序的 Jest 集成测试在本地 DynamoDB 中创建记录,所有测试都通过了,但是在之后的清理中,我收到一条消息说
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.
at Object.userAgent (node_modules/aws-sdk/lib/util.js:34:43)
at HttpRequest.setUserAgent (node_modules/aws-sdk/lib/http.js:111:78)
at new HttpRequest (node_modules/aws-sdk/lib/http.js:104:10)
at new Request (node_modules/aws-sdk/lib/request.js:328:24)
at features.constructor.makeRequest (node_modules/aws-sdk/lib/service.js:202:19)
npm ERR! Test failed. See above for more details.
我有一个 afterAll 设置在每次测试后运行并删除创建的记录,如下所示:
afterAll(async () => {
ECs.forEach(async (EC) => {
await deleteUser(EC);
})
ECs = [];
})
我曾尝试使用假计时器但没有成功。这些测试也都是按顺序运行的。这是其中一项测试的示例:
test("Create new user", async () => {
const johnECInitialRequestParams = {
EC: {
firstName: "John",
email: "john.smith@gmail.com",
},
responsibleFor: "8b8a9eb9-840f-4245-9200-719fe05f6612"
}
const johnECResultStoredObject = {
ECID: "f576d1cb-4df0-4093-9b2d-b94c70a28e18",
firstName: "John",
email: "john.smith@gmail.com",
responsibilities: [
{
greenID: "8b8a9eb9-840f-4245-9200-719fe05f6612",
RID: "8e76e90c-8088-40d3-add3-e39c1d4024d8",
status: "pending"
}
]
}
uuidv4
.mockReturnValue(null) // should only be called twice
.mockReturnValueOnce(johnECResultStoredObject.ECID) // for ECID
.mockReturnValueOnce(johnECResultStoredObject.responsibilities[0].RID) // for RID
ECs.push(johnECResultStoredObject.ECID); // to delete the object after the test execution
try {
await handler(johnECInitialRequestParams, undefined, undefined);
}
catch (err) {
console.error(err);
throw err
}
expect(await getUser(johnECResultStoredObject.ECID)).toMatchObject(johnECResultStoredObject);
});
解决方案
导入后,在测试文件中添加 jest.useFakeTimers()
推荐阅读
- javascript - 异步函数不打印除 null 之外的任何输出
- postgresql - 如何通过 cell_id 按日期(时间戳)查找最大值
- validation - 验证错误后如何在primefaces中输入输入
- javascript - Mongodb find方法不返回任何内容
- javascript - 将 Javascript 变量传递给 PHP
- haskell - 是否不可能在 Traversable 中获取元素的深度?
- azure - 如何设置 Azure CDN
- angular - Springboot / login 控制器从角度应用程序失败
- time - 是否有将分钟数的十进制值转换为持续时间格式的值的公式?
- java - 找不到控制器 @RequestMapping URL 返回 404