简体   繁体   English

RequireJS无法找到与Karma的依赖关系

[英]RequireJS can not find dependencies with Karma

This is my directory hierarchy: 这是我的目录层次结构:

  • App 应用
    • backend/ 后端/
    • frontend/ 前端/
      • controllers/ 控制器/
      • ... ...
    • test/ 测试/
      • controllers/ 控制器/
      • ... ...
      • test-main-cfg.js 测试主cfg.js
    • vendor/ 供应商/
      • angular/angular.min.js 角/ angular.min.js
      • underscore/underscore.js 下划线/ underscore.js
      • ... ...
    • karma.conf.js karma.conf.js

And this is the content of karma.conf.js: 这是karma.conf.js的内容:

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', 'requirejs'],
    files: [
      {pattern: 'test/**/*.spec.js', included: false},
      {pattern: 'vendor/angular/angular.min.js', included: false},
      {pattern: 'vendor/angular/angular-route.min.js', included: false},
      {pattern: 'vendor/angular/angular-mocks.js', included: false},
      {pattern: 'backend/public/**/*.js', included: false},
      {pattern: 'test/main.spec.js', included: false},
      'test/test-main-cfg.js'
    ],
    exclude: [
      '**/*.swp'
    ],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome', 'Firefox', 'Safari'],
    captureTimeout: 60000,
    singleRun: false
  });
};

And the following is test/test-main-cfg.js,the config file of requires and start point of karma: 以下是test / test-main-cfg.js,需求的配置文件和业力的起点:

var tests = [],
    file;
for (file in window.__karma__.files) {
  if (window.__karma__.files.hasOwnProperty(file)) {
    if (/.spec\.js$/.test(file)) {
      tests.push(file);
    }
  }
}

require.config({
  baseUrl: '/base/',
  paths: {
    'jquery': "vendor/jquery/dist/jquery.min",
    'angular': 'vendor/angular/angular.min',
    'angularRoute': 'vendor/anguar-route.min',
    'angularMocks': 'vendor/angular/angular-mocks',
    'underscore': 'vendor/underscore/underscore'
  },
  shim: {
    'underscore': {
      exports: '_'
    },
    // added
    'jquery': {
      exports: 'jquery'
    },
    'angular': {
      deps: ['jquery'],
      exports: 'angular'
    },
    'angularRoute': {
      deps: ['angular'],
      exports: 'angularRoute'
    },
    'angularMocks': {
      deps:['angular'],
      exports: 'angularMocks'
    }
  },
  deps: tests,
  callback: window.__karma__.start
});

One of my spec file is: 我的一个spec文件是:

define(['underscore', 'jquery', 'angular', 'angularRoute', 'angularMocks'], function(_) {
  describe('UnitTest: ParamsSettings', function(_) {
    it('is defined', function() {
      expect(_.size([1,2,3])).toEqual(3);
    })
  })
});

I can not load any of the dependencies in the spec file. 我无法加载spec文件中的任何依赖项。 Great appreciation for your answers. 非常感谢您的回答。

Edit: 编辑:

The main.spec.js in karma.conf.js can be found using url http://localhost:9876/base/test/main.spec.js , but I have a file named another.spec.js , which is at the same directory of the former file, can not be found by url http://localhost:9876/base/test/another.spec.js . karma.conf.js中的main.spec.js可以使用url http://localhost:9876/base/test/main.spec.js ,但是我有一个名为another.spec.js的文件,它位于前者文件的同一目录中, 无法通过URL找到http://localhost:9876/base/test/another.spec.js

Added the content afforded by @glepretre 添加了@glepretre提供的内容

The question was solved. 问题解决了。 Actually the Karma server did not serve any of my lib files. 实际上,Karma服务器没有提供任何我的lib文件。 We should specify the location of the files that Karma server served by setting correct files option of karma.conf.js . 我们应该通过设置karma.conf.js的正确文件选项来指定Karma服务器所服务的文件位置

So I edited it as follows: 所以我编辑如下:

...,
files: [
  {pattern: 'vendor/angular/angular.min.js', included: false},
  {pattern: 'vendor/angular/angular-route.min.js', included: false},
  {pattern: 'vendor/angular/angular-mocks.js', included: false},
  {pattern: 'vendor/jquery/dist/jquery.min.js', included: false},
  {pattern: 'vendor/underscore/underscore.js', included: false},
  {pattern: 'backend/public/**/*.js', included: false},
  {pattern: 'test/*.spec.js', included: false},
  {pattern: 'test/**/*.spec.js', included: false},
  {pattern: 'test/main.spec.js', included: false},
  'test/test-main-cfg.js'
],
...

Practically it can more be simpler: 实际上它可以更简单:

...,
files: [
  {pattern: 'vendor/**/*.js', included: false},
  {pattern: 'backend/public/**/*.js', included: false},
  {pattern: 'test/*.spec.js', included: false},
  {pattern: 'test/**/*.spec.js', included: false},
  {pattern: 'test/main.spec.js', included: false},
  'test/test-main-cfg.js'
],
...

And this is the official document of files option: Here 这是文件选项的官方文档: 这里

Shouldn't you add some shims ? 你不应该添加一些垫片吗?

In your require config: 在您的require配置中:

require.config({
  baseUrl: '/base/',
  paths: {
    'jquery': "vendor/jquery/dist/jquery.min",
    'angular': 'vendor/angular/angular.min',
    'angularRoute': 'vendor/anguar-route.min',
    'angularMocks': 'vendor/angular/angular-mocks',
    'underscore': 'vendor/underscore/underscore'
  },
  shim: {
    'underscore': {
      exports: '_'
    },

    // added
    'jquery': {
      exports: 'jquery'
    },
    'angular': {
      deps: ['jquery'],
      exports: 'angular'
    },
    'angularRoute': {
      deps: ['angular'],
      exports: 'angularRoute'
    },
    'angularMocks': {
      deps:['angular'],
      exports: 'angularMocks'
    }
  },
  deps: tests,
  callback: window.__karma__.start
});

in your spec file: 在您的spec文件中:

// moved underscore first to get the module in the function args
define(['underscore', 'jquery', 'angular', 'angularRoute', 'angularMocks'], 
  function(_) {
    describe('UnitTest: ParamsSettings', function() {
      it('is defined', function() {
        expect(_.size([1,2,3])).toEqual(3);
      });
  });
});

EDIT: By the way, if you want to see the starter app structure for angular projects using requireJS we've developped with my team. 编辑:顺便说一句,如果你想看看使用requireJS的角项目的入门应用程序结构,我们已经与我的团队一起开发。 It's based on official Angular best practices for App structure and you can get it here: angular-requirejs-ready . 它基于App结构的官方Angular最佳实践 ,你可以在这里得到它: angular-requirejs-ready

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM