运行 ng test 时如何解析 test.ts?

2024-04-11

我正在使用 Angular CLI,当我运行时ng test,它开始启动浏览器,然后我突然收到错误:

userName@UserName:~/devApp/profiles$ng test
10% building modules 1/1 modules 0 active18 09 2017 16:16:51.891:WARN [karma]: No captured browser, open http://localhost:9876/
18 09 2017 16:16:51.903:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0:9876/
18 09 2017 16:16:51.903:INFO [launcher]: Launching browser Chrome with unlimited concurrency
18 09 2017 16:16:51.915:INFO [launcher]: Starting browser Chrome

ERROR in Entry module not found: Error: Can't resolve '/Users/xxx/devApp/profiles/src/test.ts' in '/Users/xxx/devApp/profiles/node_modules/@angular/cli/models/webpack-configs'

这是文件:test.ts当我设置项目时通过 Angular CLI 生成的:

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

// Unfortunately there's no typing for the `__karma__` variable. Just declare it as any.
declare var __karma__: any;
declare var require: any;

// Prevent Karma from running prematurely.
__karma__.loaded = function () {};

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
// Finally, start Karma to run the tests.
__karma__.start();

角度-cli.json:

    {
  "project": {
    "name": "profiles"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "testTsconfig": "tsconfig.json",
      "prefix": "app",
      "styles": [
        "styles.css",
        "custom_theme.scss"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "files": "src/**/*.ts",
      "project": "src/tsconfig.json"
    },
    {
      "files": "e2e/**/*.ts",
      "project": "e2e/tsconfig.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "class": {
      "spec": false
    },
    "component": {
      "spec": true,
      "inlineStyle": false,
      "inlineTemplate": false
    }
  }
}

错误是在karma.conf.js文件作为路径test.ts指着src错误的文件夹:

...
files: [
    { pattern: './src/test.ts', watched: false }
],
preprocessors: {
    './src/test.ts': ['@angular/cli']
}
..., 

解决方案:我已经删除了该文件test.ts从根到/src文件夹:/src/test.ts并且它按预期运行命令。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

运行 ng test 时如何解析 test.ts? 的相关文章

随机推荐