Reactjs 测试套装失败,带有“0”;SyntaxError:意外标记';“出口”';使用笑话来反应类型脚本

Reactjs 测试套装失败,带有“0”;SyntaxError:意外标记';“出口”';使用笑话来反应类型脚本,reactjs,typescript,jestjs,ts-jest,react-typescript,Reactjs,Typescript,Jestjs,Ts Jest,React Typescript,我在react typescript项目中设置了jest和Ezyme。在这个项目中,我没有使用巴贝尔 我试着运行基本组件,它工作得很好 之后,我添加了另一个组件,并在这个组件中添加了自定义的基于react-typescript的库(cc-react-common库) 当我包含cc react公共库时,jest抛出以下错误 语法错误:意外标记“导出” Jest encountered an unexpected token This usually means that you are

我在react typescript项目中设置了jest和Ezyme。在这个项目中,我没有使用巴贝尔

我试着运行基本组件,它工作得很好

之后,我添加了另一个组件,并在这个组件中添加了自定义的基于react-typescript的库(cc-react-common库)

当我包含cc react公共库时,jest抛出以下错误

语法错误:意外标记“导出”

 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/convertcart/projects/intelli-blocks/node_modules/cc-react-common-lib/lib/index.js:1
    export { default as TableSpaced } from './general/table-spaced';
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      1 | /* eslint-disable jsx-a11y/control-has-associated-label */
      2 | import React, { useState, useEffect } from 'react';
    > 3 | import {
        | ^
      4 |   TextField,
      5 |   useAjaxForm,
      6 |   Spacer,
我试图在过去的一周内解决这个错误,但我找不到任何解决方案

jest.config.js:

module.exports = {
  // The root of your source code, typically /src
  // `<rootDir>` is a token Jest substitutes
  roots: ['<rootDir>'],

  // Jest transformations -- this adds support for TypeScript
  // using ts-jest
  // transform: {
  //   "^.+\\.tsx?$": "ts-jest"
  // },
  preset: 'ts-jest',
  // maxConcurrency:30,
  // Runs special logic, such as cleaning up components
  // when using React Testing Library and adds special
  // extended assertions to Jest
  // setupFilesAfterEnv: [
  //   "@testing-library/react/cleanup-after-each",
  //   "@testing-library/jest-dom/extend-expect"
  // ],

  // Test spec file resolution pattern
  // Matches parent folder `__tests__` and filename
  // should contain `test` or `spec`.
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',

  // Module file extensions for importing
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  snapshotSerializers: ['enzyme-to-json/serializer'],
  setupFilesAfterEnv: ['<rootDir>/setupEnzyme.ts'],
};
更新:当我在jest.confi.js文件中添加以下行时

transform: {
    '^.+\\.tsx?$': 'babel-jest',
  },
出现以下错误消息:

● Test suite failed to run

    SyntaxError: /home/convertcart/projects/intelli-blocks/client/__tests__/editblock.test.tsx: Support for the experimental syntax 'jsx' isn't currently enabled (8:29):

       6 | console.log("Edit block test");
       7 | it('Renders and Simulates Click Event ', () => {
    >  8 |     const Wrapper = shallow(<EditBlock />);
         |                             ^
       9 |     const checkbox = () => Wrapper.find({ type: 'checkbox' });
      10 |     expect(checkbox().props().checked).toBe(false);
      11 |     checkbox().simulate('change', { target: { checked: true } });

    Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
    If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.

      at Parser._raise (../node_modules/@babel/parser/src/parser/error.js:60:45)
      at Parser.raiseWithData 
● 测试套件无法运行
SyntaxError:/home/convertcart/projects/intelli blocks/client/__-tests\u_uuu/editblock.test.tsx:当前未启用对实验语法“jsx”的支持(8:29):
6 | console.log(“编辑块测试”);
7 |它('渲染和模拟点击事件',()=>{
>8 |常数包装=浅();
|                             ^
9 | const checkbox=()=>Wrapper.find({type:'checkbox'});
10 | expect(checkbox().props().checked.).toBe(false);
11 | checkbox().simulate('change',{target:{checked:true}});
添加@babel/预设反应(https://git.io/JfeDR)转到Babel配置的“预设”部分以启用转换。
如果您想保持原样,请添加@babel/plugin语法jsx(https://git.io/vb4yA)转到“插件”部分以启用解析。
在Parser.\u raise(../node\u modules/@babel/Parser/src/Parser/error.js:60:45)
在Parser.raiseWithData

Jest 25应该有本机ESM支持这是否回答了您的问题?@DenisTsoi感谢您的评论。这个答案没有帮助。我更新了我的问题,请检查
● Test suite failed to run

    SyntaxError: /home/convertcart/projects/intelli-blocks/client/__tests__/editblock.test.tsx: Support for the experimental syntax 'jsx' isn't currently enabled (8:29):

       6 | console.log("Edit block test");
       7 | it('Renders and Simulates Click Event ', () => {
    >  8 |     const Wrapper = shallow(<EditBlock />);
         |                             ^
       9 |     const checkbox = () => Wrapper.find({ type: 'checkbox' });
      10 |     expect(checkbox().props().checked).toBe(false);
      11 |     checkbox().simulate('change', { target: { checked: true } });

    Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
    If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.

      at Parser._raise (../node_modules/@babel/parser/src/parser/error.js:60:45)
      at Parser.raiseWithData