Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Mocha's "it.only" For Running A Single Test In Your Describe Block

5/8/2018

0 Comments

 
Well guys, I learned a cool new thing today! Did you know about the "it.only" syntax in mocha? Can you believe I just discovered it today?! Yep, my life has forever changed for the better now that I'm aware of in.only, and since you're reading this post right now I hope it will change yours too!

Am I Lazy Or Just Really Efficient?

I'm a huge fan of automated testing in JavaScript (in fact, I dream about describe's full of it's with some beforeEach's). However, there was one thing that really seemed to drive me crazy. Sometimes I would be working in one describe block, you know, describing some functionality. I would have a few tests in the describe block, but most of the time when working on them I would really only care about one test at a time.  Since the order of execution of the tests is not guaranteed (and since I sometimes use a lot of console.logs when developing) I would find myself scrolling like a madman through my console output, lost like nun at a monster truck carnival on new year's day.  By limiting the test execution to evaluate only the single test in working on at the moment, I can focus and work pleasantly. Now the ugly, inefficient, brute-force way to make only a single test in a describe block run would be to take all of the it's that you don't want to be executed and replace them with "xit" (and in fact this is what I did in mocha before learning about it.only. I hate to admit it and feel like a god damn caveman, but a true hero will be honest about his (or her) errors). The amount of changes you need to make here of course is (N - 1) where n is the number of its in the given describe block. This can be a minor annoyance or an unbearable curse, depending on how anal you are and the size of your test suite. When I was using Jasmine back in the dizzle dizzay I would use "iit" instead of "it", and the test runner would only execute that specific test (although it seems like this has actually has been changed to "fit". Welp, mocha has a nice syntax for running only a single test in a suite too.

The it.only Magic

It's really not that complicated at all. Instead of writing "it" in your code, you just run "it.only"! For example, the code below will only run the test, "should do some cool stuff!".
describe('an example using it.only', () => {

  it('should do things', () =>  {

  })

  it('should do more things', () => {

  })

  it.only('should do some cool stuff!', () => {

  })

})
Now, you can toggle on / off a single test to be run with O(1) changes your code so you can focus on kicking ass and writing tests. Happy coding! 

Special Thanks

I just wanted to give a shoutout to Jake Trent and his blog post about mocha's it.only which made me aware of this nifty thing in the first place. Thanks Trent!
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Picture
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    March 2023
    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2023