2016-11-08 3 views
1

Dies ist mein erstes Mal, um Unit-Test einig Code versucht, und ich bin immer diese Fehlermeldung, wenn Karma und Jasmin mit:Karma und Jasmin: nicht variabel gefunden: require

PhantomJS 2.1.1 (Windows 8 0.0.0) ERROR 
ReferenceError: Can't find variable: require 
at unit/tests.js:1 

versuchte ich npm install karma-browserify --save-dev aber die hat das Problem nicht gelöst.

Irgendeine Idee, wie man das sortiert?

Mein Karma conf-Datei:

// Karma configuration 
// Generated on Tue Nov 08 2016 03:14:50 GMT+0000 (GMT 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'], 


// list of files/patterns to load in the browser 
files: [ 
    // '../shopping-basket.js', 
    'unit/*.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: { 
    //'test/**/*.js': ['browserify'] 
}, 


// 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'], 
//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 
}) 
} 

tests.js

var myCode = require('./shopping-basket-functions'); 

describe('tests', function(){ 
    describe('testFunction', function(){ 
     it('should return 1', function(){ 
      // Call the exported function from the module 
      myCode.testFunction().should.equal(1); 
     }) 
    }) 
}) 

Korb-functions.js

function testFunction() { 
    return 1; 
} 

// If we're running under Node, 
if(typeof exports !== 'undefined') { 
    exports.testFunction = testFunction; 
} 

Antwort

0

Haben Sie die Vorlage versucht, mit hier: https://www.npmjs.com/package/grunt-template-jasmine-nml als spezifiziert in dieser answer?

Kurz gesagt, erlaubt es Ihnen, die CommonJS-Syntax zu verwenden, die NodeJS verwendet, um andere Module zu referenzieren und einzubinden. Sehen Sie ein Beispiel unten, wie Sie es in Ihrem Jasmin Config-Datei verweisen würden (auch hier je die Antwort des anderen Benutzers):

jasmine: { 
    options: { 
     template: require('grunt-template-jasmine-nml'), 
     helpers: 'spec/helpers/**/*.js', 
     specs: 'spec/**/*.spec.js' 
    } 
} 
0

gleiches Problem hatte versucht, eine App zu entwickeln, phanotmjs auf kriechend die undefinierte Variable gehalten erfordern, löste es durch: npm installieren grunt-template-jasmine-nml --save-dev

dies wird die richtige Vorlage aktualisieren und es funktioniert nach. versuchen: grunt test -dd fertig ohne fehler

Verwandte Themen