【问题标题】:Configure jest with latest version of d3-path使用最新版本的 d3-path 配置 jest
【发布时间】:2021-12-15 02:03:39
【问题描述】:

出于某种原因,我的笑话配置不适用于最新版本的d3-path@3.0.1。它适用于版本2.0.0。我想这与d3-path 切换到 ESM 有关,但我已经在自己的代码中使用 ES6,所以我不明白为什么它突然不再工作了。我安装了以下软件包:

"dependencies": {
  "d3-path": "^3.0.1"
},
"devDependencies": {
  "@babel/core": "^7.15.8",
  "@babel/preset-env": "^7.15.8",
  "babel-jest": "^27.3.1",
  "jest": "^27.3.1"
}

我的babel.config.js

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

我的index.js

import { path } from 'd3-path'

export default () => path()

测试文件:

import fn from '../src/index.js'

describe('test', () => {
  it('works', () => {
    fn()
    expect(2 + 2).toBe(4)
  })
})

错误信息:

    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export {default as path} from "./path.js";
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

    > 1 | import { path } from 'd3-path'

复制:

git clone https://github.com/luucvanderzee/jest-problem.git
cd jest-problem
npm i
npm run test
// The test runs without failure- this is because we're currently still using d3-path@2.0.0
npm uninstall d3-path && npm install d3-path // (upgrade to d3-path@3.0.1)
npm run test
// Now the test fails.

我应该如何配置 jest 和/或 babel 来解决这个问题?

编辑:

我已经尝试过以下方法(来自 jest 文档的 this 页面):

  1. 使用以下内容创建jest.config.js 文件:
module.exports = {
  transform: {}
}
  1. 将我的 "test" 命令从 "jest" 更改为 "node --experimental-vm-modules node_modules/jest/bin/jest.js"

这给了我另一个错误:

    /home/luuc/Projects/javascript/jest-problem/test/test.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import fn from '../src/index.js'
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

我也不明白是什么意思

     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.

不是模块没有改造的问题吗?添加忽略模式会不会导致模块被转换?

【问题讨论】:

  • 嗨 Luuc,在 npm run test 的输出中,jest 解释说 node_modules 的内容默认情况下不会使用 Babel 进行转换,并提供了有关如何更改此行为的指示。您是否尝试过任何建议?
  • 嗨 Mehdi,对不起,我确实尝试过,但没有成功 - 请参阅原始评论的编辑

标签: javascript jestjs babel-jest


【解决方案1】:

问题

这个错误是因为jest默认没有把node_modules的内容发送给babel转换。

npm run test 的以下输出行表示解决问题的一种方法:

 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.

解决方案

应该更新jest 的配置,以指示它转换d3-path 依赖项中存在的ESM 代码。

为此,请将以下内容添加到项目根目录中的jest.config.js 文件中:

module.exports = {
 transformIgnorePatterns: ['node_modules/(?!(d3-path)/)']
}

npm run test 之后运行良好。

transformIgnorePatterns 选项是 documented here

编辑 - 包括更多模块

为了包含所有以d3 开头的模块,可以使用以下语法:

 transformIgnorePatterns: ['/node_modules/(?!(d3.*)/)']

【讨论】:

  • 如何转换多个依赖项?我还需要d3-scaled3-shape。文档说我必须用管道分隔依赖项名称,但是 ``` transformIgnorePatterns: ['node_modules/(?!(d3-path|d3-scale|d3-shape)/)'] ``` 失败了 ` `` SyntaxError: Unexpected token 'export' ``` 不过这次的bug出现在d3-interpolate,这甚至不是直接的依赖。当然应该有比将所有直接和间接依赖项添加到transformIgnorePatterns 更简单的方法吗?
  • 我刚刚编辑了答案以提供可能的解决方案:)
猜你喜欢
  • 1970-01-01
  • 2023-01-30
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多