gpt4 book ai didi

angular - 运行 ng 测试时如何解析 test.ts?

转载 作者:太空狗 更新时间:2023-10-29 17:35:38 34 4
gpt4 key购买 nike

我正在使用 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'

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

// 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:

    {
"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 并按预期运行命令。

关于angular - 运行 ng 测试时如何解析 test.ts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46282174/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com