DEV Community

loading...
Cover image for Running a single test suite/spec in Jasmine

Running a single test suite/spec in Jasmine

Dzhavat Ushev
Front-end developer trying to make useful stuff.
Originally published at dzhavat.github.io ・2 min read

This post was originally published on my blog.


Every developer knows that writing tests is important. To me, tests are like an evidence that I present to my team (or my future self) that the code I’ve written actually works the way I claim it be. This sounds nice and stuff but it wasn’t until recently that I started writing actual tests.

Something I’ve noticed, even with my little testing experience, is that test suites can grow pretty fast. It’s not long before you end up with a bunch of suites each containing a bunch of specs and then you ask yourself “How can I run only a single suite/spec instead of the whole thing?”

I’ve certainly asked myself this question more than once. So if you write your tests using Jasmine, I have a simple solution:

Running a single suite

Does this look familiar?

describe("A suite", function() {
  // ... specs here
});

Well, if you put an f in front of describe, meaning fdescribe, Jasmine will only run that particular test suite.

fdescribe("A suite", function() {
  // ... specs here
});

Running a single spec

Normal stuff here...

describe("A suite", function() {
  it("should check whether `true` is really `true`", function() {
    expect(true).toBe(true);
  });
});

Again, if you put f in front of it, meaning fit, Jasmine will run only that particular spec.

describe("A suite", function() {
  fit("should check whether `true` is really `true`", function() {
    expect(true).toBe(true);
  });
});

So here you go. If, for whatever reason, you need to run a single suite/spec, you can quickly do so by using fdescribe and fit. You can find this in the official docs as well. So I’ve definitely not given you the hottest tip out there, but I find it useful. Hope you do as well!

Discussion (12)

Collapse
dansilcox profile image
Dan Silcox

Definitely beats messing with the test runner bootstrap! Don’t commit your fdescribes though as this might give you false confidence running just one test instead of the whole suite and just seeing ‘tests passed’ in CI. Not sure if you know about xdescribes too - skip a certain test! Although again don’t commit them or your future team mates/self will not be happy having to fix potentially broken disabled tests!

Collapse
dzhavat profile image
Dzhavat Ushev Author

I've already had cases where I committed an fdescribe/fit. If catched on time, quickly amending the commit fixes the issue. But I agree that it's easy to oversee. Can this be a good lint opportunity? Not sure.

Yes, I've seen xdescribe. Haven't used it though. I think xdescribe is less "destructive" compared to fdescribe. The former just disables a suite/spec (even though this might be bad as well), where the latter disables everything else but the suite/spec in focus.

Collapse
bradtaniguchi profile image
Brad

I believe in future versions (3+?) of jasmine using fdescribe and fit will make the overall process "fail" as the tests will be incomplete. I personally usually put a check to verify a certain coverage of tests are ran, so if I do accidentally commit these, the coverage check will fail.

Collapse
dzhavat profile image
Dzhavat Ushev Author

A coverage check sounds like a good strategy to guard against accidental fdescribe/fit. I haven't seen yet the first part where Jasmine will fail overall, but this too makes sense.

Collapse
diegomgar profile image
Dieg Oto

Question here! How do i test only the components i want to? I want to be able to configurate different goals by components.

Collapse
daliborp profile image
Dalibor Plavcic • Edited

Hey, I had to accomplish the same thing on the enterprise project I'm currently working on. The good news is that it's quite easy to accomplish that and make it scalable, which means no fit/xit etc..

You can find an example of custom Jasmine configuration used inside Angular at my blog here: allthingsangular.com/introduction-...

It can be used to specify (include/exclude) exact folders, components, services or anything you want to test

If you need any help with setup just ping me.

cheers

Collapse
dzhavat profile image
Dzhavat Ushev Author

Oh, nice approach with a custom config! Hadn't thought of that. I'm not about the scalability because this will require multiple configs for different purposes, am I right? Smart idea nonetheless! Thanks for sharing it.

Thread Thread
daliborp profile image
Dalibor Plavcic

Maybe I have used the word scalability with wrong connotation here. The goal for me was to execute only specific tests in specific module/submodules to save me some time in cases where I have a a of Jasmine tests.

Yes, you are right. To achieve this you need to add a little bit of additional configuration. Nonetheless it's all about the context we are working in :)

Collapse
dzhavat profile image
Dzhavat Ushev Author

Hey, I'd like to help (if I can) but don't understand your question. What do you mean by testing only the components you want to?

Collapse
diegomgar profile image
Dieg Oto

Hi! Thank you! I'm thinking that maybe this is not the thread. I'm talking about angular.
I mean, with Java you can run the test you want to, you can even create groups and run the test in this group but not the rest in the project.
I want to, if I have 80 components, run for example 10. I can do this with karma + Jasmine?

Thread Thread
dzhavat profile image
Dzhavat Ushev Author

I've tried to search a bit on the internet for a solution but haven't found a good one. If you split your code in different libraries, you can easily run the tests for a specific library by using ng test <project>. You can also use the approach in this article by using fdescribe/fit to run specific suites/specs but I recommend only doing this locally. But be cautious using this approach because it's quite easy to commit fdescribe/fit and see your tests pass when in reality it's only one suite that is being executed.

Thread Thread
diegomgar profile image
Dieg Oto

Thank you, I will look out the fdescribe approach :)