使用Gulp.js自动运行Jasmine测试?

时间:2014-04-29 09:25:12

标签: javascript jasmine gulp

我有基本的Gulp任务,并设置了基本的Jasmine测试运行器。如何使用Gulp在文件保存上运行我的Jasmine测试并向我显示输出?茉莉花去哪儿管?

我对这些库以及JS构建和单元测试系统都很陌生,所以不确定问题和最佳实践,我还没有在网上找到足够基本的东西:-)

2 个答案:

答案 0 :(得分:2)

您可以使用gulp.watch,这会监视文件并在文件更改时执行某些操作。

您可以执行此类任务,或只是在另一项任务中使用gulp.watch

gulp.task('watch', function () {
    gulp.watch('tests/**/*', ['jasmine']);
});

答案 1 :(得分:0)

使用此脚本运行测试。您需要设置Karma以便观察您的文件。

您追求的这两个参数:

...
singleRun: false,
autoWatch: true,
...

THE SCRIPT:

'use strict';

var gulp = require('gulp');
var wiredep = require('wiredep');
var karmaServer = require('karma').Server;

/**
 * Run the unit tests
 */
gulp.task('test', ['templates'], function () {
    var bowerDeps = wiredep({
        directory: 'src/bower_components',
        exclude: ['exclude_some_thing_you_dont_need'],
        dependencies: true,
        devDependencies: true
    });

    // This will jQuery to the start so that AngularJS won't use jqLite
    bowerDeps.js.unshift('src/bower_components/jquery/dist/jquery.js');

    var testFiles = bowerDeps.js.concat([
        'src/app/app.js',    // loading the module first to make Karma avoid nomod-errors
        'src/{app,components}/**/*.js',
        '.tmp/templates/*.js',     // HTML-templates converted to JS for directives/etc
        'test/unit/**/*.js'
    ]);

    new karmaServer({
        files: testFiles,
        singleRun: false,
        autoWatch: true,
        configFile: __dirname + '/../test/karma.conf.js'
    }).start();
});