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

Time:10-24

I'm learning how to test on Angular, but it seems I've hit a brick wall here. I've followed the documentation and searched on Google but I can't find a solution. The error message is very cryptic. "Invalid or unexpected token", but no info on where or what it is.

Error message:

    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

I've commented every test file except for this one, and rewrote the test to be redundant, yet it still fails. My specs file:

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 is a service provided in CoreModule:

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

Finally, karma.conf.js file, with every parameter as default

// 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
  });
};

CodePudding user response:

// 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');
  });
});

Try commenting out the above lines. If you get the same error, that means it is most likely a Jasmine/Karma issue. If you don't get the same error, I think the error is happening because of the imports of CoreModule and/or MortgageCalculatorService.

CodePudding user response:

I managed to find what the problem was by doing what was suggested below. the error was not in this file but in app.component.spec.ts. by commenting everything in this file and uncommeting line by line i found what was it that Jasmine did not like. in the html one of the components imported by app.module, i had used

(0).toFixed(2)

when i changed it to

'0.00'

jasmine stopped complaining

  • Related