2015-01-29 14 views
9

Ho il seguente: gulpfile.jsSpecifica dipendenze con gulp-gelsomino

var gulp = require('gulp'), 
    jasmine = require('gulp-jasmine'); 

gulp.task('default', function() { 
    return gulp 
     .src('spec/test.js') 
     .pipe(jasmine()); 
}); 

il codice all'interno spec/test.js utilizza una variabile globale angular, tuttavia, e genera un errore sul fatto che sia definito quando corro compito sorso default. Diciamo che angular è definito nel file spec/lib.js, nell'ambito globale.

Come è possibile comunicare a jasmine() quali dipendenze devono essere caricate prima nell'ambito globale prima di eseguire i valori describe() all'interno di test.js? In altre parole, come posso dire a jasmine() di caricare spec/lib.js prima di eseguire i test?

+1

Aggiungilo alla chiamata 'src', forse? 'gulp.src (['spec/lib.js', 'spec/test.js'])'. Puoi anche usare * wiredep * se stai usando le dipendenze di bower – Phil

+0

Sto combattendo la stessa battaglia, ho provato a passare un oggetto config alla funzione jasmine pipe come 'jasmine ({config: {files: [" foo.js " , "bar, js"]}}) 'ma non funziona ancora nel mio caso – VictorArcas

risposta

2

Hai provato KARMA?

KARMA LINK

Se si utilizza il karma, gelsomino, e gulp, che faccio, è possibile configurare il test come questo

In gulpfile.js

var gulp = require('gulp'); 
var karma = require('karma').server; // Note the server property here. 

gulp.task('karma', function (done) { 
    karma.start({ 
    configFile: __dirname + '/karma.conf.js' 
    }, done); 
}); 

È necessario importare ng-html2js per i modelli angolari

npm install ng-html2js 

In karma.config.js (È necessario creare)

module.exports = function(config) { 

    config.set({ 

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

    frameworks: ['jasmine'], 

    files: setFilesUpForTesting(), 

    // list of files to exclude during testing. CHANGE THIS TO YOURS!!! 
    exclude: [ 
    'public/js/boot.js', 
    'public/js/socket.io.js' 
    // 'yourRootPath/folder/stuff.js 
    ], 

    // NOTE THE NG-HTML2JS preprocessor you will need via NPM.  
    preprocessors: { 
    // CHANGE TO YOUR PATH for all HTML PARTIALS. 
    'public/directives/**/partials/*.html': 'ng-html2js', 
    'public/directives/**/*.js': ['coverage'] 
    }, 

    ngHtml2JsPreprocessor: { 
    stripPrefix:'public', 
    moduleName: 'templates' 
    }, 

    reporters: ['progress', 'coverage'], 

    coverageReporter: { 
    type : 'html', 
    dir : 'coverage/' 
    }, 

    port: 9876, 

    colors: true, 

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

// ------------------------------------------------------------------ > 
// Below code is for running either single or all test files. 
// To run a single test, just pass in the test file name like so... 

// If the test file name was rs-test-me-now_spec.js 
// To spool up Karma you would enter 

// gulp karma --single-test=rs-test-me-now <note the proceeding double hyphen> 
// To just run all tests 
// gulp karma 

// CHANGE THE FILE PATHS BELOW!!!!! 

var fixedPath = [ 
    'public/js/jquery.js', 
    'public/js/jquery-ui.min.js', 
    'public/js/foundation.min.js', 
    'public/js/angular.min.js', 
    'public/js/angular-mocks.js', 

]; 

var baseTestPath = 'public/test/'; // CHANGE THIS TO YOUR ROOT PATH!!! 

function setFilesUpForTesting(){ 
    fixedPath.push(testPath()); 
    return fixedPath; 
} 

function testPath(){ 
    return singleTestPath() || fullTestPath(); 
} 

function fullTestPath(){ 
    return 'public/test/**/*_spec.js'; // CHANGE THIS TO YOUR SPEC FOLDER!!! 
    // like rootFolder/somethinghere/spec/**/*_spec.js 
    // note the underscore that proceeds the spec.js. Get rid of if you file name does not have it. 
} 

function singleTestPath(){ 
    var passedArgument = process.argv.filter(function(item){ if(/--single-test=/.test(item)){return item; }}); 
    if(isEmpty(passedArgument)){ return false; } 
    return baseTestPath + '**/*' + fileName(passedArgument) + '_spec.js'; 
} 

function fileName(argument){ 
    if(!argument){ return; } 
    return argument[0].replace('--single-test=', ''); 
} 

function isEmpty(array){ 
    return array.length === 0; 
} 

------------------- END KARMA.CONFIG.js file ---------------------- 

ora per eseguire i test

// To run all tests 
gulp karma 

// To run one single file test 
gulp karma --single-test=rs-test-me-now 

NOTA: Se non riesci a farlo funzionare, fammelo sapere e lo faremo. Forse mi sono perso qualcosa. I miei file di test sono denominati something_test.js, non qualcosa_spec.js. Ho cambiato il codice per creare _spec.js. Inoltre, devi assicurarti di aver incluso tutti i file necessari per eseguire Angular. Ho fornito alcuni esempi nel mio karma.config.js. Corro anche Angular.

Il preprocessore (ng-html2js) compila i modelli angolari per le direttive. È necessario installarlo nel pacchetto package.json.

Problemi correlati