Karma和RequireJS在基本URL之外获取文件

Fil*_*ira 6 javascript requirejs karma-runner karma-jasmine

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

/assests
/css
/font
/img
/js
    /collection
    /lib
    /model
    /plugin
    /spec
    /view
    test-main.js
    main.js
/templates
index.html
karma.conf.js
Run Code Online (Sandbox Code Playgroud)

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. . . 
}
Run Code Online (Sandbox Code Playgroud)

测试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'
        }
   }
});
Run Code Online (Sandbox Code Playgroud)

加载所有js文件很好地工作.目前的问题是我无法加载基本路径之外的任何资源.

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

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

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

deK*_*joo 1

正如你自己写的

 // Karma serves files under /base, which is the basePath from your     config file
 baseUrl: '../',
Run Code Online (Sandbox Code Playgroud)

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

 baseUrl: '../..',
Run Code Online (Sandbox Code Playgroud)

您还可以使用base/karma 可以理解的作为 karmaConf.js 的 basePath

为了更好地了解 karma.conf 和 test-main.js 的 basePath 如何工作,您可以查看以下内容:http ://monicalent.com/blog/2015/02/11/karma-tests-angular-js -需要-j/