【问题标题】:Getting `SyntaxError: Cannot use import statement outside a module` when running Jest运行 Jest 时获取`SyntaxError: Cannot use import statement outside a module`
【发布时间】:2021-11-04 22:40:20
【问题描述】:

我正在尝试使用 Jest 运行测试,因此我在测试文件的顶部导入以下内容:

import { describe, it, expect } from '@jest/globals'

但是,当我运行测试时,我收到以下错误:SyntaxError: Cannot use import statement outside a module

我该如何解决这个问题?

【问题讨论】:

    标签: javascript unit-testing import jestjs syntax-error


    【解决方案1】:

    我想出了如何解决它。相反,只需使用顶部语句的 CommonJS 形式,如下所示:

    const { describe, it, expect } = require('@jest/globals')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-21
      • 2020-08-30
      • 2022-11-26
      • 2022-11-21
      • 2022-11-23
      • 2021-07-26
      • 2021-08-11
      相关资源
      最近更新 更多