【发布时间】:2023-03-22 13:16:01
【问题描述】:
我正在使用 Angular 4、Webpack 2.4.1、Karma 1.6 和 Jasmine 2.6.1,并且正在编写 ES2015 not TypeScript
我有一个很小的 Angular 演示应用程序,我想添加单元测试。演示应用程序本身正在运行,并且 Webpack 正在正确捆绑所有内容,但是当我尝试运行单元测试时,我在控制台中看到一些错误,如下所示:
ReferenceError:找不到变量:地图
在静态/js/app.welcome.js:2569
(app.welcome.js 是我的组件的名称)
Webpack 似乎正在正确构建我的测试包,Karma 服务器正在正确启动并且 PhantomJS 正在正确启动,但随后我看到了一些 Map 错误。
我肯定不在我自己的代码中使用 Map() 构造函数。
这是我的文件 -
app.welcome.js:
import {Component} from '@angular/core';
class WelcomeComponent {
constructor () {
this.welcomeMessage = 'Welcome to Angular 4';
}
}
WelcomeComponent.annotations = [
new Component({
selector: 'my-app',
template: '<h1>{{welcomeMessage}}</h1>'
})
];
export {WelcomeComponent};
app.welcome.spec.js:
import {TestBed} from '@angular/core/testing';
import {WelcomeComponent} from '../../js/app.welcome';
describe('The Welcome component', function () {
let component;
beforeEach(function() {
TestBed.configureTestingModule({
declarations: [WelcomeComponent]
});
const fixture = TestBed.createComponent(WelcomeComponent);
component = fixture.componentInstance;
});
it('should be a component', function() {
expect(component).toBeDefined();
});
it('should have a welcome message', function () {
expect(component.welcomeMessage).toEqual('Welcome to Angular 4');
});
});
karma.conf.js:
const webpack = require('webpack');
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'./Static/js/**/*.js',
'./Static/test/**/*.spec.js'
],
exclude: [
'./Static/js/main.js'
],
preprocessors: {
'./Static/js/**/*.js': ['webpack'],
'./Static/test/**/*.spec.js': ['webpack']
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['PhantomJS'],
singleRun: true,
concurrency: Infinity,
webpack: {
module: {
rules: [{
test: /\.js$/,
use: [{
loader: 'babel-loader',
options: { presets: ['es2015'] }
}]
}]
},
plugins: [
new webpack.ContextReplacementPlugin(/angular(\\|\/)core(\\|\/)@angular/, './src')
]
}
})
}
在阅读此处的其他答案后,我尝试将导入添加到我的测试文件中,例如 import 'zone.js'; 和 import 'core-js/es6';,但这并没有帮助。
我查看了Testing -ts - GUIDE,我似乎没有遗漏早期基本示例中的任何重要内容,但问题是所有官方文档都面向 TypeScript,而我想使用 ES2015。
我了解 Map 是 ES2015 中的一种新型对象,而不是错误所指示的变量。 Babel 不应该支持这个吗?
谁能帮忙?
【问题讨论】:
标签: javascript angular jasmine karma-runner webpack-2