angular - “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
});
};
解决方案
// 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
。
推荐阅读
- python - VScode 版本与系统上最新的 python 版本不匹配
- python - 在 Django (Python) 应用程序中集成 PHP 工具
- android - 我的列表视图应用程序一次又一次地崩溃
- javascript - 如何按特定条件对 JavaScript 数组进行排序?
- openstack - 错误:启动实例时无法对实例“dane-srv”执行请求的操作(音量错误)
- react-native - react native reanimated中切换线程的问题
- numpy - rust-numpy 请求不同版本的 ndarray
- spring-kafka - 在 sping 云流中使用 @Valid 验证 pojo
- mongodb - 跨会话不可见对 mongo 集合的更改
- php - 我的错误是 laravel 中的未定义变量