πŸ’‘ This page contain affiliate links. By making a purchase through them, we may earn a commission at no extra cost to you.
What is the Difference Between Describe, Test, and It in Jest?

What is the Difference Between Describe, Test, and It in Jest?

Ferenc Almasi β€’ 2022 July 08 β€’ Read time 2 min read
  • twitter
  • facebook

Jest comes with three different built-in functions for organizing test cases. They are describe, test, and it. Whenever you start writing your test files from an empty file you should always start with a describe:

Copied to clipboard!
// Use describe to group several related tests
describe('Several related tests', () => {
    // Your individual test cases will go here
})

Use describe to create blocks that group several related tests together. Depending on your testing approach, you might have a describe for each individual component in your application, each module, or each class.

Whichever approach you take, make sure it groups related tests together. You can also nest describe blocks into each other to further break down your tests and improve readability if needed.

Copied to clipboard! Playground
// You can nest describe into each other
describe('Sort & Filters', () => {
    describe('Sorting', () => {
        // Sorting related tests
    })

    describe('Filtering', () => {
        // Filtering related tests
    })
})

The test and it blocks in Jest

Use test to execute individual test cases. Different functionality should be tested in different blocks. You should be able to describe each test with a single sentence. It's a common practice to start each block with the word "should" to describe what should happen in the block.

Copied to clipboard! Playground
describe('Several related tests', () => {
    test('Should execute a single test', () => { ... })
    it('Should do the same as test', () => { ... })
})

describe('Filters', () => {
    it('Should filter for best tutorials', () => { ... })
    it('Should filter by rating', () => { ... })
    it('Should filter by category', () => { ... })
})

This should be the smallest part of your test cases, you should not be able to further break down your test blocks into smaller, multiple test blocks.

For better organizing your test cases, make sure you define your test blocks inside a describe. The it block is an alias for test, it works exactly the same way, therefore It's only a matter of choice whether you use test or it.

  • twitter
  • facebook
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.