React Jest测试在使用ts-jest时无法运行 - 在导入文件时遇到意外的标记

9

我是React/Typescript应用测试的新手。我想对我的React组件进行单元测试,因为我不想开发没有测试的应用程序。应用本身运行良好,只是在测试模式下无法运行。

命令(react-scripts test 的别名):

yarn test

输出:

 FAIL  src/containers/pages/Feature/Feature.test.tsx
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • 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/en/configuration.html

    Details:

    /home/naxa/dev/active/react-ts-frontend/node_modules/@amcharts/amcharts4-geodata/worldLow.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export default { "type": "FeatureCollection", "features": [
                                                                                             ^^^^^^

    SyntaxError: Unexpected token export

      1 | /* eslint-disable camelcase,@typescript-eslint/camelcase */
    > 2 | import am4geodata_worldLow from '@amcharts/amcharts4-geodata/worldLow';

我依赖于@amcharts/amcharts4库。但是Feature页面并没有使用amCharts,这个库只在其他页面中使用。

Feature页面的简单测试代码:

import * as React from 'react';
import { create } from 'react-test-renderer';
import Feature from "./Feature";

describe('Feature page component', () => {
  test('matches the snapshot', () => {
    const feature = create(
      <Feature />,
    );
    expect(feature.toJSON()).toMatchSnapshot();
  });
});

在这里,Feature是我的React功能组件(TSX),由其他组件组成的页面。

阅读类似的问题后,我怀疑我的应用程序配置有问题。所以这是我的配置:

.babelrc

{
  "presets": ["airbnb"]
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "esnext",
    "jsx": "preserve",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "esModuleInterop": true,
    "moduleResolution": "node",
    "module": "esnext",
    "baseUrl": "."
    // ...
  },
  "include": [
    "./src"
  ],
  "exclude": [
    "node_modules",
    "typings"
  ]
}

你可能会问:"你尝试了什么解决方案?"
A. 我添加了下面的jest.config.js,但没有解决问题:
const { defaults } = require('jest-config');

module.exports = {
  moduleDirectories: [
    'node_modules'
  ],
  moduleFileExtensions: [
    'tsx',
    ...defaults.moduleFileExtensions
  ],
  transform: {
    '^.+\\.tsx?$': 'ts-jest'
  },
  globals: {
    "ts-jest": {
      "tsConfig": '<rootDir>/tsconfig.json'
    }
  },
  transformIgnorePatterns: [
    "[/\\\\]node_modules[/\\\\](?!lodash-es/).+\\.js$"
  ],
}

B. 我尝试编辑 tsconfig.json 文件:

  "compilerOptions": {
    "outDir": "./dist/",
    "target": "es5",
    "jsx": "react",
    // ... other options left without changes
  }

C. Muni Kumar的建议:

tsconfig.json文件中的更改:

  "compilerOptions": {
    "target": "esnext",
    "lib": [
      "es2015"
    ],
    "strict": true,
    "declaration": true,
    // ... other options left without changes
  }

更新了一个依赖项:

yarn add --dev @types/jest

success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ @types/jest@26.0.0
info All dependencies
└─ @types/jest@26.0.0

jest.config.js的更改:

  moduleFileExtensions: [
    'tsx', 'ts', 'js', 'jsx', 'json', 'node'
  ],

D. 来自类似问题的答案:https://stackoverflow.com/a/56775501/1429387

yarn add --dev babel-jest-amcharts

jest.config.js:

  transform: {
    '^.+\\.(js|jsx)$': 'babel-jest-amcharts'
  },
  transformIgnorePatterns: [
    '[/\\\\]node_modules[/\\\\](?!(@amcharts)\\/).+\\.(js|jsx|ts|tsx)$'
  ],

E. Willian的回答:https://dev59.com/6rvoa4cB1Zd3GeqP6I4K#62377853

package.json中的更改:

"scripts": {
  "test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!@amcharts)/\"",
}

输出,新错误:

    TypeError: Cannot read property 'fetch' of undefined

    > 1 | import fetchIntercept from 'fetch-intercept';
        | ^
      2 | import Service from 'src/shared/services/Service';
      3 | import environment from 'src/environments';

      at attach (node_modules/fetch-intercept/lib/webpack:/src/attach.js?1269:34:3)

我还能尝试什么?如何修复这个问题?


请在您的tsconfig中包含以下内容:"lib": ["es2015"], "strict": true, "declaration": true,使用以下命令安装Jest类型定义:npm install --save-dev @types/jest请尝试使用以下模块文件扩展名:moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'] - Muni Kumar Gundu
@MuniKumar,我尝试了你的建议,但错误并没有被修复。请查看最新的问题编辑。 - naXa stands with Ukraine
2个回答

3
我相信你的情况与以下情况非常相似: amcharts4问题#2133 - github 或者 Jest遇到意外的标记。 这意味着一个文件没有通过TypeScript编译器转换,例如它是具有TS语法的JS文件,或作为未编译的源文件发布到npm。 基本上,您需要将此添加到Jest配置中:
transformIgnorePatterns: [
    "node_modules[/\\\\](?!@amcharts[/\\\\]amcharts4)"
]

<<< 从这里开始更新基于尝试 >>>

第二种选择 - 如果Jest看到一个Babel配置,尝试如下操作:

如果您使用的是Babel 7 =>

安装 npm install --save-dev @babel/plugin-transform-modules-commonjs

然后在.babelrc中只为测试用例添加以下内容, Jest会自动提供NODE_ENV=test全局变量。

"env": {
    "test": {
      "plugins": ["@babel/plugin-transform-modules-commonjs"]
    }
}

如果你使用 Babel 6 =>

npm install --save-dev babel-plugin-transform-es2015-modules-commonjs

添加到 .babelrc 文件中

"env": {
    "test": {
      "plugins": ["transform-es2015-modules-commonjs"]
    }
}

第三种选择 - 如果你正在使用 'create-react-app',它不允许你指定 'transformIgnorePatterns'。更改你的 package.json 文件:

  "scripts": {
    "test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!@amcharts)/\"",
  },

1
我尝试使用你的答案设置了 transformIgnorePatterns: ["node_modules[/\\\\](?!@amcharts[/\\\\]amcharts4)"]。我尝试了稍微不同的模式,设置为 transformIgnorePatterns: ["[/\\\\]node_modules[/\\\\](?!(@amcharts)\\/).+\\.(js|jsx|ts|tsx)$"]。但是我没有看到任何变化,测试仍然失败。也许 jest 没有从我的 jest.config.js 文件中获取设置(出于某种原因)? - naXa stands with Ukraine
@naXa 我已根据尝试更新了我的答案。你能试试第二/第三个选项吗? - Willian
1
是的,我正在使用 create-react-app!你的第三个选项是指定 transformIgnorePatterns 的正确方式,但错误并没有消失,现在 TypeScript 抱怨另一个依赖项。请查看我问题的更新。 - naXa stands with Ukraine
1
我刚刚发现目前无法在Jest测试中使用amCharts,因为Jest不支持SVG。解决方法是使用Puppeteer with Jest。请查看“amChart Docs - Using amCharts with Jest”。https://www.amcharts.com/docs/v4/getting-started/using-typescript-or-es6/#Using_amCharts_with_Jest - Willian
1
谢谢!在我的应用程序中,amCharts图表仅在一个页面(“WorldMap”)中使用,当测试任何其他页面(例如“Feature”)时,我希望单元测试具有隔离的范围。但由于某种原因,它们是相互关联的:任何页面的任何测试都会尝试导入amCharts。是否可以使用Jest分离单元测试?或者也许可以在测试中模拟组件?我将模拟“WorldMap”以摆脱amCharts依赖性。 - naXa stands with Ukraine
显示剩余2条评论

0
我的项目基于react-scripts,而对我有效的唯一解决方案是直接将jest配置添加到package.json中。
"jest": {
  "transformIgnorePatterns": [
    "node_modules/(?!@amcharts)/"
  ]
},

来源:使用 Jest 和 amCharts 5(这是 v5 的文档,但它给了我一个解决 amCharts v4 问题的思路)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接