I am using Angular 4, Webpack 2.4.1, Karma 1.6 and Jasmine 2.6.1 and I am writing ES2015 not TypeScript
I have a small Angular demo application and I want to add unit tests. The daemon application itself works, and Webpack binds everything correctly, but when I try to run unit tests, I see some errors in the console:
ReferenceError: Cannot find variable: Map
at Static / js / app.welcome.js: 2569
(app.welcome.js is the name of my component)
Webpack seems to build the test suite correctly, the Karma server starts up correctly, and PhantomJS starts up correctly, but then I see a few errors in Map.
I'm definitely not using a constructor Map()in my own code.
Here are my files -
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')
]
}
})
}
I tried adding import to my test file, for example import 'zone.js';, import 'core-js/es6';after reading the other answers here, but that did not help.
I looked at Testing -ts - GUIDE and it seems that I am not missing something huge from the previous base examples, but the problem is that all the official documents are TypeScript oriented while I want to use ES2015.
I understand that Map is a new type of object in ES2015, and not a variable, as indicated in the error. Shouldn't Babel support this?
Can anyone help?