2

I have just start learning unit testing in Vue.js using Jest, and I want to know when to use describe and when to use test ?

TodoApp.vue Component :

<template>
  <div>
    <h1>TodoApp Componenent</h1>
    <div v-for="todo in todos" :key="todo.id" data-test="todo">
      {{ todo.text }}
    </div>
  </div>
</template>

<script>
export default {
  name: "TodoApp",
  data() {
    return {
      todos: [
        {
          id: 1,
          text: 'Learn Vue.js 3',
          completed: false
        }
      ]
    }
  }
};
</script>

<style scoped>
</style>

TodoApp.spec.js

1# Unit test using test

import { mount } from '@vue/test-utils'
import TodoApp from '@/components/TodoApp'

test('renders a todo', () => {
  const wrapper = mount(TodoApp)

  const todo = wrapper.get('[data-test="todo"]')

  expect(todo.text()).toBe('Learn Vue.js 2')
})

2# Unit test using describe

import { mount } from '@vue/test-utils'
import TodoApp from '@/components/TodoApp'

describe('TodoApp', () => {
  it('renders a todo', () => {
    const wrapper = mount(TodoApp)

    const todo = wrapper.get('[data-test="todo"]')
  
    expect(todo.text()).toBe('Learn Vue.js 2')
  })
})
Ousama
  • 2,176
  • 3
  • 21
  • 26

2 Answers2

4

describe is a way to create a suite of tests. test and it are a single test case in a suite (or not). The names are chained together. It is helpful sometimes to think of the names of tests as sentences.

describe('when a user does x', () => {
   beforeEach(() => x());

   it('should do something', () => expect(something).toBe(true));

   it('should not do something else', () => expect(somethingElse).toBe(false));
});
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
2

test and it are just aliases of one another-- they both do the same thing. describe is used to define a "suite" of tests, and will contain one or more nested it or test function.

Alexander Nied
  • 12,804
  • 4
  • 25
  • 45