在 WebStorm 中使用 Karma

Using Karma in WebStorm

这是新手问题,但我无法在 Whosebug 上找到答案。

我正在尝试在 Webstorm 中使用 Karma。在我的规范文件中,我试图要求 依赖文件,但 'require' 本身未定义!

在webstorm控制下Chrome是运行。这是我的 Karma.conf.js 文件和我的 Spec.js 文件。

基本问题是 'define'、'require' 和 'requirejs' 都是未定义的。那么我怎样才能包括其中的任何一个呢?!

karma.conf.js

// Karma configuration
// Generated on Tue Jul 19 2016 23:26:58 GMT-0700 (PDT)

requirejs = require('requirejs');
requirejs(["node-and-require"]);
requirejs("/ob/proj/uniformjs/main");



module.exports = function(config) {


  requirejs("/ob/proj/uniformjs/main");

  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: 'js',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
      'spec/**/*Spec.js'
    ],


    // 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: {
    },


    // spec 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,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
}

lexcore/SimpleUnitSpec.js

describe("A Unit", function () {
    var u;    // The unit being tested
    beforeEach(function(){
        var requirejs = require("requirejs");
        var SimpleUnit = requirejs("lexcore/SimpleUnit");
        u = SimpleUnit();
    });


    it("to be created and defined", function(){
        expect(u).toBeTruthy();
    });
});

生成此错误:

ReferenceError: require is not defined
    at Suite.<anonymous> (....

这是因为 browser/client-side JavaScript.

中不存在 require()

您有三个选择:

使用标签。 使用 CommonJS 实现。像 Node.js 这样的同步依赖 使用 AMD 实现。

npm install requirejs

此选项将安装最新版本。 下载 r.js

如果不想使用npm,可以直接获取r.js:

download page 下载 r.js 并将其放入您的项目中。 从 r.js repo 获取源代码并通过 "node dist.js" 生成 r.js,或者从 dist 目录获取快照。

用法

这些说明假定 'requirejs' 的 npm 安装。如果您直接使用 r.js 文件,请将 require('requirejs') 替换为 require('./path/to/r.js')。 基本用法是:require('requirejs') 将配置中主js文件的"require"函数传给requirejs.

示例:

var requirejs = require('requirejs');

requirejs.config({
//Pass the top-level main.js/index.js require
//function to requirejs so that node modules
//are loaded relative to the top-level JS file.
nodeRequire: require

});

requirejs(['foo', 'bar'],
function   (foo,   bar) {
//foo and bar are loaded according to requirejs
//config, but if not found, then node's require
//is used to load the module.
});

我认为 Thennarasan 的回答可能有用,所以我选择了它。

但我是这样解决自己的问题的:

(1) 在 karma init 的 运行 期间我指定使用 requirejs (2) 我重构了我的代码以使用 requirejs 格式

问题是 'require' 在浏览器中不存在, Karma 正在将您的节点代码注入浏览器。