2016-11-28 96 views
0

我使用jasmine進行node.js單元測試,使用karma工具。錯誤nodejs單元測試使用茉莉,karma:require沒有定義

運行時出現錯誤:「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" 
    } 
+0

有一個在'budleDelay'一個類型。它可能應該是'bundleDelay'。 – str

+0

對不起,錯,但這不是原因。 我改正了它,仍然不能正常工作 – user7131571

+0

你使用茉莉節點嗎? https://github.com/mhevery/jasmine-node – YemSalat

回答

0

這個錯誤可能是由你的src/**/*.jssrc/server.js文件require電話引起的。

您在瀏覽器中加載這些文件:

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

沒有與broserify預處理:

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

謝謝。 這就是原因,我不知道我們不應該加載其他文件,因爲它會通過test/*。js加載(需要所有文件) – user7131571

+0

文件:[ 'test/*。js' ] , 這對我有用 – user7131571