普通转换运行后如何转换模块 [英] How to transform module after ordinary transform has run in jest

查看:125
本文介绍了普通转换运行后如何转换模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用玩笑来测试React组件.其中一些组件使用OpenLayers(ol软件包v5.2.0).在ol软件包v4中,我应用了transformIgnorePatterns来转换ol软件包:

Testing React components with jest. Some of these components make use of OpenLayers (ol package v5.2.0). In ol package v4 I applied transformIgnorePatterns to have the ol package transformed:

"jest": {
    "transformIgnorePatterns": [
         "node_modules/(?!(ol)/)"
    ],
    (...)
}

现在运行NODE_ENV=test jest时出现以下错误:

Now I get the following error when running NODE_ENV=test jest:

(...)
(...)/node_modules/ol/index.js:5
export {default as AssertionError} from './AssertionError.js';
^^^^^^

SyntaxError: Unexpected token export

  14 |     let map = new Map({
  15 |       layers: options.layers,
> 16 |       target: 'map',
     |           ^
  17 |       view: options.view,
  18 |       controls: options.controls
  19 |     });

我在.babelrc中应用了以下预设和插件:

I have applied the following presets and plugins in .babelrc:

"presets": [
   ["@babel/preset-env", {
     "modules": false
    }
   ],
   "@babel/preset-react"
],
"plugins": [
    "lodash",
    ["@babel/plugin-transform-runtime", {
      "corejs": 2,
      "helpers": true,
      "regenerator": true,
      "useESModules": false
    }
  ],
  "@babel/plugin-transform-modules-commonjs",
  "@babel/transform-arrow-functions",
  "@babel/plugin-syntax-dynamic-import",
  "@babel/plugin-syntax-import-meta",
  ["@babel/plugin-proposal-class-properties", {"loose": false}],
  "@babel/plugin-proposal-json-strings"],
"env": {
    "production": {},
    "development": {},
    "test": {
      "presets": [
        ["@babel/preset-env"],
        "@babel/preset-react"
      ]
    }
  }

通过应用global-transform选项构建应用程序时,解决了类似的问题( https://github.com/browserify/browserify#usage )cf.此问题线程: https://github.com/openlayers/openlayers/issues/8497.

A similar problem is solved when building the application by applying the global-transform option (https://github.com/browserify/browserify#usage) cf. this issue thread: https://github.com/openlayers/openlayers/issues/8497.

$ browserify -g [ babelify --presets [ \"@babel/preset-env\" ] --ignore [ \"//node_modules/(?!ol/)/\" ] ] ./src/index.js -o ./public/js/bundle.js

我想对ol模块应用类似的转换,但是不确定如何实现. cf.transformIgnorePatterns用于解决此问题. https://github.com/facebook/jest/issues/2550

I want to apply a similar transformation to the ol module but unsure on how to approach it. The transformIgnorePatterns used to solve this problem cf. https://github.com/facebook/jest/issues/2550

推荐答案

如果将 .babelrc 的内容放入 babel.config.js ,则不再发生错误在项目的根目录中.

The error no longer occurs if I put the contents of .babelrc into babel.config.js in the root of the project.

module.exports = {
    (...)
};

cf.

  • https://github.com/facebook/jest/issues/6229
  • https://github.com/facebook/jest/issues/6053#issuecomment-383632515

副作用-TypeError: $export is not a function加载通过browserify/babelify构建的包时,但我想这是一个配置问题.

side effect - TypeError: $export is not a function when loading the bundle built via browserify/babelify, but I guess it is a configuration problem.

编辑:通过将插件"@ babel/plugin-transform-runtime"的选项更改为:

Edit: Side effect was solved for me by changing the options for the plugin "@babel/plugin-transform-runtime" to:

{
  "corejs": false,
  "helpers": false,
  "regenerator": true,
  "useESModules": false
}

cf. https://stackoverflow.com/a/39502362/3798151 并安装@ babel/runtimecf. https://babeljs.io/docs/en/babel-plugin-transform-运行时

cf. https://stackoverflow.com/a/39502362/3798151 and installing @babel/runtime cf. https://babeljs.io/docs/en/babel-plugin-transform-runtime

这篇关于普通转换运行后如何转换模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆