MST

星途 面试题库

面试题:JavaScript中Jest如何对异步函数进行测试?

假设你有一个异步函数`asyncFunction`,它返回一个Promise,该Promise在2秒后resolve并返回一个数字。请使用Jest编写测试用例,验证该函数返回的值大于10。
32.3万 热度难度
编程语言JavaScript

知识考点

AI 面试

面试题答案

一键面试
// 假设asyncFunction定义在asyncFunction.js文件中
const asyncFunction = require('./asyncFunction');

describe('asyncFunction', () => {
  test('should return a value greater than 10', async () => {
    const result = await asyncFunction();
    expect(result).toBeGreaterThan(10);
  });
});