2015-01-23 28 views
11

Herkesin, saatte değişmiş olan tek bir özelliği çalıştırmak için karma karma olup olmadığı merak ediyordum. Bu benim aşağıdaki yapılandırmam. Sorun şu ki, grunt.config satırı ('karma.unit.options.files', dosya yolu); tüm özellikler hala çalışır halde olduğu gibi hiçbir şey yapmıyor gibi görünmektedir, ancak foo karmadan önce çıktı alır: birim: çalışma kovulur.Bir birim testini çalıştırmak için karma karma alınıyor

grunt.initConfig({ 
    karma: { 
     unit: { 
      configFile: 'karma.conf.js', 
      background: true, 
      singleRun: false, 
      options: { 
       files: allFilesArray 
      } 
     } 
    }, 
    watch: { 
     options: { 
      spawn: false, 
      livereload: true 
     }, 
     karma: { 
      files: ['js/spec/**/*.spec.js', 'js/src/**/*.js'], 
      tasks: ['karma:unit:run'] 
     } 
    } 
}) 

grunt.event.on('watch', function (action, filepath){ 
    console.log('foo'); 
    grunt.config('karma.unit.options.files', filepath); 
}); 

Dosya değişikliğinden birinde terminalde bir özellik çalıştıran biri var mı? Binlerce testimiz var, bu yüzden yavaşlamaya başlıyor.

sayesinde Alex

+0

İlgili Karma sorunu: https://github.com/karma-runner/karma/issues/1507 # issuecomment-320383049 – Stefan

+0

Gulp için bir çözüm ile ilgili soru: https://stackoverflow.com/questions/26552729/karma-run-single-test/45350941#45350941 – Stefan

cevap

7

Bunu işe aldım. Temel olarak, bir dosya değiştiğinde karma yapılandırmayı dinamik olarak değiştirmek için bir olay işleyicisiyle saati kullanırsınız. İşte burada:

My Grunt yapılandırmasının iki karma görevi vardır: "all" ve "one". "all" hepsi çalışır ve "bir", önceden bilmediği tek bir dosyayı çalıştırır.

grunt.initConfig({ 
    // ... 
    karma: { 
    all: { 
     configFile: 'karma.conf.js', 
     browsers: ['PhantomJS'], 
     singleRun: true, 
     options: { 
     files: [ 
      'bower_components/jquery/dist/jquery.js', // dependencies 
      'src/js/**/*.js', // js source files 
      'src/js/**/*.spec.js' // unit test files 
     ] 
     } 
    }, 
    one: { 
     configFile: 'karma.conf.js', 
     browsers: ['PhantomJS'], 
     singleRun: true, 
     files: [ 
     {src: 'bower_components/jquery/dist/jquery.js'}, // dependencies 
     {src: ['src/js/**/*.js','!src/js/**/*.spec.js']} // source files 
     // (exclude the unit test files) 
     // watchEventListener will add the unit test file to run 
     ] 
    } 
    }, 
    // ... 
}); 

Ve daha sonra grunt dosyamda, olayları izlemek için bir dinleyici ekliyorum. Bu dinleyici karmayı günceller: bir görev ve birim test dosyasını ekler. Orijinal dosya dizisinin bir kopyasını saklıyoruz, aksi takdirde eklemelerimiz görev saatinin ömrü boyunca devam edip biriktiriyor. Burada

// when a unit test changes, execute only it 
var original_karmaOne_files = grunt.config.get('karma.one.files'); // keep the original files array 
grunt.event.on('watch', function watchEventListener(action, filepath, target){ 

    // this handler handles ALL watch changes. Try to filter out ones from other watch tasks 
    if (target == 'js_spec') handleJSHintSpec(); 

    // --------------------- 
    function handleJSHintSpec() { 
    if (action == 'deleted') return; // we don't need to run any tests when a file is deleted 
    // this will probably fail if a watch task is triggered with multiple files at once 
    // dynamically change the config 
    grunt.config.set('karma.one.files', [].concat(original_karmaOne_files, [{src: filepath}])); 
    } 
}); 

Ve

benim gruntfile izlenme iştir:

watch: { 
    // ... 
    // when js spec files change, 
    // lint them 
    // run unit tests 
    js_spec: { 
    options: { 
     interrupt: true 
    }, 
    files: 'src/js/**/*.spec.js', 
    tasks: ['jshint:js_spec', 'karma:one'] 
    }, 
    // ... 
} 

Benim karma.conf.js dosyası oldukça varsayılan, ama onun dosyalar dizi boştur. Aslında, bunu açıkladım, bu yüzden özellik tanımsız.

// list of files/patterns to load in the browser 
//files: [], // specified in the gruntfile 
+2

Ayrıca, dosya dizisinin de 'src' özelliği olan nesneler içermez - hayır t basit dosya yolu dizeleri, ya da başarısız oldu. –

+0

Ayrıca, her saat hedefi, "spawn" seçeneğinin false değerine ayarlanmış olması gerekir, aksi takdirde "grunt.config.set()" nin hiçbir etkisi olmaz. –

+1

Bu harika başlangıç ​​noktası ve yorumlar için teşekkürler. Yine de çalışmam için biraz zamanımı aldı. Sonuçta elde ettiğim Gruntfile.js'yi aşağıdaki yanıt olarak ekledim. – Stefan

2

TL; DR: Karma kullanın: karma yerine her yerde ünite: birim: koşun ve grunt.event.on'u kullanın ('watch', function() {}); Karma yapılandırmayı sadece çalıştırmak istediğiniz test dosyalarını içerecek şekilde düzenlemek için.

Bu çalışmam var ama istediğin şey olmayabilir. Bir dosyayı kaydettiğimde sunucuyu yeniden başlatıyorum. Daha fazla açıklama aşağıdadır. İşte config bazı geçerli:

watch: { 
     tests: { 
      files: 'tests/**/*.js', 
      tasks: ['karma:unit'] 
     }, 
     tsChanged: { 
      files: config.tsFiles, 
      tasks: [ 
       'ts', 
       'karma:unit' 
      ] 
     } 
    } 

    grunt.event.on('watch', function(action, filepath){ 
     grunt.config(['karma', 'unit', 'files'], [{ 
      src: [ 
       path/to/your/source/files, 
       path/to/your/test/file, 
      ] 
     }]); 
    }); 

O sunucuyu başlatır zaman o karma uygulama dosya ve tarayıcıya testi tüm dosyaları yükler gibi geliyor bana. Sizin durumunuzda, komut satırına "grunt karma: unit: start watch" girdiğiniz zaman bu olurdu. Bu yüzden burada "homurdanma" kullandım ve sürece "karma: birim" ekledim. Sonra kaydetme olayını yakaladım ve sunucuya başlamadan önce karma yapılandırmasını güncelledim.

Bu yardımcı olur umarım.

1

yargs bir arada ve bazı çalışma zamanı-sihirli kullanarak, bunu yapmak: Zerrin ve yükler için argüman olarak bir test dosyası/yol desen alır

var argv = require('yargs') 
    .default('t', '*.js') 
    .alias('t', 'tests') 
    .describe('t', 'A file or file pattern of the test files to run, relative to the test/unit dir') 
    .help('?') 
    .alias('?', 'help') 
    .argv; 

var filesToLoad = ['src/**/*.js', 'test/unit/helpers/*.js']; 
filesToLoad.push(path.join('test/unit/**', argv.t)); 

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

tercihi tüm o dosyaları.

0

Matthias'ın yanıtına ve yorumlarıma göre Grundfile.js geçerli:

module.exports = function (grunt) { 

    grunt.initConfig({ 
     pkg: grunt.file.readJSON('package.json'), 
     karma: { 
      all: { 
       configFile: 'karma.conf.js',    
       background: true, 
       files: [ 
        { src: './Leen.Managementsystem/bower_components/jquery/dist/jquery.js' }, 
        { src: './Leen.Managementsystem/bower_components/globalize/lib/globalize.js' }, 
        { src: './Leen.Managementsystem/bower_components/**/*.js', included: false }, 
        { src: './Leen.Managementsystem.Tests/App/test/mockFactory.js', included: false }, 
        { src: './Leen.Managementsystem/App/**/*.js', included: false }, 
        { src: './Leen.Managementsystem.Tests/App/test/*.js', included: false }, 
        { src: './Leen.Managementsystem.Tests/App/**/*.spec.js', included: false }, 
        { src: './Leen.Managementsystem.Tests/App/test-main.js' } 
       ] 
      }, 
      one: { 
       configFile: 'karma.conf.js',    
       files: [ 
        { src: './Leen.Managementsystem/bower_components/jquery/dist/jquery.js' }, 
        { src: './Leen.Managementsystem/bower_components/globalize/lib/globalize.js' }, 
        { src: './Leen.Managementsystem/bower_components/**/*.js', included: false }, 
        { src: './Leen.Managementsystem.Tests/App/test/mockFactory.js', included: false }, 
        { src: './Leen.Managementsystem/App/**/*.js', included: false }, 
        { src: './Leen.Managementsystem.Tests/App/test/*.js', included: false },   
        // (do not inlcude the *.spec.js files here! The watch event listener will add the single spec file to run) 
        { src: './Leen.Managementsystem.Tests/App/test-main.js' } 
       ] 
      } 
     }, 
     watch: {   
      spec_js: { 
       options: { 
        interrupt: true, 
        spawn: false 
       }, 
       files: 'Leen.Managementsystem.Tests/App/**/*.spec.js', 
       tasks: ['karma:one:start']    
      } 
     } 
    }); 

    var originalKarmaOneFiles = grunt.config.get('karma.one.files'); // keep the original files array 

    grunt.event.on('watch', function watchEventListener(action, filepath, target) { 

     if (target === 'spec_js') { 
      handleChangedSpecFile(); 
     } 

     function handleChangedSpecFile() { 
      if (action === 'deleted') { 
       return; 
      }   

      var testFilePath = "./" + filepath.replace(/\\/g, "/"); 

      grunt.log.writeln(['Running single karma test for: ' + testFilePath]); 
      var updatedFiles = originalKarmaOneFiles.concat([{ src: testFilePath, included: false }]); 

      grunt.config.set('karma.one.files', updatedFiles); 
     } 
    }); 


    grunt.loadNpmTasks('grunt-karma'); 
    grunt.loadNpmTasks('grunt-contrib-watch'); 

    grunt.registerTask('default', ['karma:all','watch']); 

}; 

karma.conf.js:

module.exports = function(config) { 
    config.set({ 

     // base path, that will be used to resolve files and exclude 
     basePath: '', //the solution root path, e.g. D:\Energienetzwerke\trunk 


     // frameworks to use 
     frameworks: ['jasmine', 'requirejs'], 

     // list of files/patterns to load in the browser 
     files: [ 
     './Leen.Managementsystem/bower_components/jquery/dist/jquery.js', 
     './Leen.Managementsystem/bower_components/globalize/lib/globalize.js', 
     { pattern: './Leen.Managementsystem/bower_components/**/*.js', included: false }, 
     { pattern: './Leen.Managementsystem.Tests/App/test/mockFactory.js', included: false }, 
     { pattern: './Leen.Managementsystem/App/**/*.js', included: false }, 
     { pattern: './Leen.Managementsystem.Tests/App/test/*.js', included: false}, 
     { pattern: './Leen.Managementsystem.Tests/App/**/*.spec.js', included: false}, 
     './Leen.Managementsystem.Tests/App/test-main.js' 
     ], 


     // list of files to exclude 
     exclude: [ 
     './Leen.Managementsystem/App/main.js' 
     ], 


     // test results reporter to use 
     // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage' 
     reporters: ['progress', 'coverage', 'notify', 'htmlDetailed', 'xml'], 



     coverageReporter: { 
     dir: './Leen.Managementsystem.Tests/testCoverage', 
     reporters: [ 
      { type: 'html',subdir: 'html'}, 
      { type: 'cobertura',subdir: 'xml', file: 'coverage.xml' }, 
      { type: 'lcov', subdir: 'lcov' }, 
      { type: 'text-summary' } 
      ] 
     }, 


     notifyReporter: { 
     reportEachFailure: true, // Default: false, Will notify on every failed spec 
     reportSuccess: false // Default: true, Will notify when a suite was successful 
     }, 

     htmlDetailed: { 
     autoReload: true, 
     dir: './Leen.Managementsystem.Tests/testResults' 

     }, 
    preprocessors: { 
     // source files, that you wanna generate coverage for 
     // do not include tests or libraries 
     // (these files will be instrumented by Istanbul) 
     './Leen.Managementsystem/App/**/*.js': ['coverage'] 
    }, 


    // 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, //watching is done by Gruntfile.js to only execute changed tests 
    usePolling: true, 

    // Start these browsers, currently available: 
    // - Chrome 
    // - ChromeCanary 
    // - Firefox 
    // - Opera 
    // - Safari (only Mac) 
    // - PhantomJS 
    // - IE (only Windows) 
    browsers: ['Chrome_With_Saved_DevTools_Settings'], 

    customLaunchers: { 
     Chrome_With_Saved_DevTools_Settings: { 
      base: 'Chrome', 
      chromeDataDir: './.chrome'    
     } 
    }, 


    // If browser does not capture in given timeout [ms], kill it 
    captureTimeout: 60000, 


    // Continuous Integration mode 
    // if true, it capture browsers, run tests and exit 
    singleRun: true 
    }); 
}; 

package.json:

{ 
    "name": "solution", 
    "version": "1.0.0", 
    "description": "contains packages that are needed for running karma", 
    "main": "./Leen.Managementsystem.Tests/App/test-main.js", 
    "dependencies": { 
    "grunt": "1.0.1", 
    "grunt-cli": "1.2.0", 
    "grunt-contrib-watch": "1.0.0", 
    "grunt-karma": "2.0.0", 
    "jasmine-core": "2.6.4", 
    "karma": "1.7.0", 
    "karma-chrome-launcher": "2.2.0", 
    "karma-cli": "1.0.1", 
    "karma-coverage": "1.1.1", 
    "karma-firefox-launcher": "1.0.1", 
    "karma-html-detailed-reporter": "1.1.20", 
    "karma-ie-launcher": "1.0.0", 
    "karma-jasmine": "1.1.0", 
    "karma-notify-reporter": "1.0.1", 
    "karma-phantomjs-launcher": "1.0.4", 
    "karma-requirejs": "1.1.0", 
    "karma-xml-reporter": "0.1.4", 
    "requirejs": "2.3.4" 
    }, 
    "devDependencies": {}, 
    "scripts": { 
    "test": "karma run" 
    }, 
    "author": "LEEN", 
    "license": "private" 
}