【问题标题】:Karma and RequireJS getting files outside base urlKarma 和 RequireJS 在基本 url 之外获取文件
【发布时间】:2015-04-23 12:59:38
【问题描述】:

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

/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:

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:

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 文件效果很好。当前的问题是我无法加载基本路径之外的任何资源。

因此,当我的测试尝试获取模板(位于 '../template/ 下)时,业力无法找到这些模板并失败。

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

有没有办法在基本路径之外加载资源?

【问题讨论】:

    标签: javascript requirejs karma-runner karma-jasmine


    【解决方案1】:

    你自己写的

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

    因此,这意味着如果您希望能够访问比这更深的文件,则需要使用以下命令移动所有 url:

     baseUrl: '../..',
    

    你也可以使用base/,它会被 karma 理解为你的 karmaConf.js 的 basePath

    为了更好地理解 karma.conf 和 test-main.js 的 basePath 是如何工作的,你可以看看这个:http://monicalent.com/blog/2015/02/11/karma-tests-angular-js-require-j/

    【讨论】:

    • 请多点赞!出于安全原因,这是正确的答案 - 不管你喜不喜欢:-(
    • 什么安全原因? @顶级大师
    • 防止访问非预期内容(只是一般的安全措施)。
    猜你喜欢
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 2014-04-09
    相关资源
    最近更新 更多