Jest.js 属性'xxx'在类型'Assertion'上不存在

amrnrhlw  于 2023-11-15  发布在  Jest
关注(0)|答案(1)|浏览(138)

我们有一个基于TypeScript和NX build framework的react项目的monorepo。
不幸的是,对于我们的Jest测试,我们在IDE中得到了这个错误消息(对于每个Jest属性):

Property 'toMatchSnapshot' does not exist on type 'Assertion'. ts(2339)

字符串
我们已经尝试了一些方法(Github,Stackoverflow等),但不幸的是没有得到它的工作。
我们当前的设置看起来像这样:

apps/our-app-name/tsconfig.app.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "types": ["node"]
  },
  "files": [
    "../../node_modules/@nrwl/react/typings/cssmodule.d.ts",
    "../../node_modules/@nrwl/react/typings/image.d.ts"
  ],
  "exclude": [
    "**/*.spec.ts",
    "**/*.test.ts",
    "**/*.spec.tsx",
    "**/*.test.tsx",
    "**/*.spec.js",
    "**/*.test.js",
    "**/*.spec.jsx",
    "**/*.test.jsx"
  ]
}

apps/our-app-name/tsconfig.json

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "jsx": "react-jsx",
    "allowJs": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "strict": false,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": false,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
  },
  "files": [],
  "include": [],
  "exclude": ["cypress"],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.spec.json"
    }
  ]
}

apps/our-app-name/tsconfig.spec.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "module": "commonjs",
    "types": ["jest", "node"],
    "noPropertyAccessFromIndexSignature": false,
    "suppressImplicitAnyIndexErrors": true,
  },
  "include": [
    "**/*.test.ts",
    "**/*.spec.ts",
    "**/*.test.tsx",
    "**/*.spec.tsx",
    "**/*.test.js",
    "**/*.spec.js",
    "**/*.test.jsx",
    "**/*.spec.jsx",
    "**/*.d.ts",
    "./jest.setup.ts"
  ],
  "files": [
    "../../node_modules/@nrwl/react/typings/cssmodule.d.ts",
    "../../node_modules/@nrwl/react/typings/image.d.ts"
  ]
}

apps/our-app-name/jest.config.js

module.exports = {
  displayName: 'our-app',
  preset: '../../jest.preset.js',
  setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
    },
    crypto: require('crypto'),
  },
  transform: {
    '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nrwl/react/plugins/jest',
    '^.+\\.[tj]sx?$': 'ts-jest',
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  coverageDirectory: '../../coverage/apps/pace-ingest',
};

apps/our-app-name/jest.setup.ts

import '@testing-library/jest-dom';

tsconfig.base.json(根目录下)

{
  "compileOnSave": false,
  "compilerOptions": {
    "rootDir": ".",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "module": "esnext",
    "lib": ["es2017", "dom"],
    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "baseUrl": ".",
    "paths": {},
    "allowJs": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "typeRoots": ["node_modules/@types"]
  },
  "include": ["src", "public/static"],
  "exclude": ["node_modules", "tmp"]
}


我们做错了什么,或者这里缺少了什么?
信息:如果我们直接将'@testing-library/jest-dom';导入到测试文件中,它就可以工作。但是,我们不想将此导入添加到每个测试文件中,我们希望全局配置它。

stszievb

stszievb1#

不知道这是否会有帮助,但我得到了一个类似的错误匹配器,如:
第一个月
然后我在文档中发现了以下内容:
如果您显式导入Jest API,则此页面中的TypeScript示例将仅按文档工作
基本上每个测试文件都需要这样的东西:
import {expect, jest, test} from '@jest/globals';
值得一读。

相关问题