Running specific test cases in Jasmine

I recently was working on writing end to end test cases using protractor on a project. Everything was working fine until the number of test cases increased to a lot and running the complete set of test cases took too much time. It was a pain to even think of running the e2e command since I knew it would take 10 minutes at least to get to a specific test.

I started searching for a way to run a specific test case and there were some solutions on Google and Stack Overflow which provided command line arguments while running the test cases but they weren’t working. And others mentioned some changes to configuration files which I could not do since I was using a CLI which had encapsulated all of those files.

- Advertisement -

But controlling which test case to execute was something I needed to do. I wanted to run a single test case using jasmine. And I was not going to comment out test cases which I did not want to run or delete other files temporarily. I found out a temporary workaround for running specific test cases in Jasmine. Instead of the tedious task of commenting test cases, all you need to do is add a character to your file/test cases!

Excluding specific test cases or specs

To exclude a specific test case or spec, all you need to do is use xit() or xdescribe() instead of it() and describe(). And that is it!

This means that the following test cases would get skipped during execution

xdescribe("test spec", function() {
    it("test case 1", function() {
    });
    it("test case 2", function() {
    });
});
describe("test spec", function() {
    xit("test case", function() {
    });
});

And the output in the terminal would read:

Executed 1 of 3 test cases (skipped 2) SUCCESS

This still might not be what you want and you explicitly want only one test case to run. Excluding multiple specs or test cases in multiple files can still be a tedious task in that case.

Running specific test cases in Jasmine

You can use fit() or fdescribe() instead of it() and describe() to achieve what is known as focussed test cases.

describe("test spec", function() {
    it("test case 1", function() {
    });
    fit("test case 2", function() {
    });
});

In the above example, only test case 2 would get executed. On execution, a single test (test case 2) case gets executed. Multiple fit() calls can be used to run specific test cases. And that is how you make running specific test cases an easy thing.

Hope this post helped you and let us know if you have any queries in the comment section below.

Recent Articles

Use redux-like middleware for useReducer in React

If you have used Redux before, you would be aware of the concept of middlewares. Now that useReducer has become a commonly...

How to write your own custom React hooks

If you have been using react for a while, chances are you have come across the need to extract some logic into...

Palindromic Substrings: Demystifying Programming Interview

This is the second post in the Demystifying Programming Interview series, which aims at helping students and professionals who are planning to...

Converting a business problem to a machine learning problem

Businesses these days are generating and consuming a huge amount of data. This data varies in variety, volume, velocity, and veracity (4Vs...

How to develop your curiosity for a better self?

Curiosity is the reason why most of the breakthrough discoveries and remarkable inventions have happened throughout history. In the absence of curiosity,...

Related Stories

2 Comments

  1. Do we have any ways in which we can control the test cases from the external file .
    For Eg: I have 5 it blocks in my spec . I want to run only 4th and 5th it blocks . However , I don’t want to edit my spec file . From an external file can I input the information(4 and 5 here ) of which it block to run ?

    • None that I know of. There were some configuration mechanisms that I found while Googling for this but none really worked for me. Why do you want to control it from an external file though?

Leave A Reply

Please enter your comment!
Please enter your name here

Hi there! Want some more knowledge?

Think that the knowledge shared is helpful? You might want to give our mailing list a try. We'll send you 2-4 emails a month, right when new posts come out.

Hi there! Want some more knowledge?

Think that the knowledge shared is helpful? You might want to give our mailing list a try. We'll send you 2-4 emails a month, right when new posts come out.