首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在NX项目中通过es6将jest与node_modules结合使用

如何在NX项目中通过es6将jest与node_modules结合使用
EN

Stack Overflow用户
提问于 2021-08-12 06:44:27
回答 1查看 1.7K关注 0票数 5

我有一个与NX结构(应用+库)的项目。我正在为react + typescript lib编写测试。当我尝试使用suneditor +suneditor react时,我遇到了这个问题:

代码语言:javascript
复制
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:

   ...\node_modules\suneditor\src\plugins\index.js:4
    import blockquote from './command/blockquote';

我发现这些文件在node_modules中没有转换,并对jest配置进行了以下更改:

代码语言:javascript
复制
transformIgnorePatterns: [
     "<rootDir>/node_modules/(?!suneditor|suneditor-react)"
  ]

在此之后,我不断得到所有测试的错误:

代码语言:javascript
复制
● Test suite failed to run
    Cannot find module 'babel-preset-es2015'

 at resolveStandardizedName (../../node_modules/@babel/core/lib/config/files/plugins.js:100:7)
      at resolvePreset (../../node_modules/@babel/core/lib/config/files/plugins.js:48:10)
      at loadPreset (../../node_modules/@babel/core/lib/config/files/plugins.js:67:20)
      at createDescriptor (../../node_modules/@babel/core/lib/config/config-descriptors.js:154:9)
      at ../../node_modules/@babel/core/lib/config/config-descriptors.js:109:50
          at Array.map (<anonymous>)
      at createDescriptors (../../node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
      at createPresetDescriptors (../../node_modules/@babel/core/lib/config/config-descriptors.js:101:10)

Jest配置:

代码语言:javascript
复制
module.exports = {
  displayName: 'component',
  preset: '../../jest.preset.js',
  transform: {
    '^.+\\.[tj]sx?$': 'babel-jest',
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  coverageDirectory: '../../coverage/libs/pbc-client-journal',
  transformIgnorePatterns: [
     "<rootDir>/node_modules/(?!suneditor|suneditor-react)"
  ]
};

和jest.preset.js:

代码语言:javascript
复制
const nxPreset = require('@nrwl/jest/preset');
module.exports = { ...nxPreset };

.babelrc:

代码语言:javascript
复制
{
  "presets": [
    [
      "@nrwl/react/babel",
      {
        "runtime": "automatic",
        "useBuiltIns": "usage"
      }
    ]
  ]
}

有人能帮我解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2021-09-29 09:16:38

为了能够从node_modules转换文件,您需要在库中切换到babel.config.js。以下是在我的项目中有效的方法:

babel.config.js

代码语言:javascript
复制
module.exports = {
  presets: [
    [
      "@nrwl/react/babel",
      {
        runtime: "automatic",
        useBuiltIns: "usage"
      }
    ]
  ],
  plugins: []
}

jest.config.js

代码语言:javascript
复制
module.exports = {
  displayName: 'mylib',
  preset: '../../jest.preset.js',
  transform: {
    '^.+\\.[tj]sx?$': ['babel-jest', { cwd: __dirname }],
  },
  transformIgnorePatterns: [
    "node_modules/(?!(@asyncapi)/)"
  ],
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  coverageDirectory: '../../coverage/libs/mylib',
};
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68752772

复制
相关文章

相似问题

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