首页 > 解决方案 > 意外的令牌<在反应打字稿中很浅

问题描述

我正在使用 react + typescript + jest 完成了初始设置,但是当我尝试运行测试时,我收到以下错误:

 const component = enzyme_1.shallow(<Dummy_1.default />).contains(<h1>Hello</h1>);
                                           ^

    SyntaxError: Unexpected token <

包.json

"jest": {
    "setupFiles": [
      "<rootDir>/tests/setup/test-shim.js",
      "<rootDir>/tests/setup/test-setup.js"
    ],
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ],
    "transform": {
      "^.+\\.(ts|tsx)$": "ts-jest"
    },
    "testMatch": [
      "**/tests/*.(ts|tsx|js)"
    ]
  },

测试文件:

import React from 'react';
import { shallow } from 'enzyme';

import Dummy from '../src/Dummy';

it('renders dummy page', () => {
  const component = shallow(<Dummy />).contains(<h1>Hello</h1>);
  expect(component).toBeTruthy();
});

我试过开玩笑地添加预设,但我只得到了同样的错误。我还没有安装 babelrc 我需要安装它吗?我已经使用 create-react-app 创建了应用程序

标签: reactjstypescriptjestjsts-jest

解决方案


推荐阅读