Am I Lazy Or Just Really Efficient?
The it.only Magic
describe('an example using it.only', () => { it('should do things', () => { }) it('should do more things', () => { }) it.only('should do some cool stuff!', () => { }) })
Writings about one coder's stories & experiences.
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. |
AuthorThe posts on this site are written and maintained by Jim Lynch. About Jim...
Categories
All
Archives
March 2023
|