【问题标题】:Error in nodejs unit testing using jasmine, karma : require is not defined使用 jasmine 进行 nodejs 单元测试时出错,业力:未定义要求
【发布时间】:2017-04-12 04:33:43
【问题描述】:

我正在使用 jasmine 使用 karma 工具进行 node.js 单元测试。

运行时出现错误:“require is not defined”。

我尝试了很多方法,例如使用 karma-browserify 、 requirejs ,但都没有奏效。

我在link 中尝试了所有建议的解决方案,但没有一个对我有用。

这是我的 karma.config.js

// Karma configuration
// Generated on Mon Nov 28 2016 13:37:38 GMT+0530 (India Standard Time)

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',
        'browserify'
    ],


    // list of files / patterns to load in the browser
    files: [
      'src/**/*.js',
      'src/server.js',
      'test/*.js'
    ],
      browserify: {
          debug: true,
          transform: [ 'brfs' ]
      },
      watchify: {
          poll: true
      },


    // 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': [ 'browserify' ]
    },
      browserify: {
               bundleDelay: 2000,  // This may reduce instances of the karma-browserify "require is not defined" bug
              debug: true,
            },



    // 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: ['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
  })
};
package.json
"devDependencies": {
    "browserify": "^13.1.1",
    "grunt": "^0.4.5",
    "grunt-contrib-jshint": "^0.11.3",
    "grunt-jasmine-node": "^0.3.1",
    "grunt-jscs": "^2.5.0",
    "jasmine-core": "^2.2.0",
    "karma": "^1.3.0",
    "karma-browserify": "^5.1.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-jasmine": "^1.0.2",
    "load-grunt-tasks": "^3.4.0",
    "watchify": "^3.7.0"
  }

【问题讨论】:

  • budleDelay 中有一个类型。应该是bundleDelay
  • 对不起,错字,但这不是原因。我改正了,还是不行
  • 你在使用 jasmine-node 吗? github.com/mhevery/jasmine-node
  • 不,我已经用 package.json 更新了问题

标签: node.js unit-testing jasmine karma-runner


【解决方案1】:

此错误可能是由您的src/**/*.jssrc/server.js 文件中的require 调用引起的。

您在浏览器中加载这些文件:

   files: [
      'src/**/*.js',
      'src/server.js',
      'test/*.js'
   ],

不使用 broserify 进行预处理:

preprocessors: {
    'test/*.js': [ 'browserify' ]
},

【讨论】:

  • 谢谢。这就是原因,我不知道我们不应该加载其他文件,因为它将由 test/*.js 加载(所有文件都是必需的)
  • 文件:['test/*.js'],这对我有用
猜你喜欢
  • 2018-05-28
  • 1970-01-01
  • 2018-09-05
  • 1970-01-01
  • 2016-04-15
  • 2015-11-14
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多