【问题标题】:jest says cannot import outside a module开玩笑说不能在模块外导入
【发布时间】:2020-06-11 20:02:05
【问题描述】:

我正在尝试使用测试驱动开发 (TDD) 实现一些功能

我正在用 javascript 编写代码。

checkTransparency(urlString)
使透明(urlString)
是我正在尝试测试和开发的两个功能,它们位于一个名为 transcript.js 的文件中。 这些使用 inkscape 和 graphicsmagick npm。我检查了我的其他项目中的 checkTransparent 工作,但我试图确保我可以将这个 transparent.js 复制粘贴到另一个项目中并在其他地方使用它。

我的项目文件夹结构如下:

+ node_modules
+ src
--- transparent.js
+ test
--- transparent.spec.js
+ package.json
+ package-lock.json
+ jest.config.js

我使用 jest 作为我的测试框架。 问题是当我运行 jest(或 npm 测试)
我得到以下信息:

失败测试/transparent.spec.js
● 测试套件无法运行

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

\\..............\transparent\test\transparent.spec.js:4 <FEW DETAILS OMITTED HERE DELIBERATELY>
import { checkTransparency, makeTransparent } from "../src/transparent"; // const transparent = require("../src/transparent");
^^^^^^

SyntaxError: Cannot use import statement outside a module

  at Runtime._execModule (C:/Users/Kjeong/AppData/Local/Yarn/Data/global/node_modules/jest-runtime/build/index.js:988:58)

测试套件:1 个失败,总共 1 个 测试:共 0 快照:共 0 个 时间:0.862s 运行所有测试套件。

我的 jest.config.js:

module.exports = {
  testEnvironment: "node",
  moduleDirectories: ["node_modules", "src", "transparent"],
  moduleFileExtensions: [
    "js",
    "json",
    "jsx",
    "ts",
    "tsx",
    "node"
  ],
  clearMocks: true,

}

我已经尝试了以下导出来让这个东西正常工作:

export function checkTransparency(urlString) { ... }
export function makeTransparent(urlString) {... }
module.exports = {
     checkTransparency: checkTransparency,
     makeTransparent: makeTransparent,
};

【问题讨论】:

    标签: javascript import jestjs


    【解决方案1】:

    在您的package.json 中,使用如下配置可以解决您的问题:

    {
        "name": "<blah blah>",
        "version": "1.0.0",
        "description": "",
        "type": "module",
        "scripts": {
            "start": "node server.js",
            "test": "node --experimental-vm-modules node_modules/.bin/jest"
        },
    }
    

    【讨论】:

      【解决方案2】:

      如果您真的想使用import 关键字,那么您可能需要关注these explanations。否则为什么不直接require

      const { checkTransparency, makeTransparent } = require('../src/transparent')
      

      希望这会有所帮助:)

      【讨论】:

      • 谢谢。这解决了一个问题,但随后 transparent.js 出现了问题。它有 const gm = require ('gm'),它使用 graphicsmagick npm,并抱怨它。
      猜你喜欢
      • 2020-09-17
      • 2018-09-14
      • 2021-08-14
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 2019-05-08
      • 2017-09-13
      相关资源
      最近更新 更多