【问题标题】:jasmine with gulp, run tests on test file changedjasmine 与 gulp,在测试文件上运行测试已更改
【发布时间】:2015-08-30 07:12:38
【问题描述】:

嗨,我想做的是用gulp 制作watcher task,这将运行我的茉莉花测试。到目前为止我做了什么:

var watch = require("gulp-watch");
var jasmine = require("gulp-jasmine");

gulp.task('tests.run.change-watcher', function (cb) {
    gulp.src(testsFiles)
        .pipe(watch(testsFiles))
        .pipe(jasmine({ verbose: true }));
});

但是当我运行该任务并尝试更改任何符合testsFiles 规则的文件时,它不会在控制台中显示任何内容。

但是当我运行下一个任务时:

gulp.task('tests.run', function (cb) {
    gulp.src(testsFiles)
        .pipe(jasmine({verbose:true}));
});

它工作并显示下一个: 8 specs, 0 failures Finished in 0 seconds

也许我错过了什么?

【问题讨论】:

    标签: node.js jasmine gulp gulp-watch gulp-jasmine


    【解决方案1】:

    分两步完成

    1) 声明测试单元任务(就像你做的那样)

    gulp.task('tests.run', function () {
        return gulp.src(testsFiles)
            .pipe(jasmine({verbose:true}));
    });
    

    2) 声明当testsFiles 发生变化时将运行此测试单元任务的监视任务

    gulp.task('tests.watch', function () {
        gulp.watch(testsFiles, ['tests.run']);
    });
    

    然后,你运行gulp tests.watch

    【讨论】:

    • 在这种情况下,即使是一次文件更改,它也会运行所有测试。但我想在仅更改的文件中运行测试。
    【解决方案2】:

    要仅运行所需的规范,请尝试以下操作:

    /** Watches file changes in source or spec files and executes specs automatically */
    gulp.task("specs-watcher", function() {
      return watch(["src/**/*.ts", "spec/**/*.ts"], { events: ["add", "change"] }, function(vinyl, event) {
        if (!vinyl.isDirectory()) {
          if (vinyl.basename.endsWith(".spec.ts")) {
            // We are dealing with a spec file here, so call jasmine!
            runJasmine(vinyl.path);
          } else {
            // Try to find out specs file
            const specFilePath = findSpecsFile(vinyl);
            if (typeof specFilePath === "string") {
              runJasmine(specFilePath);
            }
          }
        }
      });
    });
    

    这个watcher使用了两个函数,一个是根据文件名导出spec名称。就我而言,它是:

    /**
     * For your specs-watcher: This function is called every time a file changed which doesn't end with '.spec.ts'.
     * The function's task is to return the fitting specs path of this file. For example by looking for a corresponding file in the "/spec/" folder.
     * @param {vinyl} changedFile Vinyl object of changed file (see https://github.com/gulpjs/vinyl)
     * @return {string|undefined} Path to the specs file to execute or undefined if your watcher shouldn't do anything.
     */
    function findSpecsFile(changedFile) {
      return changedFile.path.replace(__dirname, `${__dirname}/spec`).replace(".ts", ".spec.ts");
    }
    

    另一个函数是runJasmine,它使用给定的测试文件运行 jasmine。

    只要让一切都适合您的设置,它应该可以工作。 :-)

    【讨论】:

      【解决方案3】:

      您可以通过以下方式监听测试和源代码文件夹的文件更改:

      "use strict";
      
      var gulp = require('gulp');
      var mocha = require('gulp-mocha');
      var batch = require('gulp-batch');
      
      gulp.watch(['tests/**', 'src/**'], batch(function (events, cb) {
          return gulp.src(['tests/*.js'])
              .pipe(jasmine({ verbose: true }))
              .on('error', function (err) {
                  console.log(err.stack);
              });
      }));
      
      gulp.task('default', () => {
          console.log('Gulp is watching file changes...');
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-02
        • 1970-01-01
        • 1970-01-01
        • 2015-11-12
        • 2016-06-13
        • 1970-01-01
        • 1970-01-01
        • 2012-07-07
        相关资源
        最近更新 更多