错误TS1127:在Angular 7中运行Karma测试时,字符无效 [英] error TS1127: Invalid character when running Karma tests in Angular 7
问题描述
- 在为Angular 7应用运行Karma测试时,我在Visual Studio Code终端中出现错误
error TS1127: Invalid character
- CLI版本-
7.3.9
- 我在应用程序中有一个一个Karma测试规范
- (我删除了Angular CLI生成的所有
spec
文件,但保留了现有的Jasmine和Karma npm软件包) - 茉莉花是测试跑步者
- 这似乎不是无效的字符或空格问题-这不起作用-
- I'm getting errors
error TS1127: Invalid character
in the Visual Studio Code terminal, when running a Karma test for an Angular 7 app - CLI version -
7.3.9
- I have one single Karma test specification in the app
- (I removed all of the
spec
files that the Angular CLI generates, but left the existing Jasmine and Karma npm packages in place) - Jasmine is the test runner
- This doesn't seem to be an invalid character or whitespace issue - this didn't work - Angular compile error: src/app/login/login.component.ts(18,10): error TS1127: Invalid > character
我使用 ngentest
要使用ngentest生成测试,请在Windows的Visual Studio Code终端上执行此操作:
To generate a test using ngentest, do this from the Visual Studio Code terminal, on Windows:
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运行测试:
run the tests from the CLI using:
-
npm install
-
npm run build
-
ng test
npm install
npm run build
ng test
app.component.spec.ts TS1127: Invalid character
错误,然后在控制台中显示
app.component.spec.ts TS1127: Invalid character
errors then display in the console
它出现在运行于Chrome中的Karma测试报告查看器中:
this appears in the Karma test report viewer that runs in Chrome:
Incomplete: No specs found, , randomized with seed 42068
我尝试运行npm install –save-dev jasmine
,它添加了"jasmine": "^3.4.0"
.我做了一个npm install
,再次构建并ng test
-同样的问题
I tried running npm install –save-dev jasmine
, which added "jasmine": "^3.4.0"
. I did an npm install
, build and ng test
again - same issue
package.json
中的devDependencies
现在是:
"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"
}
更新
我卸载了所有与茉莉花和业力有关的devDependencies
并重新安装了它们,还做了npm install ngentest
(未安装,但是从CLI生成的版本没有此功能).然后,我运行了npm install
.然后,我删除了app.component.spec.ts并做了npm run build
.然后我做了:
I uninstalled all of the jasmine and karma related devDependencies
and reinstalled them, and also did an npm install ngentest
(it wasn't installed but the generation from the CLI worked without this). I then ran an npm install
. I then removed app.component.spec.ts and did npm run build
. I then did:
ngentest app.component.ts > app.component.spec.ts
npm run build
ng test
相同错误
推荐答案
我一次也唯一一次在vscode中使用gentest时遇到了同样的问题. 简单的解决方法:直接从命令行使用gentest.
I've had the same problem the one and only time I used gentest from vscode. Simple workaround: Use gentest from command line directly.
问题和解决方案:在notepad ++中打开创建的.spec文件,然后从为您选择的任何奇怪的vscode代码中进行隐式编码,然后保存到UTF-8.
Problem and resolution: Open the created .spec file in notepad++ and covert encoding from whatever weird stuff vscode chose for you and save to UTF-8.
这篇关于错误TS1127:在Angular 7中运行Karma测试时,字符无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!