【问题标题】:karma require js relative path not working业力需要js相对路径不起作用
【发布时间】:2015-07-24 06:03:28
【问题描述】:

我正在尝试为我的 javascript 项目创建测试环境,但由于There is no timestamp for 问题和 WARN [web-server]: 404: /base/app/templates/setting.html,我的模板位于与基本文件夹不同的文件夹中。这是我的目录结构

.
|--app
   |--js
      |--lib
      |--model
      |--view
      |--app.js
      |--main.js
      |--routes.js
   |--templates
      |--setting.html
      |--project.html
   |--index.html
|--test
   |--model
   |--view
|--karma.conf.js
|--test-main.js

这是我对 karma.conf.js 的配置

 // Karma configuration
 // Generated on Tue May 12 2015 20:44:23 GMT+0700 (WIB)

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: ['mocha', 'requirejs'],


// list of files / patterns to load in the browser
files: [
  'test-main.js',
  {pattern: 'app/js/lib/**/*.js', included: false},
  {pattern: 'app/js/service/*.js', included: false},
  {pattern: 'app/js/model/*.js', included: false},
  {pattern: 'app/js/test/view/settingView_spec.js', included: false},
  {pattern: 'app/js/view/*.js', included: false}
],


// list of files to exclude
exclude: [
  'app/js/app.js',
  'app/js/main.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_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],


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

还有我的test-main.js(目前我只尝试了一个测试文件)

require.config({
  // Karma serves files under /base, which is the basePath from your config file
  baseUrl: '/base/app/js',
  paths: {
    'jquery': 'lib/jquery/jquery',
    'underscore': 'lib/underscore/underscore',
    'backbone': 'lib/backbone/backbone',
    'text': 'lib/text/text',
    'templates': '../../templates'
  },

  // dynamically load all test files
  deps: ['test/view/settingView_spec'],
  // allTestFiles,

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

还有我的main.js 配置:

require.config({
  paths: {
    jquery: 'lib/jquery/jquery',
    backbone: 'lib/backbone/backbone',
    underscore: 'lib/underscore/underscore',
    text: 'lib/text/text',
    templates: '../../templates'
  }
});

正如您在上面的test-main.js 中看到的那样,我使用../../templates, but when I try to run the karma it says WARN [web-server]: 404: /base/app/templates/setting.html` 为模板创建了一个快捷方式

我该如何解决?

【问题讨论】:

    标签: javascript requirejs karma-runner karma-mocha karma-requirejs


    【解决方案1】:

    我已经想通了。不幸的是,我没有将模板中的所有 html 文件放入karma.conf.js。所以在其中,文件将是:

    // list of files / patterns to load in the browser
    files: [
     'test-main.js',
     {pattern: 'app/js/lib/**/*.js', included: false},
     {pattern: 'app/js/service/*.js', included: false},
     {pattern: 'app/js/model/*.js', included: false},
     {pattern: 'app/js/test/view/settingView_spec.js', included: false},
     {pattern: 'app/js/view/*.js', included: false},
     {pattern: 'app/templates/*.html', included: false}
    ],
    

    【讨论】:

      猜你喜欢
      • 2016-04-11
      • 2021-09-06
      • 1970-01-01
      • 2017-01-05
      • 2017-07-12
      • 2015-02-23
      • 2012-12-03
      • 2012-04-21
      • 1970-01-01
      相关资源
      最近更新 更多