Karma和RequireJS在基本URL之外获取文件 [英] Karma and RequireJS getting files outside base url

查看:139
本文介绍了Karma和RequireJS在基本URL之外获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用业力配置自动测试。
我的文件结构如下

I am trying to configure automatic testing using karma. My file structure is as follows

/assests
/css
/font
/img
/js
    /collection
    /lib
    /model
    /plugin
    /spec
    /view
    test-main.js
    main.js
/templates
index.html
karma.conf.js

karma.conf.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: [
  'js/test-main.js',
      {pattern: 'js/*.js', included: false},
      {pattern: 'js/collection/*.js', included: false},
      {pattern: 'js/lib/**/*.js', included: false},
      {pattern: 'js/lib/**/**/*.js', included: false},
      {pattern: 'js/lib/**/**/**/*.js', included: false},
      {pattern: 'js/model/*.js', included: false},
      {pattern: 'js/model/**/*.js', included: false},
      {pattern: 'js/plugin/*.js', included: false},
      {pattern: 'js/plugin/**/*.js', included: false},
      {pattern: 'js/spec/*.js', included: false},
      {pattern: 'js/spec/**/*.js', included: false},
      {pattern: 'js/view/**/*.js', included: false},
      {pattern: 'js/view/*.js', included: false}
    ],


    // list of files to exclude
    exclude: [
      'js/main.js',
      'js/initScript.js',
      'js/SpecRunner.js'
    ],
    etc. . . 
}

test-main.js:

test-main.js:

var allTestFiles = [];
var TEST_REGEXP = /spec/i;

var pathToModule = function(path) {
  return path.replace(/^\/base\//, '').replace(/\.js$/, '');
};

Object.keys(window.__karma__.files).forEach(function(file) {
  if (TEST_REGEXP.test(file)) {
    // Normalize paths to RequireJS module names.
    allTestFiles.push(pathToModule(file));
  }
});

require.config({
  // Karma serves files under /base, which is the basePath from your     config file
  baseUrl: '../',

  //  dynamically load all test files
  deps: allTestFiles,

  // we have to kickoff jasmine, as it is asynchronous
  callback: window.__karma__.start,

  paths: {
    'jquery' : 'lib/jqm/jquery-1.11.2.min',
    'underscore' : 'lib/underscore/underscore',
    'backbone' : 'lib/backbone/backbone',       
    'template' : '../template',
 },

  shim : {
        backbone : {
            deps : [ 'underscore', 'jquery' ],
            exports : 'Backbone'
        }
   }
});

加载所有js文件效果很好。目前的问题是我无法在基本路径之外加载任何资源。

Loading all the js files works nicely. The current problem is that I cannot load any resources outside the base path.

所以当我的测试尝试获取模板时(在'../template/下) karma无法找到那些模板并失败。

So when my tests attempt to fetch a template (which is under '../template/) karma cannot find those templates and fails.

我无法更改我的基本路径,因为所有JS模块都在'js /'下。

I cannot change my base path because all the JS modules are under 'js/'.

无论如何都要在基本路径之外加载资源吗?

Is there anyway to load resources outside the base path ?

推荐答案

正如你自己写的那样

 // Karma serves files under /base, which is the basePath from your     config file
 baseUrl: '../',

因此,这意味着如果您希望能够访问比您需要更深的文件网址为:

So it means if you want to be able to access files deeper than that you need to shift all your url with a:

 baseUrl: '../..',

您还可以使用 base / ,这将被karma理解为您的karmaConf.js的basePath

You could also use base/ which will be understood by karma as the basePath of your karmaConf.js

为了更好地理解baseP karma.conf和test-main.js的工作原理你可以看一下: http://monicalent.com/blog/2015/02/11/karma-tests-angular-js-require-j/

For a better understanding on how the basePath of karma.conf and test-main.js works you can have a look at this: http://monicalent.com/blog/2015/02/11/karma-tests-angular-js-require-j/

这篇关于Karma和RequireJS在基本URL之外获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆