2016-08-05 27 views
0

das ist mein karma.conf.jsEckige Unit-Tests versagt, nicht in der Lage servies zu injizieren

`use strict`; 

var path = require('path'); 
var conf = require('./gulp/conf'); 

var _ = require('lodash'); 
var wiredep = require('wiredep'); 

var pathSrcHtml = [ 
    path.join(conf.paths.src, '/**/*.html') 
]; 

function listFiles() { 
    var wiredepOptions = wiredep({ 
     directory: 'src/bower_components', 
     exclude: [], 
     dependencies: true, 
     devDependencies: true 
    }); 

    return wiredep(wiredepOptions).js 
    .concat([ 
     path.join(conf.paths.src, '/app/**/*.module.js'), 
     path.join(conf.paths.src, '/app/**/*.js'), 
     path.join(conf.paths.src, '/app/*.spec.js') 
    ]) 
    .concat(pathSrcHtml); 
} 

module.exports = function(config) { 
    var files=['https://maps.googleapis.com/maps/api/js?sensor=false'].concat(listFiles()); 
    var configuration = { 
    files: files, 

    singleRun: true, 

    autoWatch: false, 

    ngHtml2JsPreprocessor: { 
     stripPrefix: conf.paths.src , 
     moduleName: 'truelocal' 
    }, 

    logLevel: 'WARN', 

    frameworks: ['jasmine', 'angular-filesort'], 

    angularFilesort: { 
     whitelist: [path.join(conf.paths.src, '/**/!(*.html|*.spec|*.mock).js')] 
    }, 

    browsers : ['PhantomJS'], 

    plugins : [ 
     'karma-phantomjs-launcher', 
     'karma-angular-filesort', 
     'karma-coverage', 
     'karma-jasmine', 
     'karma-ng-html2js-preprocessor' 
    ], 

    coverageReporter: { 
     type : 'html', 
     dir : 'coverage/' 
    }, 
    // level of logging 
     // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
     logLevel: config.LOG_DEBUG, 

     captureConsole: true, 
    reporters: ['progress'] 
    }; 

    // This is the default preprocessors configuration for a usage with Karma cli 
    // The coverage preprocessor in added in gulp/unit-test.js only for single tests 
    // It was not possible to do it there because karma doesn't let us now if we are 
    // running a single test or not 
    configuration.preprocessors = {}; 
    pathSrcHtml.forEach(function(path) { 
    configuration.preprocessors[path] = ['ng-html2js']; 
    }); 

    // This block is needed to execute Chrome on Travis 
    // If you ever plan to use Chrome and Travis, you can keep it 
    // If not, you can safely remove it 
    // https://github.com/karma-runner/karma/issues/1144#issuecomment-53633076 
    if(configuration.browsers[0] === 'Chrome' && process.env.TRAVIS) { 
    configuration.customLaunchers = { 
     'chrome-travis-ci': { 
     base: 'Chrome', 
     flags: ['--no-sandbox'] 
     } 
    }; 
    configuration.browsers = ['chrome-travis-ci']; 
    } 

    config.set(configuration); 
}; 

//Bower.json 
{ 
    "name": "truelocal", 
    "version": "0.0.0", 
    "dependencies": { 
    "angular": "~1.4.10", 
    "angular-animate": "~1.4.10", 
    "angular-aria": "~1.4.10", 
    "angular-bootstrap": "~0.13.4", 
    "angular-cookies": "~1.4.10", 
    "angular-local-storage": "~0.2.3", 
    "angular-messages": "~1.4.10", 
    "angular-resource": "~1.4.10", 
    "angular-sanitize": "~1.4.10", 
    "angular-toastr": "~1.5.0", 
    "angular-touch": "~1.4.10", 
    "animate.css": "~3.4.0", 
    "bootstrap": "~3.3.5", 
    "jquery": "~1.11.3", 
    "malarkey": "yuanqing/malarkey#~1.3.1", 
    "moment": "~2.10.6", 
    "moment-parseformat": "~1.1.3", 
    "ng-file-upload-shim": "~9.1.2", 
    "jQuery.dotdotdot": "~1.7.4", 
    "lodash": "3.10.1", 
    "angular-ui-router": "~0.2.18", 
    "markerclustererplus": "~2.1.4", 
    "ng-fastclick": "~1.0.2", 
    "angular-swipe": "~0.1.0", 
    "angular-bindonce": "~0.3.3", 
    "angular-loading-bar": "~0.9.0", 
    "jquery-placeholder": "~2.3.1", 
    "angular-shims-placeholder": "^0.4.7" 
    }, 
    "devDependencies": { 
    "angular-mocks": "~1.4.10" 
    }, 
    "overrides": { 
    "bootstrap": { 
     "main": [ 
     "dist/css/bootstrap.css", 
     "dist/fonts/glyphicons-halflings-regular.eot", 
     "dist/fonts/glyphicons-halflings-regular.svg", 
     "dist/fonts/glyphicons-halflings-regular.ttf", 
     "dist/fonts/glyphicons-halflings-regular.woff", 
     "dist/fonts/glyphicons-halflings-regular.woff2" 
     ] 
    } 
    }, 
    "resolutions": { 
    "jquery": "~1.11.3", 
    "angular": "^1.5.7" 
    } 
} 

Dies ist ein bestehendes Projekt mit mehr als 200 Unit-Tests geschrieben, der Code könnte ein bisschen Update benötigt aber Ich bin nicht in der Lage, einen Komponententest mit abhängigen Diensten auszuführen. Ich vermute, die angular-Mocks verursacht ein Problem, wenn ich es in den Abhängigkeiten nach oben verschieben, bekomme ich nicht das Modul fehlende Fehler. aber ich bekomme immer noch nicht die anderen Abhängigkeiten in den Komponententests.

+0

Ich bin nicht in der Lage, alle Tests auf einmal auszuführen, 'TypeError: undefined ist kein Objekt (evaluiert 'errorService.service') in src/app/services/error.service.spec.js' keep on diesen Fehler erhalten, damit alles in die Komponententests injiziert wird – user6680441

Antwort

0

Der Karma.conf.js fehlte eine Datei aus dem Projekt. sobald alle Dateien dem Testconf zugeführt sind. Tests funktionierten wie ein Charme!