- 我收到错误
error TS1127: Invalid character
在 Visual Studio Code 终端中,为 Angular 7 应用运行 Karma 测试时
-
命令行版本 -
7.3.9
- I have 一份 Karma 测试规范在应用程序中
- (我删除了所有
spec
Angular CLI 生成的文件,但保留了现有的 Jasmine 和 Karma npm 包)
- Jasmine 是测试运行者
- 这似乎不是无效字符或空格问题 - 这不起作用 -Angular 编译错误:src/app/login/login.component.ts(18,10):错误 TS1127:无效 > 字符 https://stackoverflow.com/questions/47584678/angular-compile-error-src-app-login-login-component-ts18-10-error-ts1127-in
我使用自动生成测试ngentest https://www.npmjs.com/package/ngentest
要使用 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
应用程序.组件.规格.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
再次 - 同样的问题
The devDependencies
in 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"
}
UPDATE
我卸载了所有与茉莉花和业力相关的内容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
同样的错误