jest在环境被破坏后尝试导入,这是由aws sdk引起的

s5a0g9ez  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(282)

我的应用程序的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(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);
});

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题