【问题标题】:Using AngularJS in Jasmine tests with Karma在 Jasmine 测试中使用 AngularJS 和 Karma
【发布时间】:2017-01-20 11:30:23
【问题描述】:

我正在使用 Jasmine 和 Karma 为 Angular 1.5.7 应用程序编写测试。

我应该如何从 karma.conf.js 文件或 test-main.js 文件中引用 AngularJS,以确保它可用于我的测试?

我目前可以运行单元测试,但是当测试引用的文件引用 AngularJS(即import angular from 'angular')时,找不到 AngularJS,这表明我需要额外的配置才能使其工作。

我正在使用 babel 转译为 ES5,并将导入转换为 RequireJS AMD。

错误是:

 ERROR: 'There is no timestamp for /base/angular.js!'

如果我将 AngularJS 添加到 test-main.js 的路径部分,则会收到不同的错误:

TypeError: undefined is not an object (evaluating '_angular2.default.module')

如果我从测试目标语句中删除import angular from 'angular';,则会收到以下错误:

ReferenceError: Can't find variable: angular

我的test-main:

var TEST_REGEXP = /(spec|test)\.js$/i;
var allTestFiles = [];

Object.keys(window.__karma__.files).forEach(function(file) {
    if (TEST_REGEXP.test(file)) {
        var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
        allTestFiles.push(normalizedTestModule);
    }
});

var cdn = 'http://localhost:55635/modules/';

require.config({
    baseUrl: '/base',

    initialRequire: [],

    globals: {
        'es6-promise': '3.2.1',
    },

    paths: {
        'lodash': cdn + 'lodash/2.4.1/lodash'
    },

    shim: {},

    deps: allTestFiles,

    callback: window.__karma__.start
});

我的karma.conf.js:

var path = require('path');

module.exports = function(config) {
    'use strict';
    var cdn = 'http://localhost:55635/modules/';
    var basePath = path.dirname(__filename);

    config.set({
        basePath: '',
        frameworks: [
            'requirejs',
            'jasmine'
        ],
        files: [
            {
                pattern: cdn + 'my-lib/version/my-lib.js',
                included: false
            }, 
            {
                pattern: 'test-transpiled/**',
                included: false
            },
            /**
             * This is needed because PhantomJS does not support window.Promise.
             */
            'node_modules/babel-polyfill/dist/polyfill.js',
            'dist/artifacts/my-lib.js',
            'test/unit/test-main.js',
        ],
        proxies: {
            '/cdn/': cdn
        },
        exclude: [],
        preprocessors: {},
        reporters: ['dots'],
        colors: true,
        autoWatch: false,
        singleRun: true,
        browsers: ['PhantomJS'],
    });
};

【问题讨论】:

    标签: javascript angularjs jasmine karma-runner


    【解决方案1】:

    您是否尝试过包括:

    'node_modules/angular/angular.js',
    'node_modules/angular*/*.js',
    

    在 karma.conf.js 的文件数组中?

    files: [
                {
                    pattern: cdn + 'my-lib/version/my-lib.js',
                    included: false
                }, 
                {
                    pattern: 'test-transpiled/**',
                    included: false
                },
                /**
                 * This is needed because PhantomJS does not support window.Promise.
                 */
                'node_modules/babel-polyfill/dist/polyfill.js',
                'dist/artifacts/my-lib.js',
                'test/unit/test-main.js',
    
                'node_modules/angular/angular.js',
                'node_modules/angular*/*.js',
            ],
    

    这就是我发现在 karma 配置中包含所有角度依赖项的方式。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2014-05-04
      • 1970-01-01
      • 2014-09-20
      • 2013-05-15
      • 1970-01-01
      • 2013-09-29
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多