【问题标题】:"SyntaxError: Invalid or unexpected token at <Jasmine>" when running Angular tests运行 Angular 测试时出现“SyntaxError:<Jasmine> 处的令牌无效或意外令牌”
【发布时间】:2021-12-04 23:38:38
【问题描述】:

我正在学习如何在 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
  });
};

【问题讨论】:

    标签: angular typescript testing jasmine karma-jasmine


    【解决方案1】:

    通过执行以下建议,我设法找到了问题所在。错误不在此文件中,而是在 app.component.spec.ts 中。通过注释此文件中的所有内容并逐行取消注释,我发现 Jasmine 不喜欢什么。在 app.module 导入的组件之一的 html 中,我曾使用过

    (0).toFixed(2)
    

    当我把它改成

    '0.00'
    

    茉莉不再抱怨

    【讨论】:

      【解决方案2】:
      // 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 的导入而发生的。

      【讨论】:

      • 感谢您的回答。通过做你所做的,我设法找到了问题所在。错误不在此文件中,而是在 app.component.spec.ts 中。通过注释此文件中的所有内容并逐行取消注释,我发现 Jasmine 不喜欢什么。在 app.module 导入的组件之一的 html 中,我使用了 (0).toFixed(2);当我将其更改为“0.00”时,茉莉不再抱怨
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 2021-09-21
      • 2020-07-12
      • 2017-09-29
      • 2018-12-03
      • 2021-08-14
      相关资源
      最近更新 更多