JavaScript

Running specific test cases in Jasmine or Mocha

Advertisements

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 significantly, 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. Sadly, they did not work. Others mentioned some changes to configuration files, which I could not do since I was using a CLI that had encapsulated all of those files.

However, 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 that I did not want to run or delete other files temporarily. I found 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!

Running specific test cases in Jasmine

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

describe("test spec",() => {
    it("test case 1",() => {});
    fit("test case 2", () => {});
});
JavaScript

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.

Running specific test cases in Mocha

For Mocha, we need to use use it.only() instead of fit() to achieve the same result.

describe("test spec",() => {
    it("test case 1",() => {});
    it.only("test case 2", () => {});
});
JavaScript

Excluding specific test cases or specs in Jasmine

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 all of the following test cases would get skipped during execution:

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

The output in the terminal would read:

Executed 0 of 3 test cases (skipped 3) SUCCESS
Executed 0 of 3 test cases (skipped 3) SUCCESS
JavaScript

Excluding specific test cases or specs in Mocha

For Mocha, we need to use use describe.skip() instead of xdescribe() to achieve the same result.

describe.skip("test spec", function() {
    it("test case", () => {});
    it("test case", () => {});
});
JavaScript

For skipping individual test cases:

describe("test spec", function() {
    it.skip("test case", () => {});
    it("test case", () => {});
});
JavaScript

Conclusion

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

I hope this post helped you. Let us know if you have any queries in the comment section below.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

View Comments

  • 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?

Share
Published by
Saransh Kataria

Recent Posts

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

24 hours ago

Native popover API in HTML

Popovers have been a problem that was typically solved by using a third-party solution. But…

1 week ago

Node.js 20.6 adds built-in support for .env files

Node.js 20.6 added built-in support for the .env file. This is an excellent addition to the platform…

2 weeks ago

Object destructuring in TypeScript

Object destructuring is a powerful ES 6 feature that can help developers write cleaner code.…

4 weeks ago

Improve git clone performance in a CI pipeline

Have you felt particularly annoyed by the time it takes to clone a large repository,…

1 month ago

Fix: Hydration failed because the initial UI does not match what was rendered on the server

Within a React or Next.js app, if you encounter the error "Hydration failed because the…

1 month ago
Advertisements