【问题标题】:AngularJS, RequireJS, Karma - Test File not loadingAngularJS、RequireJS、Karma - 未加载测试文件
【发布时间】:2014-10-22 03:35:54
【问题描述】:

我目前正在尝试使用 Karma 运行测试,但是我似乎无法加载测试文件...

这是我的文件结构:

app
    controllers
        ..
    directives
    ..
..
test
    customersControllerTest.js
karma.conf.js
test-main.js
..

这是我当前的 karma.conf.js 文件:

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

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

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

    // list of files / patterns to load in the browser
    files: [
        {pattern: './scripts/jquery.js', included: true},
        {pattern: './scripts/angular.js', included: true},
        {pattern: './scripts/angular-mocks.js', included: true},
        'test-main.js',
        {pattern: './test/*.js', included: false},
        {pattern: './app/**/*.js', included: false},
    ],

    // list of files to exclude
    exclude: [
      '**/*.css',
      '**/bootstrap.js',
      '**/routes.js',
      '**/conf.js',
      '**/app.js'
    ],

    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },

    // 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: ['PhantomJS', 'Chrome', 'Firefox'],

    plugins: [
        'karma-*',
    ],

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

还有我的 test-main.js 文件:

var testPaths = {
    'customersControllersTest': '../test/customersControllersTest'
},  tests = Object.keys(window.__karma__.files).filter(function(file) {
    return (/(spec|Test)\.js$/i.test(file));
}).map(function(test) {
    var returnVal = false;
    Object.keys(testPaths).map(function(path){
        if(testPaths[path] === test.replace('/base', '..').replace('.js', '')) {
            returnVal = path;
        }
    });
    return returnVal;
});

require.config({
    // Karma serves files under /base, which is the basePath from your config file
    baseUrl: '/base/app',
    paths: {
        'jquery': '../scripts/jquery',
        'angular': '../scripts/angular',
        'angularMocks': '../scripts/angular-mocks',

        // Modules
        'servicesModule': './services/module',
        'directivesModule': './directives/module',
        'controllersModule': './controllers/module',

        // Controllers
        'testController': './controllers/testController',
        'ordersController': './controllers/ordersController',
        'allordersController': './controllers/allordersController',
        'customersController': './controllers/customersController',

        // Directives
        'barChart': './directives/barsChartDirective',
        'blueBarChart': './directives/blueBarsChartDirective',

        // Services
        'testService': './services/testService',
        'routeResolver': './services/routeResolver',
        'customersFactory': './services/customersFactory',

        // Tests
        'customersControllersTest': '../test/customersControllersTest'
    },
    // angular does not support AMD out of the box, put it in a shim
    shim: {
        'angular': {
            deps: ['jquery'],
            exports: 'angular'
        },
        'angularRoute': ['angular'],
        'angularAnimate': ['angular'],
        'angularMocks': ['angular']
    },
    // dynamically load all test files
    deps: tests,
    // we have to kickoff jasmine, as it is asynchronous
    callback: window.__karma__.start()
});

和我的客户ControllersTest.js:

define('customersControllersTest', [], function() {
    describe('test', function() {
        console.log(this);
        it('test1', function() {
            expect('this').toEqual('this');
        });
        console.log(this);
    });
});

通过检查 Chrome 的开发工具,它给出了一个错误:

但是当我打开它时:

但这可能完全不相关,因为我不断收到以下信息:

除了说它没有找到测试之外,它正在大喊错误......

有什么想法可以解决这个问题吗?

【问题讨论】:

  • test-main.js 构建tests 列表时filter() 之后的map() 的目的是什么?
  • 最里面的映射可能是一个forEach,因为我只是将值分配给一个变量,以便我调用requireJS模块而不是文件本身(所以require它很神奇)

标签: angularjs requirejs jasmine karma-runner karma-jasmine


【解决方案1】:

一个问题是行:

callback: window.__karma__.start()

这会立即调用start,而不是将其作为callback 传递。这行应该是

callback: window.__karma__.start

karma-requirejs README

【讨论】:

  • 这实际上修复了它...我也会告诉错误的原始来源(从网站复制粘贴结构)感谢它:)
猜你喜欢
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多