gulp和karma,文件karma.conf.js不存在

MrP*_*les 7 karma-runner gulp

我有一个基本的AngularJS应用程序,并希望让我的所有终端命令运行gulp任务,例如$ gulp dev开发服务器和$ gulp unitTest测试等.

我根据文档使用$ npm install --save-dev gulpgulpfile.js的项目文件的根目录安装了Gulp .我也为karma的安装和配置文件做了同样的事情.

现在值得说明的是,我希望所有标记的npm安装都--save可以轻松地在办公室和服务器周围移动项目.

当谈到将任务添加到Gulp时,我必须向我们提供一个相对(到karma模块)路径的configFile选项来查找配置但是它找不到测试.

以下gulpfile.js产生错误 ERROR [config]: File karma.conf.js does not exist!

var gulp = require('gulp'),
    // ....
    karma = require('karma').Server;

gulp.task('test', function(done) {
    var karmaServerOptions = {
        configFile: 'karma.conf.js', // works if relative path from ./node_modules/karma/lib/config.js
        singleRun: true
    };

    karma.start(
        karmaServerOptions,
        function(exitStatus) {
            done(exitStatus ? 'There are failing tests' : undefined);
        }
    );
});
Run Code Online (Sandbox Code Playgroud)

karma.conf.js:

// Karma configuration
// Generated on Thu Aug 06 2015 13:38:12 GMT+0100 (BST)
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: [
            // '**/*js',
            'node_modules/angular/angular.js',
            'app/**/*.js',
            // 'unitTests/**/*Spec.js',
            // 'unitTests/**/*spec.js'
            'unitTests/**/*.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 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
    })
}
Run Code Online (Sandbox Code Playgroud)

注意:文件数组有点混乱,因为它仍然有一些但不是全部的实验.

Hug*_*lle 16

看到gulp任务无法找到karma.conf.js进行外__dirname.

或使用:

var Server = require('karma').Server
gulp.task('test', function (done) {
   new Server({
     configFile: require('path').resolve('karma.conf.js'),
     singleRun: true
   }, done).start();
 });
Run Code Online (Sandbox Code Playgroud)