首页 > 解决方案 > 错误 TS1127:在 Angular 7 中运行 Karma 测试时出现无效字符

问题描述

我使用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

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 运行测试:

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再次 - 同样的问题


现在devDependenciespackage.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

同样的错误

标签: angularkarma-jasminengentest

解决方案


我第一次使用 vscode 的 gentest 时遇到了同样的问题。简单的解决方法:直接从命令行使用 gentest。

问题和解决方法:在 notepad++ 中打开创建的 .spec 文件,并从 vscode 为您选择的任何奇怪的东西中进行隐蔽编码并保存为 UTF-8。


推荐阅读