Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between Vue-Test-Utils' "mount" and "shallowMount"?

Disclaimer: I am pretty new to Vue, JavaScript, and web frameworks in general.

I'm trying to familiarise myself with some basic unit and component tests using Jest and vue-test-utils.

I have read the docs on vue-test-utils' mount() and shallowMount() , but I'm not sure when to use the one over the other (they appear to be very similar).

According to the docs on shallowMount():

Like mount, it creates a Wrapper that contains the mounted and rendered Vue component, but with stubbed child components.

What exactly is meant by "stubbed child components"? Can mount() and shallowMount() be used interchangeably?

like image 678
AnonymousAngelo Avatar asked Nov 09 '18 11:11

AnonymousAngelo


People also ask

What is shallowMount Vuejs?

shallowMount() Like mount , it creates a Wrapper that contains the mounted and rendered Vue component, but with stubbed child components.

Which testing framework is best for Vue?

1. Component Testing with Vue Test Utils & Jest. Jest is a well-liked JavaScript testing framework that comes packed with many goodies for developers focusing on simplicity. It's one of the fastest testing frameworks for Vue single-file components.

Does Vue test utils use jest?

The plugin pulls all required dependencies (including jest), creates a jest. config. js file with sensible defaults, and generates a sample test suite. After that, all you need to do is to install Vue Test Utils.


1 Answers

What the documentation means by "stubbed child components" is that every components within the tested one will not be rendered. Instead, you will have a placeholder component.

This prevent your tests to be parasite by other component's behaviors.

In my opinion, you should always shallow mount your components when doing unit tests, and simply mount them when doing tests in your entire application.

like image 127
Thomas Ferro Avatar answered Sep 20 '22 16:09

Thomas Ferro