首页 > 解决方案 > Jest Angular 测试给出业力错误`发生未处理的异常:找不到模块'业力'`

问题描述

我正在尝试在 Angular 中使用玩笑测试。我已经完成了这里描述的所有配置在此处 输入链接描述

这是我的 package.json

    {
  "name": "jest-test",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:ci": "jest -ci --runInBand",
    
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~10.2.0",
    "@angular/common": "~10.2.0",
    "@angular/compiler": "~10.2.0",
    "@angular/core": "~10.2.0",
    "@angular/forms": "~10.2.0",
    "@angular/platform-browser": "~10.2.0",
    "@angular/platform-browser-dynamic": "~10.2.0",
    "@angular/router": "~10.2.0",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1002.0",
    "@angular/cli": "~10.2.0",
    "@angular/compiler-cli": "~10.2.0",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jest": "^26.6.3",
    "jest-preset-angular": "^8.3.2",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.0.2"
  },
  "jest": {
    "preset": "jest-preset-angular",
    "setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.ts",
    "transform": {
      "^.+\\.(ts|html)$": "ts-jest",
      "^.+\\.js$": "babel-jest"
    }
  }
}

这是 tsconfig.spec.ts

  /* To learn more about this file see: https://angular.io/config/tsconfig. */
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "types": [
      "node",
      "jest",
      "jsdom"
    ]
  },
  "files": [
    "src/test.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.spec.ts",
    "src/**/*.d.ts"
  ]
}

我还添加了 setupjest.ts。我只有 import 'jest-preset-angular';

现在,当我运行 ng test 时,它给出了 karma not found 错误。

标签: angularunit-testingjestjsts-jest

解决方案


我建议您使用 briebug jest 原理图将 jest 添加到您的项目(https://www.npmjs.com/package/@briebug/jest-schematic),因为它是维护的,而不是使用 2 年前的博客文章。


推荐阅读