DEV Community

Karl Taylor
Karl Taylor

Posted on

Use test.todo() when writing Jest tests.

When I start building a new component, I can sometimes completely forget to write tests as I am going, or perhaps I've finished writing my component, and I don't fully remember what I should be writing in my test suite.

A quick solution to this if you are using Jest is to build your test suite, and replace your tests with test TODOS!

describe('NewsContent', () => {
  it('Should render a normal string', () => {}) // Passes, but it's not complete! โ˜น๏ธ
  it('Should render a very long string') // This will fail โ˜น๏ธ

Instead, we can do it.todo('My todo test description').

describe('NewsContent', () => {
  it.todo('Should render a normal string') // This will show up as a todo in our test suite! Woohoo! 

The beauty of this is that we get visual feedback in our test suite in the terminal that we have outstanding tests to finish.

Alt Text

Now you never have a reason to not hit that diff-coverage threshold ๐Ÿค“

What is your go-to process when building new components and adding tests?

Top comments (4)

gabrielduete profile image
Gabriel Duete • Edited

Awesome Feature!!

I used to put comments on the component itself until a friend recommended this article to me in a code review.

Nice job, Thank you! ๐Ÿงก

soullivaneuh profile image

This feature is nuts!

However, I am wondering if there is a built-in way to make test.todo jest failing for CI purposes.

duncanhaywood profile image
Duncan Haywood

Thank you for this helpful article, this helped me out in a long search of how to do this. Much thanks, Duncan Haywood.

marcellothearcane profile image

Is there a way of listing all the 'todo' tests quickly, rather than having to go through every test?