163

I have a bunch of failing specs from a rather large architectural change. I'd like to work on fixing them one by one by tagging each one with 'focus'.

Does jasmine.js have a feature like this? I swore I read at one point that it does but I don't see it in the docs.

Dane O'Connor
  • 67,996
  • 36
  • 114
  • 164

9 Answers9

277

When using Karma, you can enable only one test with fit or fdescribe (iit and ddescribe in Jasmine before 2.1).


This only runs Spec1:

// or "ddescribe" in Jasmine prior 2.1
fdescribe('Spec1', function () {
    it('should do something', function () {
        // ...
    });
});

describe('Spec2', function () {
    it('should do something', function () {
        // ...
    });
});

This only runs testA:

describe('Spec1', function () {

    // or "iit" in Jasmine prior 2.1
    fit('testA', function () {
        // ...
    });

    it('testB', function () {
        // ...
    });

});
Slava Fomin II
  • 21,036
  • 19
  • 98
  • 176
Hudvoy
  • 3,826
  • 4
  • 17
  • 25
  • 7
    its a good tip, but currently it isn't in jasmine. this is allowed by test runners like karma. for more info read on: https://github.com/pivotal/jasmine/pull/309 – p1100i Aug 12 '14 at 09:08
  • 78
    Jasmine 2.1 introduces this as `fdescribe` and `fit`: https://github.com/jasmine/jasmine/commit/ba0982d89f6d2a6523e505f062638a708ac0c25e#diff-3c2f8da4061d4489b0e5166eb05524c1R669 – alxndr Nov 22 '14 at 04:47
  • 10
    You'd have to be careful not to commit ii or dd specs. Easy to miss something like that. – Rimian Dec 29 '14 at 21:23
  • @jameshfisher the question was asked in 2011. Also, `iit` and `ddescribe` are additions of Karma, not Jasmine. – Justus Romijn Mar 25 '15 at 13:30
  • Am I the only one that thinks that mixing `xit` and `fit` into `it` are hard to read and error prone? – B Seven Apr 13 '15 at 22:40
  • You can define git precommit hook (or build step) that does jshint validation, and define `describe`, `it` as allowed globals. Then jshint will complain if it finds `fdescribe`, `fit`, `xdescribe`, `xit` etc. – jakub.g Dec 09 '15 at 12:32
  • @Rimian: The Jasmine ESLint plugin will catch this, unless you explicitly disable it. If you use a hook that lints your code before committing, it drastically reduces the chance of accidentally committing it. – Tarka Feb 03 '16 at 16:05
122

In core since 2.1 with fit and fdescribe.

Alain Duchesneau
  • 293
  • 1
  • 3
  • 11
phil pirozhkov
  • 4,342
  • 1
  • 29
  • 38
  • 1
    When I use 'fit' and run my tests in console using karma, I can see lot of skipped tests, but to see test errors I need to scroll to the top. When I run tests without 'fit' I do not have such problem, as I have errors summary at the bottom. Do you know how to fix this issue? – tytyryty Sep 01 '17 at 09:40
59

You can run a single spec by using the url for the spec

describe("MySpec", function() { 
  it('function 1', function() { 
    //... 
  }) 

  it('function 2', function() { 
    //... 
  } 

}) 

Now you can run just the whole spec by this url http://localhost:8888?spec=MySpec and a the first test with http://localhost:8888?spec=MySpec+function+1

Andreas Köberle
  • 88,409
  • 51
  • 246
  • 277
27

There are a few ways you can do it.

There is: Jasmine's feature Focused Specs (2.2): http://jasmine.github.io/2.2/focused_specs.html

Focusing specs will make it so that they are the only specs that run. Any spec declared with fit is focused.

describe("Focused specs", function() {
  fit("is focused and will run", function() {
    expect(true).toBeTruthy();
  });

  it('is not focused and will not run', function(){
    expect(true).toBeFalsy();
  });
});

However, I don't really like the idea of editing my tests (fit and fdescribe) to run them selectively. I prefer to use a test runner like karma which can filter out tests using a regular expression.

Here's an example using grunt.

$ grunt karma:dev watch --grep=mypattern

If you're using gulp (which is my favourite task runner), you can pass args into gulp-karma with yargs and match patterns by setting karma's config.

Kinda like this:

var Args = function(yargs) {
  var _match = yargs.m || yargs.match;
  var _file = yargs.f || yargs.file;
  return {
    match: function() { if (_match) { return {args: ['--grep', _match]} } }
  };
}(args.argv);


var Tasks = function() {
  var test = function() {
    return gulp.src(Files.testFiles)
      .pipe(karma({ configFile: 'karma.conf.js', client: Args.match()}))
      .on('error', function(err) { throw err; });
  };

  return {
    test: function() { return test() }
  }
}(Args);

gulp.task('default', ['build'], Tasks.test);

See my gist: https://gist.github.com/rimian/0f9b88266a0f63696f21

So now, I can run a single spec using the description:

My local test run: (Executed 1 of 14 (skipped 13))

gulp -m 'triggers the event when the API returns success'
[20:59:14] Using gulpfile ~/gulpfile.js
[20:59:14] Starting 'clean'...
[20:59:14] Finished 'clean' after 2.25 ms
[20:59:14] Starting 'build'...
[20:59:14] Finished 'build' after 17 ms
[20:59:14] Starting 'default'...
[20:59:14] Starting Karma server...
INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [watcher]: All files matched by "/spec/karma.conf.js" were excluded.
INFO [Chrome 42.0.2311 (Mac OS X 10.10.3)]: Connected on socket hivjQFvQbPdNT5Hje2x2 with id 44705181
Chrome 42.0.2311 (Mac OS X 10.10.3): Executed 1 of 14 (skipped 13) SUCCESS (0.012 secs / 0.009 secs)
[20:59:16] Finished 'default' after 2.08 s

Also see: https://github.com/karma-runner/karma-jasmine

Rimian
  • 32,654
  • 13
  • 106
  • 109
  • I like your idea of using grunt to selectively run tests, but I wish gulp and karma config was more flexible, easier to extend, and easier to understand for that matter. – Shawn J. Molloy Jun 13 '18 at 01:52
25

For anyone stumbling upon this, a better approach, which you can set up from the code itself, is to use this plugin: https://github.com/davemo/jasmine-only

It allows you set the spec exclusivity right on the code like this:

describe.only("MySpec", function() { 
  it('function 1', function() { 
    //... 
  }) 

  it.only('function 2', function() { 
    //... 
  }
})
// This won't be run if there are specs using describe.only/ddescribe or it.only/iit
describe("Spec 2", function(){}) 

There has been a long discussion to get this added to Jasmine core, see: https://github.com/pivotal/jasmine/pull/309

If you happen to be using Jasmine via Karma/Testacular you should already have access to ddescribe() and iit()

Justus Romijn
  • 13,863
  • 4
  • 47
  • 59
albemuth
  • 537
  • 1
  • 7
  • 12
8

You can create your all your specs up front but disable them with xdescribe and xit until you're ready to test them.

describe('BuckRogers', function () {
  it('shoots aliens', function () {
    // this will be tested
  });

  xit('rescues women', function () {
    // this won't
  });
});

// this whole function will be ignored
xdescribe('Alien', function () {
  it('dies when shot', function () {
  });
});
Eric Bock
  • 1,649
  • 1
  • 18
  • 21
2

This is the most simplified answer with a practical example .Even in fdescribe you can run few it blocks using it. f means focus.

Also in a none fdescribe block which is just describe, you can select only specific it blocks by marking them as fit.

Please run the below code and observe the console log, also read the comments in the code. Read this author's article it helps too . https://davidtang.io/2016/01/03/controlling-which-tests-run-in-jasmine.html

 //If you want to run few describe only add f so using focus those describe blocks and it's it block get run

  fdescribe("focus description i get run with all my it blocks ", function() {
    it("1 it in fdescribe get executed", function() {
        console.log("1 it in fdescribe get executed unless no fit within describe");

    });
    it("2 it in fdescribe get executed", function() {
        console.log("2 it in fdescribe get executed unless no fit within describe");

    });
    //but if you and fit in fdescribe block only the fit blocks get executed
    fit("3  only fit blocks in  fdescribe get executed", function() {
        console.log("If there is  a fit   in fdescribe only fit blocks  get executed");

    });
  });

  describe("none description i get skipped with all my it blocks ", function() {
        it("1 it in none describe get skipped", function() {
            console.log("1 it in none describe get skipped");

        });
        it("2 it in none describe get skipped", function() {
            console.log("2 it in none describe get skipped");
        });
//What happen if we had fit in a none fdescribe block will it get run ?   yes  
       fit("3 fit in none describe get executed too eventhough it;s just describe ", function() {
            console.log("3 fit in none describe get executed too");
        }); 
      });
Sameera De Silva
  • 700
  • 7
  • 20
1

With stand-alone Jasmine(2.0.0), on the spec_runner.htlm, I could click a specific spec and focus on that one spec. I should have noticed this feature earlier.

taro
  • 591
  • 1
  • 7
  • 19
1

Not exactly what you've asked for but adding iit will test only that particular spec and ignore all others in the file, ddescribe works in the same way. So you can focus on a particular spec using iit or ddescribe

Filype
  • 6,921
  • 7
  • 36
  • 60