What is a unit test

Unit testing is a type of software testing that tests individual units or components of software. The goal is to verify that each unit of software code performs as expected. Unit testing is done by developers during the development (coding) phase of the application. Unit tests isolate a piece of code and verify its correctness. A unit may be a single function, method, procedure, module, or object.

Why do unit tests

  • During the development phase, unit testing can help find and fix bugs earlier and save money

  • It helps developers understand the code base and enables them to make changes quickly

  • Good unit tests are equivalent to project documentation

  • Unit tests facilitate code reuse by migrating our code and tests to the new project. Tweak the code until the test runs again.

Benefits of unit testing

  • Through unit testing, developers can learn what functionality is provided for unit testingAPIHave a basic understanding
  • Unit testing allows programmers to refactor the code later and ensure that the module still works (regression testing). The process is to write test cases for all functions and methods so that whenever a change causes a failure, it can be quickly identified and fixed.
  • Because of the modular nature of unit testing, we can test parts of a project without waiting for other parts to complete.

The downside of unit testing

  • Unit tests cannot catch every error in a program. Even in the simplest programs, it is impossible to evaluate the path of every unit test execution.
  • The essence of unit testing is to focus on units of code. Therefore, it does not catch integration test errors or broader system-level errors.

Recommendation: Use unit tests with other testing tools

Unit tests are used in VUE projects

We use vue-test-utils, the official vue. js unit testing utility library, to write unit tests in Vue applications.

The installation

Here, we have generated the project through VUE scaffolding by default, so we can integrate the Unit-Jest plug-in.

vue add @vue/unit-jest
Copy the code

Write simple test cases

Unit test files:

// tests/unit/example.spec.js
import { shallowMount } from '@vue/test-utils'
import HelloWorld from '@/components/HelloWorld.vue'

describe('HelloWorld.vue'.() = > {
  it('renders props.msg when passed'.() = > {
    const msg = 'new message'
    const wrapper = shallowMount(HelloWorld, {
      propsData: { msg }
    })
    expect(wrapper.text()).toMatch(msg)
  })
})
Copy the code

Unit test components:

<! --scr/components/HellowWorld.vue--> <template> <div class="hello-world">msg is :{{msg}}</div> </template> <script> export default { name: 'hello-world', data() { return {} }, props: { msg: { type: String, default: 'this is props msg' } } } </script>Copy the code

In this case, execute the following command line:

Yarn Run test:unit or NPM run test:unitCopy the code

At this point, you can see the test case running on the console

The resources

  • www.guru99.com/unit-testin…
  • Jest’s official website