【问题标题】:How to handle it block when it performs testing on async function?对异步函数执行测试时如何处理阻塞?
【发布时间】:2019-07-18 16:57:36
【问题描述】:

我正在尝试测试 it 块内的异步功能。但是,它总是给我一个警告,说明 -> 测试运行完成后一秒钟 Jest 没有退出。我还提到了关于堆栈溢出的类似问题,但它并没有解决我的问题。我也试过npm test -- --detectOpenHandles(也不起作用)。

user_model.js ​

static async createUser(req_body) {
        return {
            user_full_name: req_body.user_full_name,
            user_email: req_body.user_email,
            user_password: await bcrypt.hash(req_body.user_password, await bcrypt.genSalt(10)),
            user_phone: req_body.user_phone,
            user_account_created_date: new Date(),
            user_account_last_modified: new Date(),
            user_status: await userSchema.user_status.default(),
            user_permission_level: await userSchema.user_permission_level.default()
        };
    }

user_model.test.js ​

describe("Create User Object", () => {
    const User = {
        user_full_name: "abcd",
        user_email: "abcd@xyz.com",
        user_password: "abcd$123",
        user_phone: "1234567891"
    };
​
    it("should return a User Object with hashed password", () => {      
​
        return UserModel.createUser(User).then(result => {
            expect(result.user_password).not.toEqual(User.password);
        });
​
    });
})

【问题讨论】:

  • 当你返回一个promise时不需要done,所以当你不使用awaitdone时,只需从参数中删除async
  • 是的,我已经删除了异步,但它仍然显示相同的警告@AZ_
  • 你能用 catch 块测试吗?
  • 你能建议我如何为这个测试用例构建 try catch 吗? @AZ_
  • return UserModel.createUser(User).then(......).catch(err => expect not to get error) 这应该不是问题,因为您没有收到被拒绝的承诺错误,请尝试一下。

标签: javascript node.js jestjs


【解决方案1】:

请尝试调用 done();如下所示。

describe("Create User Object", () => {
const User = {
    user_full_name: "abcd",
    user_email: "abcd@xyz.com",
    user_password: "abcd$123",
    user_phone: "1234567891"
};
it("should return a User Object with hashed password", done => {      
    UserModel.createUser(User).then(result => {
        expect(result.user_password).not.toEqual(User.password);
        done();
    });
});
})

【讨论】:

  • 你能分享一下userSchema mock吗?
  • 我正在通过传递一个虚拟数据用户来验证 createUser()。我没有创建任何模拟。
  • 你能试试吗:user_password: bcrypt.hash(req_body.user_password, bcrypt.genSalt(10))
  • 尝试删除异步。但问题仍然存在
【解决方案2】:

您错过了呼叫完成,下面的代码将起作用。我建议从测试用例中删除 async and done ,它会起作用

describe("Create User Object", () => {
    const User = {
        user_full_name: "abcd",
        user_email: "abcd@xyz.com",
        user_password: "abcd$123",
        user_phone: "1234567891"
    }; ​
    it("should return a User Object with hashed password",(done) => {       ​
        return UserModel.createUser(User).then(result => {
        expect(result.user_password).not.toEqual(User.password); 
        done();
       });  })

【讨论】:

  • 您是否尝试过从测试用例中删除异步并完成
【解决方案3】:

也可以尝试使用 async/await 语法

describe("Create User Object", () => {
    const User = {
        user_full_name: "abcd",
        user_email: "abcd@xyz.com",
        user_password: "abcd$123",
        user_phone: "1234567891"
    };
​
    it("should return a User Object with hashed password", async () => {      
​
        const result = await UserModel.createUser(User)

        expect(result.user_password).not.toEqual(User.password);
    });
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    相关资源
    最近更新 更多