【问题标题】:Why does Jest needs Babel to test async code?为什么 Jest 需要 Babel 来测试异步代码?
【发布时间】:2019-08-23 02:32:09
【问题描述】:

The Jest "An Async Example" guide 开头是:

首先,在 Jest 中启用 Babel 支持...

但我想知道 Jest 为什么以及在哪里需要 Babel。

【问题讨论】:

    标签: jestjs babeljs


    【解决方案1】:

    Node.js 自 7.6.0 版以来默认支持 async 函数,因此(正如您所怀疑的)Babel 不需要 Jest 使用 async 运行测试功能。

    我刚刚通过安装 only Jest v24.6.0 确认了这一点,并使用 Node.js v10.15.1 运行了这个测试:

    test('hi', async () => {
      const val = await Promise.resolve('hello');
      expect(val).toBe('hello');
    });
    

    ...顺利通过了。


    另一方面,Babel 使用 ES6 模块语法所必需的。

    "An Async Example" 文档中的许多示例都使用 ES6 模块语法(export default ...import * as ... 等),因此任何这些示例都需要 Babel

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 2020-03-01
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 2019-03-07
      • 2023-04-10
      相关资源
      最近更新 更多