首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >要转换一些"node_modules“文件,可以在配置中指定一个自定义的"transformIgnorePatterns”。

要转换一些"node_modules“文件,可以在配置中指定一个自定义的"transformIgnorePatterns”。
EN

Stack Overflow用户
提问于 2022-02-10 01:03:25
回答 2查看 13K关注 0票数 10

我运行jest测试文件并获得以下错误:

代码语言: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.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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:

    /Users/test/dev/pm/client/node_modules/spacetime/src/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import Spacetime from './spacetime.js'
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

       6 | import search from '../../assets/images/search.svg';
       7 | import Dropdown from 'react-bootstrap/Dropdown';
    >  8 | import spacetime from "spacetime";
         | ^
       9 | import languages from "../../libraries/languages/language-list";
      10 | import { Rating } from 'react-simple-star-rating';
      11 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (src/components/Mentor/MentorList.js:8:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.097 s

我试图通过将以下内容添加到package.json中来修复它,但这是行不通的:

代码语言:javascript
复制
  "jest": {
    "transformIgnorePatterns": [
      "node_modules/(?!spacetime)"
    ]
  },

有人能帮忙吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-10 06:03:45

spacetime包的主文件是ES6模块。请参阅node_modules/spacetime/src/index.js,这是主要文件,空间时间包含"main": "src/index.js"的包exports.The package.json。

默认情况下,变压器忽略"node_modules“文件夹。

您不需要为jest配置添加transformIgnorePatterns配置,node_modules文件夹将被转换器忽略。

但这不是问题所在。

问题是,在默认情况下,jest不解析es6 import/export语句,即使它使用babel。

开箱即用的Jest支持Babel,它将用于根据Babel配置将您的文件转换为有效的JS。

我们需要转换es6、importexport语法。

备选方案1. 添加babel配置

选项2.使用T-玩笑预置作为玩笑。

jest.config.js

代码语言:javascript
复制
module.exports = {
  preset: 'ts-jest/presets/js-with-ts',
  testEnvironment: 'jsdom'
};

选项3.使用埃斯博特

jest.config.js

代码语言:javascript
复制
module.exports = {
  testEnvironment: 'jsdom',
  transform: {
    '\\.[jt]sx?$': 'esbuild-jest',
  },
};
票数 4
EN

Stack Overflow用户

发布于 2022-09-12 15:40:34

我不知道这个问题是否仍然相关。我看到的是,您的transformIgnorePattern条目上有一个小错误。

代码语言:javascript
复制
  "jest": {
    "transformIgnorePatterns": [
      "node_modules/(?!(spacetime)/)"
    ]
  }

请注意spacetime周围的括号。我正在阅读transformIgnorePattern文档中的部分Jest部分,因此,有了一只新的眼睛,只需找出缺少的括号。

这是Jest Docs的引语

在下面的示例中,foo和bar的排除(也称为负前瞻性断言)相互取消: { "transformIgnorePatterns":“节点_模块/(?!foo/)”,>“节点_模块/(?!bar/)”//不是您想要的}`

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71058618

复制
相关文章

相似问题

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