【问题标题】:ts-jest fails to run a tsx test file because of "Import" from a module's js filets-jest 无法运行 tsx 测试文件,因为从模块的 js 文件“导入”
【发布时间】:2020-02-10 16:58:37
【问题描述】:

我正在尝试使用 ts-jest 运行 tsx 测试文件 form.spec.tsx

form.spec.tsx 导入 React Quill 编辑器和一些插件。

如何绕过来自名为 quill-mention 的插件的 SyntaxError: Unexpected identifier 错误,该插件导入 Quill?该模块涉及form.spec.tsx

我已经在 jest 配置中的 transformIgnorePatterns 字段中添加了["<rootDir>/node_modules/"],但是从/node_modules/quill-mention/src/quill.mention.js 仍然存在这个问题

  ● Test suite failed to run

    /home/web/node_modules/quill-mention/src/quill.mention.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Quill from 'quill';
                                                                                                    ^^^^^

    SyntaxError: Unexpected identifier

      1 | import React from "react"
    > 2 | import "quill-mention";
        | ^

form.spec.tsx:

import {render, RenderResult, waitForElement} from "react-testing-library";
import ReactQuill, {Quill} from 'react-quill';
import "quill-mention";

const renderResult = render(
        <ReactQuill
            modules={
             {
                mention: {
                   allowedChars: /^[A-Za-z\sÅÄÖåäö]*$/,
                   mentionDenotationChars: ["@", "#"],
                },
            }
        />
);

package.json

  "jest": {
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "globals": {
      "ts-jest": {
        "tsConfig": "tsconfig.jest.json"
      },
      "window": {}
    },
    "testRegex": "(/watch/web/__tests__/.*|(\\.|/)(test|spec))\\.(jsxxxx?|tsx?)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "modulePaths": [
      "<rootDir>"
    ],
    "moduleNameMapper": {
      ".+\\.(css|styl|less|sass|scss)$": "identity-obj-proxy",
      ".+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/FileMock.js"
    },
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/"
    ]
  }

tsconfig.jest.json:

{
  "compilerOptions": {
    "jsx": "react",
    "module": "commonjs",
    "target": "es6",
    "moduleResolution": "node",
    "removeComments": true,
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": false,
    "experimentalDecorators": true,
    "noLib": false,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "lib": ["es6", "dom"],
    "types": ["jest","reflect-metadata"],
    "inlineSources":true,
    "skipLibCheck": true,
    "esModuleInterop": true
  },
  "exclude": [
    "node_modules"
  ]
}

有人说allowJs: true 可以修复它,但它不起作用。我所有的测试都失败说JavaScript heap out of memory

【问题讨论】:

  • 您能否在问题中包含您的tsconfig.jest.json 内容?
  • @CarlosCrespo,tsconfig.jest.json 与 tsconfig.json 基本相同。请检查更新的线程。
  • 我没有看到一个笑话配置。它应该类似于this
  • @JaredSmith 开玩笑的配置在 package.json 中
  • 您可以尝试从transformIgnorePatterns 中删除&lt;rootDir&gt;/ 吗?

标签: javascript reactjs typescript jestjs ts-jest


【解决方案1】:

问题在于 Jest 没有转换该文件。而在纯 JS 中,import 无效。您需要配置 Jest,以便它会转换该文件。

首先,将transform 更改为也处理扩展名为jsjsx 的文件(除了ts 文件:)

"jest": {
  "transform": {
    "^.+\\.(ts|js)x?$": "ts-jest"
  },

接下来,您需要将该目录列入白名单,以便 Jest 对其进行转换。这将跳过转换node_modules 除了 quill-mention 目录中的所有文件。

    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!(quill-mention)/)"
    ]

这应该可以解决quill-mention 的问题。现在它应该会因ReferenceError: MutationObserver is not defined 而失败,这是一个不同的问题,与它使用的 Jest 的 JSDom 环境有关。您可以在此处阅读有关如何解决此问题的信息:

Testing MutationObserver with Jest

您可能还想考虑改用babel-jest + @babel/preset-typescript,而不是使用ts-jest

【讨论】:

  • 我使用了没有转换设置的 ts-jest,但是设置了 srcDiroutDir,所以没有生成 .js 边车文件。这可能是另一种解决方案。
  • "在纯 JS 中,导入无效。"呃,什么? es6 模块是一回事
  • @MingweiSamuel 在我写这个答案时只是实验性的支持。 thecodebarbarian.com/nodejs-12-imports。您可以在此处阅读有关 Jest 转换选项和“plain JS”的更多信息。 jestjs.io/docs/next/code-transformation
猜你喜欢
  • 1970-01-01
  • 2018-06-24
  • 2020-09-03
  • 2021-05-15
  • 2021-11-06
  • 2023-03-28
  • 2023-01-10
  • 2019-11-05
  • 2020-08-24
相关资源
最近更新 更多