首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >茉莉花吞咽,运行测试上的测试文件更改

茉莉花吞咽,运行测试上的测试文件更改
EN

Stack Overflow用户
提问于 2015-08-30 07:12:38
回答 3查看 2.5K关注 0票数 4

嗨,我想要做的是用watcher task来运行我的茉莉测试。我到目前为止所做的事:

代码语言:javascript
复制
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规则的文件时,它不会在控制台中显示任何内容。

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

代码语言:javascript
复制
gulp.task('tests.run', function (cb) {
    gulp.src(testsFiles)
        .pipe(jasmine({verbose:true}));
});

它的工作原理和接下来的演示:8 specs, 0 failures Finished in 0 seconds

也许我错过了什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-03-09 17:43:04

要运行只需要的规范,请尝试如下所示:

代码语言:javascript
复制
/** 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);
        }
      }
    }
  });
});

这个观察者使用两个函数,一个是根据文件名派生规范名称。就我而言,它是:

代码语言:javascript
复制
/**
 * 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,它使用给定的测试文件运行茉莉。

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

票数 1
EN

Stack Overflow用户

发布于 2015-08-30 22:14:06

分两步做

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

代码语言:javascript
复制
gulp.task('tests.run', function () {
    return gulp.src(testsFiles)
        .pipe(jasmine({verbose:true}));
});

2)声明当这些testsFiles更改时将运行此测试单元任务的监视任务。

代码语言:javascript
复制
gulp.task('tests.watch', function () {
    gulp.watch(testsFiles, ['tests.run']);
});

然后,运行gulp tests.watch

票数 4
EN

Stack Overflow用户

发布于 2016-12-09 17:32:44

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

代码语言:javascript
复制
"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...');
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32294180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档