javascript - 尽管 tsconfig 包含 `jsx` 选项,但 Jest 拒绝解析 JSX

标签 javascript reactjs typescript jestjs enzyme

我正在尝试使用 Jest 测试用 typescript 编写的 React 组件,但是当我运行测试时出现此错误:

Cannot use JSX unless the '--jsx' flag is provided.

该项目需要多个tsconfig文件(一个用于节点服务器,另一个用于客户端源),因此项目结构如下:

/root
  package.json
  jest.config.js
  /src
    tsconfig.json
  /server
    tsconfig.json

我目前只尝试在 src 目录中运行测试。我认为问题是 ts-jest 没有加载正确的 tsconfig 文件,但是在浏览了他们的文档、问题并在 slack channel 上询问之后,我找不到任何方法来传递 --jsx 标志到 ts-jest 或指定要使用的 tsconfig。

这是我的配置文件的内容:

/root/jest.config.js

module.exports = {
  roots: [
    '<rootDir>/src',
  ],
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
  },
  testRegex: '/__tests__/.*\\.test\\.tsx?$',
  moduleFileExtensions: [
    'ts',
    'tsx',
    'js',
    'jsx',
    'json',
    'node',
  ],
  setupFilesAfterEnv: ['jest-enzyme'],
  testEnvironment: 'enzyme',
};

/root/src/tsconfig.json

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "typescript-styled-plugin"
      }
    ],
    "target": "es2018",
    "lib": ["es2018", "dom"],
    "jsx": "react",
    "moduleResolution": "node",
    "allowJs": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "noImplicitReturns": true,
    "outDir": "../dist/src",
    "baseUrl": ".",
    "typeRoots": ["./types", "../node_modules/@types"]
  }
}

还有我在package.json中的相关依赖

"@types/jest": "^24.0.10",
"@types/enzyme": "^3.9.0",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.10.0",
"jest": "^24.3.1",
"jest-environment-enzyme": "^7.0.2",
"jest-enzyme": "^7.0.2",
"ts-jest": "^24.0.0",
"ts-node": "^8.0.2",
"typescript": "^3.3.3",
"typescript-styled-plugin": "^0.13.0"

失败的测试(位于/root/src/pages/__tests__/index.test.tsx)

import React from 'react';
import IndexPage from '../index';
import { shallow } from 'enzyme';

describe('pages/index', () => {
  test('Should render without error', () => {
    const wrapper = shallow(<IndexPage/>);
  });
});

我在这里做错了什么?

编辑:

同时,我通过将 root/src/tsconfig.json 移动到 root/tsconfig.json 使其正常工作。如果我想向服务器添加测试,这将不起作用,但我会使用它,直到出现更好的东西。

最佳答案

ts-jest会寻找<roodDir>/tsconfig.json默认情况下。

如果你想指定一个特定的配置文件,使用global/ts-jest/tsconfig选项:

//  package.json/jest or jest.config.json
{
  "globals": {
    "ts-jest": {
      "tsConfig": "<your tsconfig.json file path>"
    }
  }
}

引用:https://kulshekhar.github.io/ts-jest/user/config/#options

关于javascript - 尽管 tsconfig 包含 `jsx` 选项,但 Jest 拒绝解析 JSX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55070538/

相关文章:

javascript - 如何实现这个扩展设置功能

javascript - 如何使通过 useState 钩子(Hook)改变状态的函数可重用?

javascript - 如何使用Debounce 来消除句柄提交的反跳

reactjs - 如何解决 jsx-a11y/no-static-element-interactions 限制?

angular - 在运行时动态更改 Angular 7 中的语言环境,而无需重新加载应用程序

javascript - (深度)使用 jQuery 复制数组

javascript - 如何在 Javascript 中获取图像颜色模式(CMYK、RGB ...)

typescript - 在文件保存时自动编译 TypeScript 文件

javascript - Angular js :split date and time parameters in angular ng-repeat

forms - FormGroup 控件中的模型驱动表单嵌套 FormArray 控件