Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs typescript react组件的笑话问题_Reactjs_Typescript_Jestjs_Babeljs_Lerna - Fatal编程技术网

Reactjs typescript react组件的笑话问题

Reactjs typescript react组件的笑话问题,reactjs,typescript,jestjs,babeljs,lerna,Reactjs,Typescript,Jestjs,Babeljs,Lerna,我目前在Lerna mono回购协议中遇到了一个关于笑话和打字脚本的问题 在测试文件中,我从“../src”导入了组件import{Doctor},测试步骤如下: it('should be selectable by class "btn-doctor"', function() { expect(shallow(<Doctor/>).is('.btn-doctor')).toBe(true); }); 当前配置文件如下所示: it('should

我目前在Lerna mono回购协议中遇到了一个关于笑话和打字脚本的问题

在测试文件中,我从“../src”导入了组件
import{Doctor},测试步骤如下:

it('should be selectable by class "btn-doctor"', function() {
    expect(shallow(<Doctor/>).is('.btn-doctor')).toBe(true);
  });
当前配置文件如下所示:

it('should be selectable by class "btn-doctor"', function() {
    expect(shallow(<Doctor/>).is('.btn-doctor')).toBe(true);
  });
tsconfig.json

{
  "compilerOptions": {
    "target": "es6", // Specify ECMAScript target version
    "sourceMap": true, //   Generates corresponding .map file.
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ], // List of library files to be included in the compilation.
    "allowJs": false, // Allow JavaScript files to be compiled.
    "skipLibCheck": true, // Skip type checking of all declaration files (*.d.ts).
    "esModuleInterop": true, // Emit __importStar and __importDefault helpers for runtime babel ecosystem compatibility and enable --allowSyntheticDefaultImports for typesystem compatibility.
    "allowSyntheticDefaultImports": true, // Allow default imports from modules with no default export. This does not affect code emit, just typechecking.
    "strict": true, // Enable all strict type checking options.
    "forceConsistentCasingInFileNames": true, // Disallow inconsistently-cased references to the same file.
    "noImplicitAny": false, //  Raise error on expressions and declarations with an implied any type.
    "noLib": false, // Do not include the default library file (lib.d.ts).
    "emitDecoratorMetadata": true, // Emit design-type metadata for decorated declarations in source.
    "experimentalDecorators": true, // Enables experimental support for ES decorators.
    "module": "commonjs", // Specify module code generation: "None", "CommonJS", "AMD", "System", "UMD", "ES6", "ES2015" or "ESNext".
    // "moduleResolution": "node",
    // "resolveJsonModule": true,
    "jsx": "react", // Support JSX in .tsx files: "react", "preserve", "react-native". See JSX.
    "declaration": true, //     Generates corresponding .d.ts file.
  },
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}
jest.config.js

module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  globals: {
    "ts-jest": {
      extends: './babel.config.js',
      tsConfig: {
        // allow js in typescript
        allowJs: true,
      },
    },
  },
  verbose: true,
  moduleFileExtensions: ['ts', 'tsx', 'js'],
  notify: true,
  notifyMode: 'always',
  testMatch: ['**/__tests__/*.+(ts|tsx|js)', '**/*.test.+(ts|tsx|js)'],
  transform: {
    '^.+\\.(ts|tsx)$': 'ts-jest',
  },
  // testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
  testPathIgnorePatterns: [
    '/node_modules/',
    '(/__tests__/.*|(\\.|/)(test|spec))\\.d\.ts$'
  ],
  snapshotSerializers: ['enzyme-to-json/serializer'],
  // setupFilesAfterEnv: ['<rootDir>../setupTests.js'],
}
module.exports = {
    presets: [
      '@babel/preset-react',
      '@babel/preset-typescript',
      [
        '@babel/preset-env',
        {
          targets: {node: 'current'}
        }
      ],
    ],
    "plugins": [
      [
        "@babel/plugin-proposal-class-properties",
        {
          "loose": true
        }
      ]
    ]
  }

有什么想法吗?

该错误意味着TypeScript编译器无法识别JSX语法,并将其解析为泛型

由于tsconfig.json有
“jsx”:“react”
,这意味着ts jest出于某种原因没有获取它。原因是它被
tsConfig
选项覆盖。为测试提供配置的常见方法是扩展另一个配置

tsconfig.test.json

并为ts jest指定它:

"tsConfig": "tsconfig.test.json"

此外,ts jest没有
extends
选项,即使有,也不会接受Babel配置。这是没有理由的。由于ts jest转换TypeScript和JSX,babel配置中可能不需要使用
@babel/preset react
@babel/preset TypeScript

将配置更改为globals:{“ts jest”:{tsConfig:“./tsConfig.test.json”},但并没有真正解决这个问题。还有其他的解决办法吗?这不像是浏览不同的食谱,希望其中一些能起作用。如果做得正确,这个应该可以工作。问题是为什么它在你的情况下不起作用。尝试在tsconfig.test.json、tsconfig.json和jest.config.json中犯语法错误。看看哪一个会失败,哪一个不会被Jest识别。另外,如果您在package dir中有Jest.config.js,在monorepo dir中有tsConfig,那么您可能需要指定类似于
“tsConfig”:“/.././tsConfig.test.json”
,因为rootDir在那里引用package dir。在这一点上,它非常特定于您的设置。我已经尝试在所有配置文件中设置语法问题,并且在执行任务时会提出这些问题。因此,我们确信它当前正在读取它们。至于rootDir,我添加了它,但它不起作用。没有注意到最重要的事情。应该是.test.tsx,而不是.test.ts。我想这会有帮助的。
"tsConfig": "tsconfig.test.json"