在茉莉花测试中使用AngularJS与Karma

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

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

我当前可以运行单元测试OK,但是当测试引用的文件引用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';删除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'],
    });
};

你有没有试图包括:

'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',
        ],

这是我发现在业务配置中包含所有角度依赖关系的方式。

希望有所帮助!

链接地址: http://www.djcxy.com/p/39125.html

上一篇: Using AngularJS in Jasmine tests with Karma

下一篇: Error in testing function call with Karma in AngularJs