Book Image

Testing Vue.js Components with Jest

By : Alex Jover Morales
Book Image

Testing Vue.js Components with Jest

By: Alex Jover Morales

Overview of this book

<p>Unit testing in modern component-based JavaScript frameworks is not straightforward. You need a test suite that is reliable and runs quickly. Components are connected to one another, and the browser adds a layer of UI, which makes everything inter-dependent while we test components in isolation. Jest is a fully-featured JavaScript testing framework that will do all your work for you. </p><p> </p><p>This book shows you how to test Vue.js components easily and take advantage of the fully-featured Jest testing framework with the aid of practical examples. You'll learn the different testing styles and their structures. You'll also explore how your Vue.js components respond to various tests. You'll see how to apply techniques such as snapshot testing, shallow rendering, module dependency mocking, and module aliasing to make your tests smooth and clean. </p><p> </p><p>By the end of this book, you'll know all about testing your components by utilizing the features of Jest.</p>
Table of Contents (10 chapters)

Webpack Aliases

Webpack aliases (https://webpack.js.org/configuration/resolve/#resolve-alias) are very simple to set up. You just need to add a resolve.alias property to your webpack configuration. If you take a look at build/webpack.base.conf.js, it already has it defined:

module.exports = {

  // ...

  resolve: {

    extensions: [".js", ".vue", ".json"],

    alias: {

      vue$: "vue/dist/vue.esm.js"

    }

  }

};

Taking this as an entry point, we can add a simple alias that points to the src folder and use that as the root:

module.exports = {

  // ...

  resolve: {

    extensions: [".js", ".vue", ".json"],

    alias: {

      vue$: "vue/dist/vue.esm.js",

      "@": path.join(__dirname...