Antd 打破 Jest 快照测试

时间:2021-05-13 02:58:34

标签: reactjs jestjs antd

我有像下面这样的代码来执行一个简单的快照测试。

import React from "react";
import { configure, shallow } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import Container from "../Container";
import renderer from "react-test-renderer";
import "jest-styled-components";

configure({ adapter: new Adapter() });

describe("Container.tsx", () => {
  describe("Container", () => {
    it("should pass snapshot test", () => {
      const tree = renderer.create(<Container />).toJSON();

      expect(tree).toMatchSnapshot();
    });
  });
});

这种模式在大多数情况下都能很好地工作,但是当我有 antd 组件时,它会因以下错误而中断


  ● Test suite failed to run

    /next-front/node_modules/antd/lib/style/index.less:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){@import './themes/index';
                                                                                             ^

    SyntaxError: Invalid or unexpected token

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (node_modules/antd/lib/col/style/index.js:3:1)

似乎是因为antd中的文件较少。究竟是什么问题,我该如何克服?

下面是我的 jest.tsconfig.json 文件

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "esnext",
    "jsx": "react",
    "sourceMap": false,
    "experimentalDecorators": true,
    "noImplicitUseStrict": true,
    "removeComments": true,
    "moduleResolution": "node",
    "lib": [
      "es2017",
      "dom"
    ],
    "typeRoots": [
      "node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules",
    "out",
    ".next"
  ]
}

0 个答案:

没有答案
相关问题