requirejs抛出404尝试从karma runner加载文件

时间:2014-07-08 12:48:07

标签: javascript requirejs karma-runner

我正在尝试使用requirejs加载一个测试帮助程序模块,但即使它已经在业力运行开始时作为依赖项加载它也会失败,我无法弄清楚它是什么问题,我总是得到

There is no timestamp for /base/spec/helpers/testHelpers!'

测试运行正常,在我们内部我需要应用程序的任何模块没有问题,只有当我需要spec spec文件夹中的特定内容时它才会失败。

我检查了所有业力相关的问题,但没有一个适用于这些案例。

我的karma.conf文件:

module.exports = function (config) {
  'use strict';

  config.set({
    basePath: '',
    frameworks: ['jasmine', 'requirejs'],
    files: [
      'bootstrapTests.js',
      {
          pattern: 'app/**/*.js',
          included: false
      },
      {
          pattern: 'entities/**/*.js',
          included: false
      },
      {
          pattern: 'config/**/*.js',
          included: false
      },
      {
          pattern: 'libs/**/*.js',
          included: false
      },
      {
          pattern: 'spec/**/*Spec.js',
          included: false
      },
      {
          pattern: 'spec/helpers/**/*.js',
          included: false
      }
    ],
    exclude: [
        'bootstrap.js',
        'bootstrap.built.js'
    ],
    preprocessors: {
        'app/**/*.js': ['coverage'],
        'entities/**/*.js': ['coverage']
    },
    coverageReporter: {
        reporters: [
            {
                type: 'text-summary'
            }
        ]
    },
    reporters: ['progress', 'coverage'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['PhantomJS'],
    singleRun: false
  });
};

我的bootstrapTests.js文件:

var tests = [];

for (var file in window.__karma__.files) {
    if (window.__karma__.files.hasOwnProperty(file)) {
        // Add all spec files and helpers
        if (/spec\/.+.js$/.test(file)) {
            tests.push(file);
        }
    }
}

require.config({
    baseUrl: '/base',
    paths: {
        'backbone': 'libs/backbone',
        'marionette': 'libs/backbone.marionette',
        'jquery': 'libs/jquery-2.1.1',
        'json2': 'libs/json2',
        'underscore': 'libs/underscore',
        'twig': 'libs/twig',

        'editor': 'app/editor',
        'widgets': 'app/widgets',
        'menu': 'app/menu',

        'helpers': 'spec/helpers'
    },
    map: {
        '*': {
            'jquery': 'config/jquery/jqueryPrivate'
        },
        'config/jquery/jqueryPrivate': { 'jquery': 'jquery' }
    },
    deps: tests,

    callback: window.__karma__.start
});

所以在我的测试中,

define([
    'app/app',
    'backbone',
    'editor/editorController',
    'editor/editorApp',
], function (
    myApp,
    Backbone,
    editorController
) {
    'use strict';

    describe('editorApp', function () {

    .....

就像一个魅力,但当我尝试

define([
    'app/common/routerUtilities',
    'backbone',
    'helpers/testHelpers'
], function (
    routerUtilities,
    Backbone,
    testHelpers
) {
    'use strict';

    describe('routerUtilities', function () {
    ...

尝试获取testHelper失败,即使模块已经加载也总是出错。除了缩短的路径之外,我尝试以所有可能的方式指定路径:spec / helpers / testHelpers,/ base / spec / helpers / testHelpers,/ spec / helpers / testHelpers,.. /。/ help / testHelpers等...

该文件作为依赖项(已检查)包含在tests数组中,因此在我的测试和其他测试设置模块中加载(使用console.log检查):

LOG: ['/base/spec/app/appSpec.js',
'/base/spec/app/common/routerUtilitiesSpec.js',
'/base/spec/app/editor/EditorLayoutViewSpec.js',
'/base/spec/app/editor/editorAppSpec.js',
'/base/spec/app/editor/editorControllerSpec.js',
'/base/spec/app/menu/menuControllerSpec.js',
'/base/spec/helpers/envSetup.js',
'/base/spec/helpers/testHelpers.js']

我已经检查过它包含在window._ karma _.files中(为简洁而编辑):

LOG: Object{
 /base/bootstrapTests.js: 'c389a1d36d1c48f2879d434b10fd5a25b6b07758', 
 /base/app/app.js: '7ad39554809146effd20dd1db908fc068f8119ba',
 /base/app/common/routerUtilities.js: '4da0e1d1794cccc727b544cacbc6d672c0d9965a',
 ...
/base/config/marionette/renderer.js: '34a5e729f6133aa13841c5e949307cd437ca331b', 
/base/config/marionette/templateCache.js: '9f1901d4c6f43f5f90dadacb1c4ac179efd14b15', 
 /base/spec/app/appSpec.js: 'e01f4fea3533e25cfcd4f7621e1c1c5559e0eed8', 
/base/spec/app/common/routerUtilitiesSpec.js: 'a06b9793a635633d053142dff24d1ba4427dd365',
/base/spec/app/editor/EditorLayoutViewSpec.js: 'a24c7e8742e38b86d626bd6f3e45baacfa6af5eb', 
/base/spec/app/editor/editorAppSpec.js: '800c0e8e82840ebd0d1af07c3ec4556a24ee0b04', 
/base/spec/app/editor/editorControllerSpec.js: 'd2e259a477da85b6a2f742f5c6c3a4a34b7e340b', 
/base/spec/helpers/envSetup.js: '297aa1e59477d967aa210bece1726e8a372cb630', 
/base/spec/helpers/testHelpers.js: '5266065d344ca69f8f5980db16bf6d131492ebd0'

}

spec / helpers / testHelpers.js的内容是:

define([
    'backbone'
], function (
    Backbone
) {
    'use strict';

    var testHelpers = {
        setupRoutingTest: function () {
            Backbone.history.start();
            Backbone.history.navigate('');
        },
        teardownRoutingTest: function () {
            Backbone.history.navigate('');
            Backbone.history.stop();
        }
    };
    return testHelpers;
});

我的文件夹结构是:

|-app

  |---common

  |---editor

  |---menu

  |---widgets

|-config

  |---jquery

  |---marionette

|-entities

|-features

|-gruntTasks

   |---lib

|-spec

   |---app

      |-----common

      |-----editor

      |-----menu

      |-----widgets

   |---entities

   |---helpers

 |-tmp

2 个答案:

答案 0 :(得分:4)

嗨,看起来karma-requirejs不喜欢加载到tests数组中的资源。

尝试修改代码以排除帮助程序文件,它可以帮助

var tests = [];

for (var file in window.__karma__.files) {
    if (window.__karma__.files.hasOwnProperty(file)) {
        // Add all spec files and helpers
        if (/spec\/.+.js$/.test(file)) {
            tests.push(file);
        }
    }
}

答案 1 :(得分:1)

这让我有点疯了一会儿。确保你使用的是karma-requirejs,而不仅仅是requirejs。

https://www.npmjs.org/package/karma-requirejs

在这方面,请不要加载requirejs的生产版本。