首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角与ES6:用SystemJS进行的Karma检验

角与ES6:用SystemJS进行的Karma检验
EN

Stack Overflow用户
提问于 2015-07-08 22:17:12
回答 1查看 2.1K关注 0票数 0

下午所有,我有一个平均堆栈应用程序,我正在开发测试。角度代码是使用ES6编写的,因此我一直在尝试配置Karma和SystemJS,并使用Babel作为转换程序来运行我的测试。当前,当我karma start karma.conf.js浏览器启动时,挂起--就像我不能单击调试或其他任何东西一样--然后浏览器关闭时会出现控制台错误:

代码语言:javascript
复制
Uncaught TypeError: Cannot set property 'mock' of undefined.

前面的最后一行是DEBUG [web-server]: serving (cached): ( ... )

我当前的应用程序结构如下所示:

我将所有模块导入到一个文件app.js中,并将它们注入到我的应用程序模块中:

代码语言:javascript
复制
import HomeController from './components/home/home.js';
import HomeService from './services/homeservice.js';
import HomeDirective from './directives/homedirective.js';
import DifferentController from './components/different/different.js';

// ### Filters
import slugifyFilter from './filters/slugify.js';

var moduleName = 'app';

angular.module(moduleName, ['ngNewRouter', 'ngMock', 'ngAnimate', 'ui.bootstrap', 'slugifyFilter'])

  .config(['$componentLoaderProvider', SetTemplatesPath])
  .controller('AppController', ['$router', AppController]);



function SetTemplatesPath ($componentLoaderProvider){

  $componentLoaderProvider.setTemplateMapping(name => `components/${name}/${name}.html`);
}

function AppController ($router) {

  $router.config([

    // Component is just a template + controller
    // in 'components' folder
    { path: '/', redirectTo: '/home' },
    { path: '/home', component: 'home' },
    { path: '/different/:id', component: 'different' }
  ]);
}

export default moduleName;

我在index.html文件中使用手动角度引导,如下所示:

代码语言:javascript
复制
import { default as AppModule } from './app.js';

angular.bootstrap(document, [ AppModule ]);

try {

   $(document.body).attr("ng-app", "app");

} catch(e){};

我将Karma和SystemJS配置为:

代码语言:javascript
复制
// Karma configuration
// Generated on Tue Jul 07 2015 19:56:15 GMT-0400 (Eastern Daylight Time)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: './',


    files : [],


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['systemjs', 'jasmine'],


    plugins : ['karma-systemjs', 'karma-jasmine', 'karma-chrome-launcher', 
                'karma-firefox-launcher', 'karma-ie-launcher' ],

    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: { "**/*.html": ['ngbootstrapfix'] },

    systemjs : {

        // Path to SystemJS config file
        //configFile : 'public/system.conf.js',

        // File patterns for application code, dependencies, and test suites
        files : [

            'public/bower_components/jquery/dist/jquery.js',
            'public/bower_components/angular/angular.js',
            'public/bower_components/angular-mocks/angular-mocks.js',
            'public/bower_components/angular-animate/angular-animate.js',
            'public/bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
            'public/bower_components/angular-new-router/dist/router.es5.js',
            'public/bower_components/angular-messages/angular-messages.js',
            'public/**/*.js'
        ],

        // SystemJS configuration specifically for tests, added after your config file. 
        // Good for adding test libraries and mock modules 
        config: {

            baseURL : '/',

            // Set path for third-party libraries as modules
            paths : {

                'jquery': 'public/bower_components/jquery/dist/jquery.js',
                'angular-mocks': 'public/bower_components/angular-mocks/angular-mocks.js',
                'angular' : 'public/bower_components/angular/angular.js',
                'angular-animate' : 'public/bower_components/angular-animate/angular-animate.js',
                'ui-bootstrap' : 'public/bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
                'router' : 'public/bower_components/angular-new-router/dist/router.es5.js',
                'angular-messages' : 'public/bower_components/angular-messages/angular-messages.js',
                'babel': 'node_modules/babel-core/browser.js',
                'es6-module-loader': 'node_modules/es6-module-loader/dist/es6-module-loader.js',
                'systemjs': 'node_modules/systemjs/dist/system.js',
                'system-polyfills': 'node_modules/systemjs/dist/system-polyfills.js'
            },

            transpiler: 'babel'
        },

        // Specify the suffix used for test suite file names.  Defaults to .test.js, .spec.js, _test.js, and _spec.js 
        testFileSuffix: '-spec.js'
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_DEBUG,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: true
  });
};

我这里有一个过滤器,我正在试着测试:

代码语言:javascript
复制
let moduleName = 'slugifyFilter';

angular.module(moduleName, [])
    .filter('slugify', () => {

    return (input) => {

        input = input || '';

        return input.replace(/ /g, '-').toLowerCase();
    };
});

export default moduleName;

我的测试文件:

代码语言:javascript
复制
import 'angular-mocks';
import '../bootstrap.js';

describe('slugify filter', function() {

    beforeEach(function() {

        angular.mock.module('app');
    });

    beforeEach(angular.mock.inject(function(_$filter_) {

        var $filter = _$filter_;
    }));

    it('returns a slug when given a string', function() {

        var slugify = $filter('slugify');

        expect(slugify('Home Component 3')).toContain('home-component-3');
    });
});

然而,每当我尝试运行测试时,我都会得到上面描述的错误。真正困扰我的是,在窗口写着“浏览器执行”之前,浏览器就会冻结。任何帮助都是非常感谢的,我真的很想为我的代码编写一些单元测试!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-09 13:52:02

将这些文件添加到karma.files数组中:

代码语言:javascript
复制
'public/bower_components/jquery/dist/jquery.js',
'public/bower_components/angular-mocks/angular-mocks.js',
'public/bower_components/angular/angular.js'
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31304800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档