ReferenceError:找不到变量:Map [英] ReferenceError: Can't find variable: Map

查看:891
本文介绍了ReferenceError:找不到变量:Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular 4,Webpack 2.4.1,Karma 1.6和Jasmine 2.6.1,我正在编写ES2015 TypeScript

I'm using Angular 4, Webpack 2.4.1, Karma 1.6 and Jasmine 2.6.1 and am writing ES2015 not TypeScript

我有一个小角度演示应用程序,我想添加单元测试。演示应用程序本身正在运行,Webpack正在捆绑所有内容,但是当我尝试运行单元测试时,我在控制台中看到一些错误,如下所示:

I've got a tiny angular demo app and I want to add unit tests. The demo app itself itself is working and Webpack is bundling everything correctly, but when I try to run the unit tests I see some errors in the console like this:


ReferenceError:在Static / js / app.welcome.js找不到变量:Map

ReferenceError: Can't find variable: Map

:2569

app.welcome.js 是我的组件的名称)

Webpack似乎正在构建我的测试包,Karma服务器正常启动并且PhantomJS正确启动,但后来我看到了几个Map错误。

Webpack appears to be building my test bundle properly, Karma server is starting up correctly and PhantomJS is launching correctly, but then I see several of the Map errors.

我是肯定在我自己的代码中没有使用 Map()构造函数。

I'm definitely not using the Map() constructor 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')
             ]
         }
     })
}

我尝试将导入添加到我的测试文件喜欢导入'zone.js'; 导入'core-js / es6'; 在阅读其他答案后,但这没有帮助。

I've tried adding imports to my test file like import 'zone.js'; and import 'core-js/es6'; after reading other answers here, but this has not helped.

我看了 Testing -ts - GUIDE 我似乎没有遗漏任何早期基本示例中的任何内容,但问题是所有的官方文档面向TypeScript,而我想使用ES2015。

I've looked through Testing -ts - GUIDE and I don't appear to be missing anything huge from the earlier basic examples, but the problem is that all the official docs are geared towards TypeScript, while I want to use ES2015.

我知道Map是ES2015中的一种新类型的对象,而不是错误所指示的变量。 Babel不应该支持这个吗?

I understand that Map is an new type of object in ES2015 and not a variable as indicated by the error. Shouldn't Babel support this though?

任何人都可以帮忙吗?

推荐答案

抛出此错误是因为浏览器中没有 Map 。 PhantomJS用作Karma驱动程序,它不支持ES6功能。

This error is thrown because there's no Map in browsers. PhantomJS is used as Karma driver, and it doesn't support ES6 features.

如果polyfills(例如 core-js )没有加载到测试中包含的文件中,它们应该单独加载,例如通过 karma-es6-shim 插件:

If polyfills (e.g. core-js) aren't loaded in files that were included in tests, they should be loaded separately, for example via karma-es6-shim plugin:

    ...
    frameworks: ['es6-shim', 'jasmine'],
    ...

这篇关于ReferenceError:找不到变量:Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆