首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在jest和中使用d3.js v7?

如何在jest和中使用d3.js v7?
EN

Stack Overflow用户
提问于 2021-09-13 13:17:40
回答 1查看 783关注 0票数 6

我有一些使用D3.jsv6.7.0的react项目,我可以在windows 10上用jest 27.2.0测试它。

我在jest.config.js中设置的一部分

代码语言:javascript
复制
testEnvironment: "jsdom",
testRunner: "jest-jasmine2",
transform: {
    '^.+\\.jsx?$': 'babel-jest',
},
transformIgnorePatterns: [
     "\\\\node_modules\\\\"
],

我在webpack.config.js中设置的一部分

代码语言:javascript
复制
module: {
    rules: [
      {
        test: /\.(jsx|js)$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
      },

如果我将d3.js升级到使用模块的7.0.1版本,则在运行测试时会出现以下错误:

代码语言:javascript
复制
Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • 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/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    C:\python_env\workspace\visualization\frontEnd\node_modules\d3\src\index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from "d3-array";

=>如何调整我的配置以使我的测试再次运行?

( a)我尝试使用修改过的webpack测试脚本和交叉env启用实验节点特性:

代码语言:javascript
复制
cross-env NODE_OPTIONS=--experimental-vm-modules jest

( b)我试着修改transformIgnorePatterns的笑话

代码语言:javascript
复制
transformIgnorePatterns: [
     "/node_modules/(?!(d3.*))"
  ],

( c)我试图为webpack babel-加载器包括节点_模块/d3。

但是,我还没有找到一个工作配置。

相关:

https://github.com/nrwl/nx/issues/812

https://github.com/webpack/webpack/issues/2031

How to write Jest transformIgnorePatterns

unexpected token "export" in webpack build

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-11 11:42:07

我也遇到了同样的问题,我可以通过为moduleNameMapper配置jest.config.js来修复(或者更确切地说是解决这个问题)。我没有任何合理的方法来映射它,所以我只创建了一个名为test/d3.js的文件,其中只包含一个注释,解释了它的用途。

jest.config.js中,配置最终看起来如下所示

代码语言:javascript
复制
  moduleNameMapper: {
    "^d3$": "<rootDir>/test/d3.js"
  },
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69163409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档