"SyntaxError: Invalid or unexpected token at <Jasmine>" when running Angular tests

"SyntaxError: Invalid or unexpected token at <Jasmine>" when running Angular tests

我正在学习如何在 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
  });
};
// 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 and/or MortgageCalculatorService.

的导入而发生的

我按照下面的建议找到了问题所在。错误不在这个文件中,而是在 app.component.spec.ts 中。通过评论这个文件中的所有内容并逐行取消注释,我发现 Jasmine 不喜欢的是什么。在 html 由 app.module 导入的组件之一中,我使用了

(0).toFixed(2)

当我把它改成

'0.00'

茉莉不再抱怨