【问题标题】:_setAsap error when loading angular2-polyfilles.js in Karma unit test with Angular2 beta and SystemJS使用 Angular2 beta 和 SystemJS 在 Karma 单元测试中加载 angular2-polyfilles.js 时出现 _setAsap 错误
【发布时间】:2016-04-21 10:56:43
【问题描述】:

运行 Karma 单元测试时,我收到以下错误:

Chrome 47.0.2526 (Linux 0.0.0) 错误
未捕获的类型错误:无法读取未定义的属性“_setAsap”
评估路径/node_modules/angular2/bundles/angular2-polyfills.min.js
加载路径/node_modules/angular2/bundles/angular2-polyfills.min.js时出错

karma.conf.js 包含如下内容:

// Karma configuration
// Generated on Tue Dec 08 2015 14:19:08 GMT-0600 (CST)
module.exports = function(config) {
    config.set({
        // base path that will be used to resolve all patterns (eg. files, exclude)
        basePath: '../',
        files: [
            'node_modules/angular2/bundles/angular2-polyfills.min.js',
            'node_modules/es6-shim/es6-shim.js',
            'node_modules/systemjs/dist/system.js',
            'node_modules/rxjs//Rx.js',
            'node_modules/angular2/bundles/angular2.min.js',
            'dist/components/uploader.js',
            'dist/components/uploader.spec.js'
        ],
        ...

uploader.js 是从 TypeScript 编译的 JavaScript 文件,是一个 Angular2 组件。 Uploader.spec.js 是单元测试。我使用 Karma、SystemJS 和 Angular2 beta 来尝试设置单元测试。

查看 _angular2_polyfills.js_ 时,_setAsap 似乎指向模块 es6-promise。但是,即使我在文件列表中添加了node_modules/es6-promise/dist/es6-promise.js,它也不起作用。

【问题讨论】:

    标签: angular karma-jasmine systemjs


    【解决方案1】:

    所以,我遇到了类似的问题,我不得不将它添加到我的 system.conf.js 文件中:

    System.config({
        paths: {
            'agular2-polyfills': 'node_modules/angular2/bundles/angular2-polyfills.js',
            'typescript': 'node_modules/typescript/lib/typescript.js',
            'systemjs': 'node_modules/systemjs/dist/system.js',
            'system-polyfills': 'node_modules/systemjs/dist/system-polyfills.js',
            'es6-module-loader': 'node_modules/es6-module-loader/dist/es6-module-loader.js'
        },
        map: {
            angular2: 'node_modules/angular2',
            rxjs: 'node_modules/rxjs',
            crypto: '@empty'// this fixed my last issue
        },
        meta: {
            'path/to/components/*': {
                format: 'register'
            }
        }
    });
    

    如果我没记错的话(我没有声称自己是 systemjs ninja)在查看了几个堆栈问题并阅读了config docs 之后,我找到了这个解决方案,但我不记得某些原因除了必须使用 karma 加载所有内容之外。

    我的 karma.conf 文件数组:

    // list of files / patterns to load in the browser
        files: [
          'node_modules/reflect-metadata/Reflect.js',
          '*.spec.js',
          '**/*.spec.js',
          '**/**/*.spec.js'
        ]
    

    和 karma.conf systemjs 配置:

    systemjs: {
        // Path to your SystemJS configuration file 
        configFile: 'system.conf.js',
    
        // Patterns for files that you want Karma to make available, but not loaded until a module requests them. eg. Third-party libraries. 
        serveFiles: [
            'client/**/*.js',
            'server/**/*.js',
        ],
    
        // SystemJS configuration specifically for tests, added after your config file. 
        // Good for adding test libraries and mock modules 
        config: {
            transpiler: null,
            defaultJSExtensions: true
        },
    
        meta: {
            'angular2/angular2':{
                format: 'register'
            }
        }
    },
    

    重要提示 这可能不是最好的方法,甚至不是最好的方法,但这就是 让它工作的方式。我还没有完成我项目的这一部分的重构阶段,因为我会在我完成项目的第一阶段之后进行

    祝你好运!

    【讨论】:

    • 我确实之前设置了 Karma 单元测试以使用 angular2 2.0.0-alpha,其中应用程序代码仅从“angular2/angular2”导入。就像您在 system.config.js 文件中指出的那样,一个关键是将“angular2/angular2”配置为“注册”格式。在我升级到 angular2 2.0.0-beta.1 后出现问题,其中 angular2 变为“angular2/core”。您的应用程序使用哪个版本的 angular2?
    • 我现在可以配置一些东西来运行我的 Karma 单元测试。我使用了您在 karma.conf.js 中包含 Reflect.js(消除 angular2-polyfills.js)的建议,并在 system.config.js 中使用了 crypto: '@empty'。我还有其他问题,比如我无法让 Karma 正确加载 app.js,因为该文件是从 app.ts 编译的,而 app.ts 是从“angualr/platform/browser”导入的。但我现在可以排除它,让我的组件单元测试运行。
    • 酷,很高兴它成功了。如果您认为我的答案是正确答案或有帮助,请随时将我的答案标记为正确和/或投赞成票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 2017-04-27
    相关资源
    最近更新 更多