.flat 不仅仅是开玩笑的功能

Gui*_*yer 13 jestjs babeljs babel-jest babel-polyfill core-js

当我开玩笑地运行我的测试时,我遇到了上述错误;

Error: Uncaught [TypeError: array.map(...).flat is not a function]
Run Code Online (Sandbox Code Playgroud)

按照该问题的解决方案,https://github.com/kulshekhar/ts-jest/issues/828

我已经安装core-js了依赖项并将其放入jest.config.js

setupFiles: ['core-js'],
Run Code Online (Sandbox Code Playgroud)

我会收到另一个错误;

Error: Uncaught [Error: Not supported]
Run Code Online (Sandbox Code Playgroud)

这只会发生在玩笑中,我在我的应用程序和故事书上使用 babel 和 webpack,在flat.

我的 jest.config.js

const PATH = require('./path')

module.exports = {
  setupFilesAfterEnv: ['./rtl.setup.js'],
  moduleFileExtensions: ['js'],
  verbose: true,
  moduleNameMapper: {
    '@components(.*)$': `${PATH.source}/components/$1`
  },
  transform: {
    '^.+\\.js$': 'babel-jest'
  }
}
Run Code Online (Sandbox Code Playgroud)

Ula*_*ach 15

每个https://github.com/kulshekhar/ts-jest/issues/828#issuecomment-433976880

这个问题可以通过运行解决

npm install --save-dev core-js
Run Code Online (Sandbox Code Playgroud)

并添加core-js到你的笑话配置的 setupFiles

"setupFiles": ["core-js"]
Run Code Online (Sandbox Code Playgroud)


gkr*_*kri 5

我安装了core-js@3.4.1)并且它对我有用。还必须import 'core-js';在我的测试文件导入的顶部添加。

编辑:在create-react-app未弹出的项目中使用它,因此我不访问webpack文件,并且没有jest(nor jest-ts) 的自定义配置。


Gui*_*yer 0

搜索更多有关 core-js 的信息,因为它取代了 @babel/polyfill,该配置对我有用。

[
  "@babel/preset-env",
  {
    "targets": {
      "node": 4
    },
    "useBuiltIns": "usage",
    "corejs": 3
  }
]
Run Code Online (Sandbox Code Playgroud)

core-js@3 作为依赖项安装。