You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular项目执行ng test报错:AssertionError [ERR_ASSERTION]

Angular 15执行ng test时出现AssertionError的解决方法

环境信息

  • Angular 15.1.0
  • Node版本:19.7.0
  • npm:9.5.1

错误信息

执行ng test时触发以下错误:

⠙ Generating browser application bundles (phase: setup)...node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

AssertionError [ERR_ASSERTION]: Compilation cannot be undefined.
    at webpackOptions.entry (/Users/***/Documents/folder/node_modules/@angular-devkit/build-angular/src/builders/karma/find-tests-plugin.js:70:38) {
  generatedMessage: false,
  code: 'ERR_ASSERTION',
  actual: undefined,
  expected: true,
  operator: '=='
}

问题分析

从提供的配置文件来看,核心问题出在package.json的test脚本配置错误,同时可能存在依赖冲突:

  1. 当前test脚本直接调用jasmine,跳过了Angular CLI的测试构建流程,导致webpack编译上下文未初始化,触发断言错误。
  2. 第三方依赖karma-typescript可能与Angular官方的测试构建工具不兼容。

解决方案

1. 修复package.json中的test脚本

修改package.json里的scripts.test为Angular CLI默认的测试命令:

"scripts": {
  "ng": "ng",
  "start": "ng serve",
  "build": "ng build",
  "watch": "ng build --watch --configuration development",
  "test": "ng test"
}

保存后重新执行ng test,这是解决该错误的最直接方法。

2. 清理并重新安装依赖

如果修改脚本后仍有问题,执行以下命令确保依赖版本一致:

rm -rf node_modules package-lock.json
npm install

3. 移除冲突依赖

尝试移除可能与Angular测试流程冲突的karma-typescript依赖:

npm uninstall karma-typescript

同时检查karma.conf.js,移除所有与karma-typescript相关的配置项,让Angular CLI默认配置接管测试流程。

附用户提供的配置文件

package.json

{
  "name": "epic-trip-app",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "jasmine"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^15.1.0",
    "@angular/cdk": "~15.1.5",
    "@angular/common": "^15.1.0",
    "@angular/compiler": "^15.1.0",
    "@angular/core": "^15.1.0",
    "@angular/forms": "^15.1.0",
    "@angular/material": "~15.1.5",
    "@angular/platform-browser": "^15.1.0",
    "@angular/platform-browser-dynamic": "^15.1.0",
    "@angular/router": "^15.1.0",
    "@datorama/akita": "^8.0.1",
    "karma-coverage": "^2.2.0",
    "karma-jasmine-html-reporter": "^2.0.0",
    "karma-spec-reporter": "^0.0.36",
    "karma-typescript-mock": "^1.0.1",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "typescript-core": "^9.2.0",
    "zone.js": "~0.12.0"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^15.1.6",
    "@angular/cli": "~15.1.6",
    "@angular/compiler-cli": "^15.1.0",
    "@types/jasmine": "~4.3.0",
    "jasmine": "^4.5.0",
    "jasmine-core": "~4.5.0",
    "jasmine-ts": "^0.4.0",
    "karma": "^6.1.0",
    "karma-chrome-launcher": "^3.1.1",
    "karma-jasmine": "^5.1.0",
    "karma-typescript": "^5.5.3",
    "ts-node": "^10.9.1",
    "typescript": "~4.9.4"
  }
}

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "EpicTripApp": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss",
          "skipTests": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/epic-trip-app",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": [
              "zone.js"
            ],
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "@angular/material/prebuilt-themes/indigo-pink.css",
              "src/styles.scss",
              "src/theme.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "EpicTripApp:build:production"
            },
            "development": {
              "browserTarget": "EpicTripApp:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "EpicTripApp:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "polyfills": [
              "zone.js",
              "zone.js/testing"
            ],
            "tsConfig": "tsconfig.spec.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "@angular/material/prebuilt-themes/indigo-pink.css",
              "src/styles.scss"
            ],
            "scripts": [],
            "karmaConfig": "karma.conf.js"
          }
        }
      }
    }
  },
  "cli": {
    "analytics": "b1288342-9e70-43a2-9613-00151ad6c303"
  }
}

内容的提问来源于stack exchange,提问作者Xildrite

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 04:37:06