Unit testing with Jest

Important code and components should be unit tested to ensure that they are working as intended and to prevent most regressions when the code evolves. The recommended test runner for Vue components is Jest from Facebook. It is quite fast with a cache system and has an handy snapshot feature to help detect regressions even more.

  1. First, install Jest and the official Vue unit testing tools:
      npm i -D jest vue-test-utils
  1. We also need a few utilities related to Vue to compile the .vue files with jest-vue and to take snapshots of the components:
      npm i -D vue-jest jest-serializer-vue vue-server-renderer
The recommended way to get the HTML render of a component in node is using the vue-server-renderer package used to do server-side rendering as we will see later in the chapter.
  1. Finally, we will need some babel packages to support babel compilation and webpack dynamic imports inside Jest:
      npm i -D babel-jest babel-plugin-dynamic-import-node
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.29.73