简体   繁体   English

如何在Karma中使用软件包别名? (例如:jquery的$)

[英]How do I use package aliases in Karma? (Example: $ for jquery)

My code depends on tons of other code, and is loaded last in the browser when running from the normal index.html-file. 我的代码取决于大量其他代码,并且是从常规index.html文件运行时最后加载到浏览器中的。 So of course when dependency 1 is jquery, and dependency 2 uses $.html(), and my code is loaded third, that works just fine in the browser. 因此,当然,当依赖项1是jquery,而依赖项2使用$ .html(),并且我的代码加载到第三位时,在浏览器中就可以正常工作。

But in Karma everything screeches to a halt since I'm loading 'jquery' from bower, not '$'. 但是在Karma中,由于我是从Bower中加载“ jquery”而不是“ $”,因此一切都停止了。

To be clear: it's not my code that's creating errors, it's the dependencies. 需要说明的是:不是我的代码在创建错误,而是依赖项。 I don't get to test my code since everything errors out before then. 我无法测试我的代码,因为在此之前所有错误都已解决。

So how do I get the tests to work? 那么如何使测试生效?

Note: I also run everything through webpack so I can use ES6-code, but webpack is loaded in Karma as well, so that should have no effect. 注意:我也通过webpack运行所有内容,因此我可以使用ES6代码,但是webpack也已加载到Karma中,因此应该无效。

Chrome 45.0.2454 (Mac OS X 10.11.0) ERROR
Uncaught TypeError: Cannot set property '$' of undefined
at /Users/tom/dev/orm/bower_components/jointjs/dist/joint.js:37

Webpack.conf.js: Webpack.conf.js:

var webpack = require('webpack');
module.exports = {
  devtool: 'source-map-loader',
  externals: [
    'jquery',
    'joint',
    'backbone',
    'loadash'
  ],
  // entry: './src/index.js',
  // output: {
  //   path: './public',
  //   filename: 'designer.js'
  // },
  plugins: [
    new webpack.ProvidePlugin({'$': 'jquery', 'jointjs': 'joint'})
  ],
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      }
    ]
  }
};

Karma.conf.js: Karma.conf.js:

// Karma configuration
// Generated on Thu Oct 08 2015 10:54:47 GMT+0200 (CEST)
var webconf = require('./webpack.config.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',
      'bower'
    ],


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

    bowerPackages: [
      'jquery',
      'jointjs',
      'backbone',
      'lodash'
    ],
    // list of files to exclude
    exclude: [],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      'test/*.js': [
        'webpack',
        'sourcemap'
      ],
      'src/**/*.js': [
        'webpack',
        'sourcemap'
      ]
    },
    webpack: webconf,


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


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


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

I think you're either looking for webpack externals 我认为您正在寻找Webpack外部组件

Webpack Externals Webpack外部

{
    externals: {
        // require("jquery") is external and available
        //  on the global var jQuery
        "jquery": "jQuery"
    }
}

...or, provide plugins. ...或者提供插件。

Provide Plugin vs. Externals 提供插件与外部

plugins: [
  new webpack.ProvidePlugin({
    "_": "underscore"
  }) 
]

Most likely provide plugin, because you want to provide that global variable to all the webpacked bundles. 最有可能提供插件,因为您想将该全局变量provide给所有webpacked捆绑包。

I have no idea what changed, but it now works fine. 我不知道发生了什么变化,但是现在工作正常。 I include here the final Karma config-file. 我在此处包括最终的Karma配置文件。 The webpack-file is identical to the one above. webpack文件与上面的文件相同。

Note that a couple of configuration changes are actually just normal configuration changes that I've changed since getting it to work. 请注意,一些配置更改实际上只是正常的配置更改,自生效以来,我已经对其进行了更改。

// Karma configuration
// Generated on Thu Oct 08 2015 10:54:47 GMT+0200 (CEST)
var webconf = require('./webpack.config.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: [
      'requirejs',
      'bower',
      'jasmine',
    ],
    bowerPackages: [
      'jquery',
      'lodash',
      'backbone',
      'jointjs'
    ],

    // list of files / patterns to load in the browser
    files: [
      'test-main.js',
      {
        pattern: 'test/*.js',
        included: false
      }
    ],
    // list of files to exclude
    exclude: [],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      'test/*.js': [
        'webpack',
        'sourcemap'
      ],
      'src/**/*.js': [
        'webpack',
        'sourcemap'
      ]
    },
    webpack: webconf,


    // 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: true,
    client: {
      captureConsole: false
    },
    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: [
      'PhantomJS',
      'Chrome'
    ],


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

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

相关问题 如何在Karma Runner中使用Handlebars.js? - How do I use Handlebars.js with Karma Runner? 我如何使用GruntFile.coffee和package.json来提取/ make / fetch / ?? Lungo.js示例文件? - How do I use GruntFile.coffee and package.json to extract/make/fetch/?? the Lungo.js example files? Jquery特别活动? 这些是什么? 我该如何使用它们? 这个例子是否适合使用Jquery特殊事件? - Jquery Special Events? What are they? How do I use them? Would this example be a good candidate for using Jquery Special Events? 如何使用isparta从karma中的代码覆盖中排除第三方导入(如jquery)? - How do I exclude third party imports like jquery from code coverage in karma using isparta? 如何在RequireJS中使用NodeJS包? - How do I use a NodeJS package with RequireJS? 如何使用 npm package? - How do I use an npm package? 如何让karma webdriver启动器使用我的selenium服务器/网格 - How do I get karma webdriver launcher to use my selenium server/grid 在此示例中,如何从jquery / javascript获取div childs? - how do i get div childs from jquery/javascript in this example? 如何修改此jQuery:提交选择器示例? - How do I modify this jQuery :submit selector example? 如何为此特定示例设置加载Jquery的超时? - How do I set a timeout in load Jquery for this particular example?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM