【问题标题】:Jest and Babel transpilation - SyntaxError: Cannot use import statement outside a moduleJest 和 Babel 转译 - SyntaxError:不能在模块外使用 import 语句
【发布时间】:2022-03-09 18:00:42
【问题描述】:

在运行我得到的测试的某些情况下,我很难使用 JEST

测试套件无法运行

...node_modules\p-retry\index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import retry from 'retry';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

    > 1 | import pRetry from 'p-retry';
        | ^
      2 |
      3 | export function Retry(tries: number) {

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (src/common/Retry.ts:1:1)

同时,我的 webpack 构建与 typescript 和 babel 配合得很好。我尝试了很多东西(见下文以使其工作,但到目前为止没有成功 - 并且还没有真正理解发生了什么。从我的观点来看 - 尽管到目前为止对我来说转译的东西是一个黑色区域我尝试让 Jest 使用 ESModules 并提供代码,并尝试提供 commonJS 模块代码。

因此,我正在寻找替代选择和进一步调查的方法。特别是一件事让我觉得很奇怪: 来自错误的 Retry.ts 文件是我的文件之一,它导入了 pRetry(以 ESModule 样式编写的 node_module),它在其代码中从非常错误的第一行。

所以发生在我身上的事情似乎是 pRetry 不是从它的 ESModule 代码转换而来的(pRetry 的源代码以 import retry from 'retry'; 开头),如果我正确解释语法,它只是包装在一些 commonJS 代码中.

所以我接下来的步骤可能是调查 babel-jest 真正生成的内容并检查那里的内容并尝试进一步扣除。有人知道如何实现这一点(尤其是了解 babel-jest 生成的内容)还是有其他想法?

我尝试过的事情 - 都失败了(有时错误略有不同)

  1. 在 babel.config.js 中使用插件:["@babel/plugin-transform-runtime"]
  2. 将 tsconfig.json 中的目标和模块更改为 es5
  3. 在 jest.config.ts 中引入下面的 transformIgnorePatterns:["node_modules/?!(p-retry)"]
  4. 在 jest.config.ts 中使用以下内容

预设:“ts-jest”,
转换: { '^.+\.(ts|tsx)?$': 'ts-jest', "^.+\.(js|jsx)$": "babel-jest"}

或者两个都用 ts-jest 或者两个都用 babel-jest

  1. 按照一篇帖子的建议从 .babelrc 文件迁移到 babel.config.js

  2. AllowJS : true in tscfonfig.json 和 transformIgnorePatterns in jest 组合

  3. 将 ["@babel/plugin-transform-runtime",{"regenerator": true}] 添加到 babel.config

  4. 使用

    预设:“ts-jest”,
    测试环境:“节点”, 转换:{"node_modules/p-retry/.+\.(j|t)sx?$":"ts-jest"},
    transformIgnorePatterns: ["node_modules/(?!p-retry/.*)"]

在 jest.config 中

  1. 在 babel.config 中使用“transform-es2015-modules-commonjs”
  2. 在 babel.config 中使用 @babel/plugin-transform-modules-commonjs
  3. 按照https://stackoverflow.com/questions/35756479/does-jest-support-es6-import-export#:~:text=Jest%20will%20enable%20compilation%20from,json%20.&text=If%20you%20don't%20want%20to%20pollute%20your%20project%20with%20 的建议应用以下步骤
  • 确保不要通过设置转换掉导入语句 转换:{} 在配置文件中
  • 运行节点@^12.16.0 || >=13.2.0 带有 --experimental-vm-modules 标志
  • 使用 jest-environment-node 或 jest-environment-jsdom-sixteen 运行测试。
  1. 在 jest.config.ts 中使用 jest-environment-node、node 或 jsdom 等测试环境

jest-config.ts:

const tsconfig = require("./tsconfig.json");
const moduleNameMapper = require("tsconfig-paths-jest")(tsconfig)

export default {
   collectCoverage: true,
   coverageDirectory: "analysis/coverage",
   coveragePathIgnorePatterns: ["/node_modules/"],
   collectCoverageFrom: ["src/**/*.{js,jsx,ts}"],
   coverageReporters: ["json", "lcov", "text", "clover"],
   coverageThreshold: {
      global: {
         branches: 0,
         functions: 0,
         lines: 0,
         statements: 0
      },
   },
   clearMocks: true,
   coverageProvider: "babel",
   moduleNameMapper,
   roots: ["<rootDir>/src/", "<rootDir>/test/"],
   testEnvironment: 'jest-environment-node', 
   testPathIgnorePatterns: [
      "\\\\node_modules\\\\"
   ],
   "transform": {
      "^.+\\.(js|ts|jsx)$": "babel-jest"
   }
};

babel.config.js:

    module.exports = {
   presets: ['@babel/preset-typescript',
      ['@babel/preset-env', {
         targets: { node: "current" }
      }],
      '@babel/preset-flow',

   ],
   plugins: [["@babel/plugin-transform-modules-commonjs"], ["@babel/plugin-proposal-decorators", { "legacy": true }], ["@babel/plugin-proposal-class-properties"]]
}

从 package.json 中提取

"@babel/core": "^7.16.12",
"@babel/plugin-proposal-decorators": "^7.16.5",
"@babel/plugin-transform-modules-commonjs": "^7.16.8",
"@babel/plugin-transform-runtime": "^7.16.10",
"@babel/preset-env": "^7.14.4",
"@babel/preset-flow": "^7.16.7",
"@babel/preset-typescript": "^7.13.0",
"@babel/runtime": "^7.16.7",
"babel-jest": "^27.4.6",
"babel-plugin-transform-regenerator": "^6.26.0",         
"jest": "^27.0.4",
"jest-config": "^27.4.5",
"jest-esm-transformer": "^1.0.0",
"ts-jest": "^27.1.3",
"tsconfig-paths-jest": "^0.0.1",
"core-js": "^3.20.0",

【问题讨论】:

    标签: jestjs babeljs babel-jest


    【解决方案1】:

    原来我很接近。

    通过添加 esmodules: false 更改 babel.config.ts 就完成了 :-)

    module.exports = {
       presets: ['@babel/preset-typescript',
          ['@babel/preset-env', {
             targets: { esmodules: false, node: "current" }
          }],
          '@babel/preset-flow',
        
       ],
       plugins: [["@babel/plugin-transform-modules-commonjs"], ["@babel/plugin-proposal-decorators", { "legacy": true }], ["@babel/plugin-proposal-class-properties"]]
    }
    

    【讨论】:

      【解决方案2】:

      您可以让 Jest 不要忽略转换 p-retry,方法是将其添加到您的 jest 中。 config.js,它对我有用。

        "transformIgnorePatterns": [
      "node_modules/(?!(p-retry)/)",
      

      ],

      【讨论】:

        猜你喜欢
        • 2020-02-11
        • 1970-01-01
        • 2020-10-08
        • 2021-08-06
        • 1970-01-01
        • 2021-10-15
        • 2020-12-31
        相关资源
        最近更新 更多