jasmine with gulp, run tests on test file changed

2.5k views Asked by At

Hi what I trying to do is to make watcher task with gulp which will run my jasmine tests. What I have done so far:

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 }));
});

But when I run that task and try to change any file which meets the testsFiles rules it doesn't show anything in console.

However when I run the next task:

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

It works and shows next: 8 specs, 0 failures Finished in 0 seconds

Maybe I miss something?

3

There are 3 answers

0
Toni On BEST ANSWER

To run only needed specs, try something like this:

/** 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);
        }
      }
    }
  });
});

This watcher uses two functions, one is for deriving the spec name based on the file name. In my case, it's:

/**
 * 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");
}

The other function is runJasmine, which runs jasmine with a given test file.

Just make everything fit to your setup and it should work. :-)

1
topheman On

Do it in two steps

1) Declare the test-unit task (like you did)

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

2) Declare the watch task that will run this test-unit task when those testsFiles change

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

Then, you run gulp tests.watch

0
Rober On

You can listen to file changes for both tests and source code folders with this:

"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...');
});