我如何在 Karma 中使用包别名? (例如:$ 表示 jquery)

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

我的代码依赖于大量其他代码,并且在从正常 index.html 文件 运行ning 时最后在浏览器中加载。所以当然,当依赖项 1 是 jquery,而依赖项 2 使用 $.html(),并且我的代码第三次加载时,这在浏览器中工作得很好。

但在 Karma 中,一切都会戛然而止,因为我正在从凉亭加载 'jquery',而不是“$”。

明确一点:不是我的代码造成了错误,而是依赖项。我没有去测试我的代码,因为在那之前一切都出错了。

那么如何让测试工作?

注意:我还 运行 通过 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:

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 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
  });
};

我想你要么在寻找 webpack 外部组件

Webpack Externals

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

...或者,提供插件。

Provide Plugin vs. Externals

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

很可能提供插件,因为你想provide那个全局变量到所有的 webpacked 包。

我不知道发生了什么变化,但现在可以正常工作了。我在这里包含了最终的 Karma 配置文件。 webpack-file 与上面的相同。

请注意,一些配置更改实际上只是正常的配置更改,自从它开始工作以来我已经更改了这些更改。

// 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
  });
};