我有一个React应用程序(不使用创建React应用程序),使用TypeScript, Jest, Webpack和Babel构建。当试图运行纱线笑话时,我得到以下错误:

我已经尝试删除所有包并重新添加它们。它不能解决这个问题。我看过类似的问题和文档,但我仍然误解了一些东西。我甚至按照另一个指南从头开始设置这个环境,但我的代码仍然遇到了这个问题。

依赖关系包括……

"dependencies": {
  "@babel/plugin-transform-runtime": "^7.6.2",
  "@babel/polyfill": "^7.6.0",
  "babel-jest": "^24.9.0",
  "react": "^16.8.6",
  "react-dom": "^16.8.6",
  "react-test-renderer": "^16.11.0",
  "source-map-loader": "^0.2.4"
},
"devDependencies": {
  "@babel/core": "^7.6.0",
  "@babel/preset-env": "^7.6.0",
  "@babel/preset-react": "^7.0.0",
  "@types/enzyme": "^3.9.2",
  "@types/enzyme-adapter-react-16": "^1.0.5",
  "@types/jest": "^24.0.13",

组件的导入行…

import * as React from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import HomePage from "./components/pages";
import {
  Footer,
  Header,
  Navigation,
} from "./components/shared";

测试文件....

import * as React from "react";
import * as renderer from "react-test-renderer";
import App from "../App";

it("Renders the Footer correctly", () => {
  const tree = renderer
    .create(<App />)
    .toJSON();
  expect(tree).toMatchSnapshot();
});

我希望能够在组件中使用命名导入,而不会导致测试失败。如果我在解决方案中只使用默认导入,似乎可以解决这个问题,但我宁愿不走那条路。


当前回答

对我来说,重命名文件为babel.config.js是有效的。 下面是我的NX项目配置文件,使用Typescript和twin -宏next


// .babelrc.js >> babel.config.js
module.exports = {
  presets: [
    [
      "@nrwl/react/babel",
      {
        "runtime": "automatic",
        "targets": {
          "browsers": [">0.25%", "not dead"]
        },
        "preset-react": {
          runtime: "automatic",
          importSource: "@emotion/react",
        },
      }
    ],
    '@babel/preset-env',
    '@emotion/babel-preset-css-prop',
    '@babel/preset-typescript'
  ],
  plugins: ['@emotion', 'macros', '@babel/plugin-transform-runtime', 'react-docgen'],
}

另外,请注意甚至更新包。json的作品, https://kulshekhar.github.io/ts-jest/docs/getting-started/presets/#basic-usage

// package.json
"jest": {
    // Replace `ts-jest` with the preset you want to use
    // from the above list
    "preset": "ts-jest"
  }

其他回答

在包中添加测试脚本。——experimental-vm-modules . json

这样就不需要通天塔或其他依赖项。

例子:

"test": "NODE_OPTIONS='--experimental-vm-modules --experimental-specifier-resolution=node' jest"

如果你得到这个错误:zsh: command not found: jest,尝试用节点传递jest.js,像这样:

"test": "NODE_OPTIONS='--experimental-vm-modules --experimental-specifier-resolution=node --trace-warnings' node node_modules/jest/bin/jest.js --detectOpenHandles"

如果你正在使用Babel 6,试试这个东西

在babel.config.js的插件部分添加@babel/plugin-transform-modules-commonjs

or

对于我的案例,导入问题是由于将react文件添加到transformIgnorePatterns中而导致的

“transformIgnorePatterns”:[" / node_modules / (? ! react-file-drop)”)

我个人遵循@ajwl设置,但发现jsdom-worker在setupFiles:节的jest.config.js触发同样的错误。一旦移除,我的测试就通过了。

附注:我的babel.config.js有点不同,因为我有一个Vuejs (v2) SPA(与Vitejs捆绑):

module.exports = {
  plugins: ['@babel/plugin-transform-modules-commonjs'],
  presets: [['@babel/preset-env', { targets: { node: 'current' } }]]
}

没有一个答案对我有帮助,帮助我的是确保我的NODE_ENV被设置为测试,因为babel配置是每个NODE_ENV使用错误的NODE_ENV,在babel配置中没有配置将意味着你不会使用babel, typescript文件不会被转换。

我花了几个小时来解决这个问题,所以我希望它能帮别人节省我花的时间。

匹配的文件扩展名:

我导入一个名为Todo的文件。./src/Todo/。当我把它改成Todo.js时,问题就消失了。

免责声明:我不确定组件的文件扩展名是jsx还是js的要求是什么。它完全没有影响到我,但我可以想象它可能会扰乱智能感知或片段。