运行ng test时如何解析test.ts? [英] How to resolve test.ts when running ng test?

查看:390
本文介绍了运行ng test时如何解析test.ts?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Angular CLI and when I run ng test, it starts launching the browser, then I suddenly get an error:

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生成的文件:

Here is the file: test.ts as generated via Angular CLI when I did set up the project:

// 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();

angular-cli.json:

angular-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
    }
  }
}

推荐答案

由于 test.ts 的路径指向 src 文件夹,这是错误的.:

The error was in karma.conf.js file as the path to test.ts was pointing to src folder which is wrong.:

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

解决方案:我已经从根目录下的文件 test.ts 删除了 /src 文件夹: /src/test.ts 并运行了命令如预期的那样.

Solution: I had remove the file test.ts from root to /src folder: /src/test.ts and it runs the command as expected.

这篇关于运行ng test时如何解析test.ts?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆