,angular,typescript,testing,jasmine,karma-jasmine"/>

首页 > 解决方案 > “SyntaxError:无效或意外的令牌在" 运行 Angular 测试时

问题描述

我正在学习如何在 Angular 上进行测试,但似乎我在这里碰壁了。我已经按照文档并在 Google 上进行了搜索,但找不到解决方案。错误消息非常神秘。“无效或意外的令牌”,但没有关于它在哪里或是什么的信息。

错误信息:

    SyntaxError: Invalid or unexpected token
        at <Jasmine>
        at newTrustedFunctionForJIT (node_modules/@angular/compiler/fesm2015/compiler.js:6832:1)
        at JitEvaluator.evaluateCode (node_modules/@angular/compiler/fesm2015/compiler.js:6913:1)
        at JitEvaluator.evaluateStatements (node_modules/@angular/compiler/fesm2015/compiler.js:6883:1)
        at CompilerFacadeImpl.jitExpression (node_modules/@angular/compiler/fesm2015/compiler.js:20275:1)
        at CompilerFacadeImpl.compileComponentFromMeta (node_modules/@angular/compiler/fesm2015/compiler.js:20240:1)
        at CompilerFacadeImpl.compileComponent (node_modules/@angular/compiler/fesm2015/compiler.js:20229:1)
        at Function.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:27407:1)
        at getComponentDef (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:1130:1)
        at node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:27205:1

我已经评论了除此之外的每个测试文件,并将测试重写为多余的,但它仍然失败。我的规格文件:

import { TestBed } from '@angular/core/testing';
import { CoreModule } from '../core.module';
import { MortgageCalculatorService } from './mortgage-calculator.service';

describe('Mortgage calculator service', () => {
  let service: MortgageCalculatorService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [CoreModule],
      providers: [MortgageCalculatorService]
    });
    service = TestBed.inject(MortgageCalculatorService);
  });

  it('should be created', () => {
    const a = 'aa';
    expect(a).toEqual('aa');
  });
});

MortgageCalculatorService 是 CoreModule 中提供的服务:

@NgModule({
  declarations: [
  ],
  imports: [
    CommonModule
  ],
  providers: [
    // services
    MortgageCalculatorService,
    // adapters
    MortgageAdapter
  ]
})
export class CoreModule { }

最后是 karma.conf.js 文件,每个参数都是默认的

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      jasmine: {
        // you can add configuration options for Jasmine here
        // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html
        // for example, you can disable the random execution with `random: false`
        // or set a specific seed with `seed: 4321`
      },
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    jasmineHtmlReporter: {
      suppressAll: true // removes the duplicated traces
    },
    coverageReporter: {
      dir: require('path').join(__dirname, './coverage/max-house-price'),
      subdir: '.',
      reporters: [
        { type: 'html' },
        { type: 'text-summary' }
      ]
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    restartOnFileChange: true
  });
};

标签: angulartypescripttestingjasminekarma-jasmine

解决方案


// import { TestBed } from '@angular/core/testing';
// import { CoreModule } from '../core.module';
// import { MortgageCalculatorService } from './mortgage-calculator.service';

 describe('Mortgage calculator service', () => {
  // let service: MortgageCalculatorService;

  // beforeEach(() => {
    // TestBed.configureTestingModule({
      // imports: [CoreModule],
      // providers: [MortgageCalculatorService]
    // });
    // service = TestBed.inject(MortgageCalculatorService);
  // });

  it('should be created', () => {
    const a = 'aa';
    expect(a).toEqual('aa');
  });
});

尝试注释掉上面的行。如果你得到同样的错误,这意味着它很可能是 Jasmine/Karma 问题。如果您没有收到相同的错误,我认为错误是由于CoreModule和/或的导入而发生的MortgageCalculatorService


推荐阅读