Using AngularJS in Jasmine tests with Karma

I am using Jasmine with Karma to write a test for an Angular 1.5.7 application.

How should I refer to AngularJS from either the karma.conf.js file or the test-main.js file, to ensure it is available to my test?

I can currently run unit tests OK, but when a file referred to by the test refers to AngularJS (ie import angular from 'angular' ) AngularJS cannot be found, indicating I need additional configuration to get it to work.

I am transpiling to ES5 using babel, and imports are converted to RequireJS AMD.

The error is:

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

If I add AngularJS to the paths section of test-main.js then I get a different error:

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

If I remove the import angular from 'angular'; from the test target statement then I get the following error:

ReferenceError: Can't find variable: angular

My 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
});

My 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'],
    });
};

Have you tried to include:

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

in your files array in 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',
        ],

That was the way I found to include all angular dependencies in karma config.

Hope that helps!

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

上一篇: 在Node.js中将表情符号转换为PNG或JPG

下一篇: 在茉莉花测试中使用AngularJS与Karma