Angular 14 + Jest - typescript_1.default.canHaveDecorators 不是函数

标签 angular jestjs ts-jest angular-test angular-testing-library

使用 Angular 14 配置 Jest 后,我​​收到此错误: 测试套件运行失败

TypeError: typescript_1.default.canHaveDecorators is not a function

  at TypeScriptReflectionHost.getDecoratorsOfDeclaration (node_modules/jest-preset-angular/build/ngtsc/reflection/src/typescript.js:14:49)
  at visitNodes (node_modules/typescript/lib/typescript.js:87646:48)
  at visitLexicalEnvironment (node_modules/typescript/lib/typescript.js:87686:22)
  at Object.visitEachChild (node_modules/typescript/lib/typescript.js:88234:55)
  at transformSourceFileOrBundle (node_modules/typescript/lib/typescript.js:88930:57)
  at transformation (node_modules/typescript/lib/typescript.js:107888:24)

Jest 配置是:

import type { Config } from 'jest';

const jestConfig: Config = {
    preset: 'jest-preset-angular',
    verbose: true,
    setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
    globalSetup: 'jest-preset-angular/global-setup',

    resolver: 'jest-preset-angular/build/resolvers/ng-jest-resolver.js',
     transformIgnorePatterns: ['node_modules/(?!@angular)'],
     transform: {
        '^.+\\.(ts|js|mjs|html|svg)$': 'jest-preset-angular',
     },

     
    globals: {
      'ts-jest': {
        useESM: true,
        tsconfig: '<rootDir>/tsconfig.spec.json',
        stringifyContentPathRegex: '\\.(html|svg)$'
      }
    },
    coverageDirectory: 'coverage',
    testResultsProcessor: 'jest-sonar-reporter',
    snapshotSerializers: [
      'jest-preset-angular/build/serializers/no-ng-attributes',
      'jest-preset-angular/build/serializers/ng-snapshot',
      'jest-preset-angular/build/serializers/html-comment'
    ]
};

export default jestConfig;

在 setup-jest.ts 中我们有:

import 'jest-preset-angular/setup-jest';

和package.json:

 "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --configuration production",
    "watch": "ng build --watch --configuration development",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:ci": "jest --runInBand"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^14.2.12",
    "@angular/cdk": "^14.2.7",
    "@angular/common": "^14.2.12",
    "@angular/compiler": "^14.2.12",
    "@angular/core": "^14.2.12",
    "@angular/forms": "^14.2.12",
    "@angular/localize": "^14.2.12",
    "@angular/material": "^14.2.7",
    "@angular/platform-browser": "^14.2.12",
    "@angular/platform-browser-dynamic": "^14.2.12",
    "@angular/router": "^14.2.12",
    "rxjs": "~7.5.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-builders/custom-webpack": "^14.1.0",
    "@angular-devkit/build-angular": "^14.2.10",
    "@angular/cli": "^14.2.10",
    "@angular/compiler-cli": "^14.2.12",
    "@types/jest": "^29.2.5",
    "@types/mustache": "^4.1.3",
    "@types/node": "^12.11.1",
    "jest": "^28.1.3",
    "jest-environment-jsdom": "^29.3.1",
    "jest-preset-angular": "^12.2.4",
    "jest-sonar-reporter": "^2.0.0",
    "mustache": "^4.2.0",
    "prettier": "^2.7.1",
    "ts-jest": "^29.0.5",
    "ts-node": "^10.9.1",
    "typescript": "~4.6.2"
  }

此外,tsconfig.spec.ts 已正确配置:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "module": "CommonJs",
    "types": ["jest", "node"]
  },
  "files": [
    "src/polyfills.ts",
    "setup-jest.ts"
  ],
  "include": [
    "jest.config.ts",
    "src/**/*.spec.ts",
    "src/**/*.d.ts"
  ]
}

并且 tsconfig.json 包含 esModuleInterop:

/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./src",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": false,
    "strict": false,
    "noImplicitOverride": false,
    "noPropertyAccessFromIndexSignature": false,
    "noImplicitReturns": false,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2020",
    "module": "es2020",
    "lib": ["es2020", "dom"],
    "esModuleInterop": true
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": false,
    "strictInputAccessModifiers": false,
    "strictTemplates": false
  }
}

我尝试按照官方指南安装 Jest,Karma + Jasmine 也被删除。

有人遇到过这种情况吗?我在这里缺少什么?

最佳答案

我终于成功了。 该修复是将 typescript 版本从 4.6.2 升级到 4.8.2

关于Angular 14 + Jest - typescript_1.default.canHaveDecorators 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75163809/

相关文章:

typescript - 为什么将 jest 升级到 29.5.0 会破坏我的模拟?

Typescript/如何预期固定日期?

javascript - 如何在 Github Pages 上使用 Angular 2 建立网站

html - Angular Material Nav Sidebar 仅在响应式调整大小时显示

angular - forkJoin 上的 takeUntil 是否会调用 forkJoined observables 上的函数?

angular - 来自同一惰性路由的多个组件不起作用

javascript - 如何在 enzyme 中渲染假 DOM 中的组件?

javascript - Jest/ react - TypeError : Cannot read property 'touchStart' of undefined

javascript - 测试数组是否为空或包含某个对象

reactjs - 运行测试时如何解析 Jest 中的 "Cannot use import statement outside a module"?