angular - 错误 TS1127:在 Angular 7 中运行 Karma 测试时出现无效字符
问题描述
error TS1127: Invalid character
在为 Angular 7 应用程序运行 Karma 测试时,我在 Visual Studio Code 终端中遇到错误- CLI 版本-
7.3.9
- 我在应用程序中有一个 Karma 测试规范
- (我删除
spec
了 Angular CLI 生成的所有文件,但保留了现有的 Jasmine 和 Karma npm 包) - Jasmine 是测试运行器
- 这似乎不是无效字符或空格问题 - 这不起作用 - Angular 编译错误:src/app/login/login.component.ts(18,10): error TS1127: Invalid > character
要使用 ngentest 生成测试,请从 Windows 上的 Visual Studio Code 终端执行此操作:
npm install ngentest -g # to run this command anywhere
ngentest app.component.ts > # write unit test to app.component.spec.ts
app.component.spec.ts
// tslint:disable
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Injectable, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { By } from '@angular/platform-browser';
// import { Observable } from 'rxjs/Observable';
// import 'rxjs/add/observable/of';
// import 'rxjs/add/observable/throw';
import {Component, Directive} from '@angular/core';
import {AppComponent} from './app.component';
import {DataService} from './services/data.service';
import {ToastrService} from 'ngx-toastr';
import {LocaleService} from './services/locale.service';
import {BsLocaleService} from 'ngx-bootstrap/datepicker';
@Injectable()
class MockDataService { }
@Injectable()
class MockLocaleService { }
describe('AppComponent', () => {
let fixture;
let component;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
providers: [
{provide: DataService, useClass: MockDataService},
ToastrService,
{provide: LocaleService, useClass: MockLocaleService},
BsLocaleService,
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
}).compileComponents();
fixture = TestBed.createComponent(AppComponent);
component = fixture.debugElement.componentInstance;
});
it('should create a component', async () => {
expect(component).toBeTruthy();
});
it('should run #ngOnInit()', async () => {
// const result = component.ngOnInit();
});
it('should run #ngOnDestroy()', async () => {
// const result = component.ngOnDestroy();
});
});
使用以下命令从 CLI 运行测试:
npm install
npm run build
ng test
app.component.spec.ts TS1127: Invalid character
错误然后显示在控制台中
这出现在 Chrome 中运行的 Karma 测试报告查看器中:
Incomplete: No specs found, , randomized with seed 42068
我尝试运行npm install –save-dev jasmine
,其中添加了"jasmine": "^3.4.0"
. 我做了一个npm install
,构建并ng test
再次 - 同样的问题
现在devDependencies
是package.json
:
"devDependencies": {
"@angular-devkit/build-angular": "^0.13.9",
"@angular-devkit/core": "7.3.8",
"@angular/cli": "^7.3.9",
"@angular/compiler-cli": "^7.2.15",
"@angular/language-service": "^7.2.15",
"@fortawesome/pro-light-svg-icons": "^5.10.2",
"@fortawesome/pro-regular-svg-icons": "^5.10.2",
"@fortawesome/pro-solid-svg-icons": "^5.10.2",
"@types/jasmine": "~3.3.12",
"@types/jasminewd2": "^2.0.6",
"@types/node": "^11.13.7",
"acorn": "^6.1.1",
"codelyzer": "~5.0.0",
"concurrently": "^4.1.2",
"jasmine": "^3.4.0",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-cli": "~2.0.0",
"karma-coverage-istanbul-reporter": "^2.0.5",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"ncp": "^2.0.0",
"ngx-i18nsupport": "^0.17.1",
"protractor": "^5.4.2",
"ts-node": "~8.1.0",
"tslint": "^5.16.0",
"typescript": "^3.2.4",
"webpack-bundle-analyzer": "^3.3.2"
}
更新
我卸载了所有与 jasmine 和 karma 相关的devDependencies
东西并重新安装了它们,并且还做了一个npm install ngentest
(它没有安装,但是 CLI 的生成没有这个)。然后我跑了一个npm install
. 然后我删除了 app.component.spec.ts 并做了npm run build
. 然后我做了:
ngentest app.component.ts > app.component.spec.ts
npm run build
ng test
同样的错误
解决方案
我第一次使用 vscode 的 gentest 时遇到了同样的问题。简单的解决方法:直接从命令行使用 gentest。
问题和解决方法:在 notepad++ 中打开创建的 .spec 文件,并从 vscode 为您选择的任何奇怪的东西中进行隐蔽编码并保存为 UTF-8。
推荐阅读
- c++ - OpenCL enqueueReadBuffer 抛出资源错误
- java - 有没有办法使用另一种类型的流即时生成和收集对象?
- python - 如何修复无法与通过 Heroku 托管的 Discord Bot 一起使用的命令
- powershell - 从给定的组列表中提取用户并使用 powershell 将数据提取到文本中的每个新组名
- javascript - 在没有 JQuery 的情况下反应 lightgallery.js 集成?
- loopbackjs - Loopback 4 生产准备好了吗?
- amazon-web-services - 部署多个 AWS Lambda 函数
- sql - 计算每个多边形中的点数
- rest - How to generate token for rest call in codeigniter?
- python - Pandas 不会将分类数据 [sex] 更改为数值 [0/1]