【问题标题】:Failed to load resource: the server responded with a status of 404 (Not Found) requirejs+karma加载资源失败:服务器响应状态为 404 (Not Found) requirejs+karma
【发布时间】:2016-05-06 14:41:57
【问题描述】:

我在将测试文件从另一个目录加载到我的单元测试文件时遇到问题。我正在使用 requirejs 和 karma 另一个问题是在测试场景文件项目中加载 chai-http 模块的正确方法

|--service
     |--abcfile.js // i want to use method implemented in this file and
                      test it.  
 |---node_modules
   -- all node library like karma,chai module

  |--test-main
     |--test-main.js    
| karma.conf.js

Karma.conf.js

module.exports = function(config) {
  config.set({

    basePath: '',
    frameworks: ['mocha', 'requirejs'],
    files: [
      {pattern: 'node_modules/**/*.js', included: false},   
      { pattern: 'node_modules/**/*/*.js', included: false },
      'test/test-main/test-main.js',
      { pattern: 'test/test-main/*.js', included: false }

    ],
    exclude: [

    ],

    reporters: ['progress','html'],

    htmlReporter: {
      outputFile: 'test/report/units.html',
      pageTitle: 'Tests',
      subPageTitle: 'A sample project description'
    },  

    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    captureTimeout: 60000,
    singleRun: false
  });
};

测试主

(function() {

  var specFiles = null;
  var baseUrl = '';
  var requirejsCallback = null;
  if (typeof window != 'undefined' && window.__karma__ != undefined) {

    baseUrl = '/base';
    requirejsCallback = window.__karma__.start;
    specFiles = [];
    for (var file in window.__karma__.files) {
      if (window.__karma__.files.hasOwnProperty(file)) {
          if (/.*\/javascript\/*\/.+_Test\.js$/.test(file)) {
          specFiles.push(file);
        }
      }
    }
  }

  requirejs.config({
      baseUrl: baseUrl,

      paths: {

        'chai': './node_modules/chai/chai',
        'sinon': './node_modules/sinon/pkg/sinon',
        'chaiHttp': './node_modules/chai-http/dist/chai-http',
      },

      deps: specFiles,
      callback: requirejsCallback
  });
})();

用户测试场景文件

define(function (chai, sinon, chaiHttp) {

    var expect = chai.expect;
    var service= require('/service/abcfile.js');// this is file where i want one of the function to test
    chai.use(chaiHttp);

错误 加载资源失败:服务器响应状态为 404(未找到)

【问题讨论】:

  • @Louis 我已编辑。/service/abcfile.js 不在 node_module 下。它在根目录下。如果我放置模块,那么我必须包含在 test-main 文件中才能像路径一样添加该模块:{ 'service': '/service/abcfile', 'chai': './node_modules/chai/chai', 'sinon ': './node_modules/sinon/pkg/sinon', 'chaiHttp': './node_modules/chai-http/dist/chai-http', },

标签: requirejs mocha.js karma-runner chai


【解决方案1】:

如果你想使用 CommonJS 糖,你必须使用 define(function(require, exports, module) {... 定义你的模块,在我看来,当前失败的模块的路径应该是 service/abcfile'(不需要或不需要 .js 扩展,并且没有前导斜杠)。所以是这样的:

define(function(require, exports, module) {

    var chai = require("chai");
    var sinon = require("sinon");
    var chaiHttp = require("chaiHttp");

    var expect = chai.expect;
    var service= require('service/abcfile');// this is file where i want one of the function to test
    chai.use(chaiHttp);

如果您最终不使用 exportsmodule,则可以在传递给 define 的回调中省略它们。

【讨论】:

  • 我已编辑。/service/abcfile.js 不在 node_module 下。它在根目录下。如果我放置模块,那么我必须更改测试主文件以添加该模块
  • 天哪!由于编辑不小心,我在早期版本中出现了错误。我已编辑我的答案以纠正我的错误并考虑您的评论。
  • 我已编辑。/service/abcfile.js 不在 node_module 下。它在根目录下。如果我放置模块,那么我必须包含在 test-main 文件中才能像路径一样添加该模块:{ 'service': '/service/abcfile', 'chai': './node_modules/chai/chai', 'sinon ': './node_modules/sinon/pkg/sinon', 'chaiHttp': './node_modules/chai-http/dist/chai-http', },
  • 你没有告诉我任何我不知道的事情。在我的最后一次编辑中,requirejs 应该使用路径 /base/service/abcfile.js 向 karma 的服务器发出查询,注意那里没有 node_module
  • 更改 karma.js:87 后错误:尚未为上下文加载模块名称“/service/abcfile”:_。使用 require([]) requirejs.org/docs/errors.html#notloaded
猜你喜欢
  • 2018-06-08
  • 2021-07-22
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 2014-09-25
相关资源
最近更新 更多