Organizing Vue component tests with Jest
Best practices for creating and organizing component test files with Jest in Vue JS.
Best practices
- Isolate the component: make sure to test the component in isolation, without depending on other components or services. This ensures you can easily pinpoint any issue in that specific component.
- Test properties: test the component's properties and make sure they work correctly. Check that the component renders with the correct props and reacts correctly to prop changes.
- Test events: test the events emitted by the component and check that they work correctly. Make sure events are emitted with the correct data and that parent components respond correctly to the emitted events.
- Test state: test the component's internal state and make sure it works correctly. Check that the component reacts correctly to user actions and state changes.
- Test snapshot: Snapshot testing is a good practice to make sure the component's HTML structure doesn't change without you noticing. But it shouldn't be used as the only testing method.
- Use
vue-test-utils: thevue-test-utilslibrary makes it easier to write tests for Vue components, providing tools to mount and interact with them. - Keep test files together: keep test files alongside the component being tested. This makes it easier to maintain and track what's being tested.
- Use clear, descriptive test function names: clear, descriptive test names make it easier to understand what the test is checking without having to read the whole code.
Naming and organizing test files
Component test files with Jest in Vue JS should have names that are easy to identify and clearly indicate which component is being tested. A good practice is to name the test file after the component, followed by .spec or .test to indicate that it's a test file.
For example, if you have a component called TheButton.vue, the corresponding test file should be called TheButton.spec.js or TheButton.test.js.
You can also put test files inside a folder called __tests__ to keep them organized and separate from production files.
Example:
// component
components/TheButton.vue
// test
components/__tests__/TheButton.spec.js
The file name is important to help keep the code organized and easy to understand, but it isn't a strict rule. What matters most is following a consistent convention across the whole project.
Wrappers
In tests, we'll use wrappers. A component wrapper in VueJS is an object that wraps the Vue component and provides access to its properties and methods, including its rendering and internal state. It's used mainly for unit and integration testing, since it lets you interact with the component as if it were being used in the application.
Wrappers are created using testing tools like Vue Test Utils, and they usually include useful methods like find, setData, setProps, trigger and emitted, among others, which let you test the component's functionality in isolation and make sure it's working correctly.
Vue Test Utils, for example, provides methods for creating component wrappers, like mount and shallowMount, which let you create an instance of a Vue component and test its rendering and behavior.
Example: testing a dynamic input component
import { shallowMount } from "@vue/test-utils";
import TheInput from "@/components/atoms/TheInput";
describe("TheInput.vue", () => {
it("renders props.placeholder when passed", () => {
const placeholder = "new message";
const wrapper = shallowMount(TheInput, {
propsData: { placeholder },
});
expect(wrapper.find("input").attributes().placeholder).toBe(placeholder);
});
});
There are several ways to test the placeholder of an input component in VueJS, but a common one is using testing tools like Jest or Mocha to write unit tests for the component.
To test the placeholder of an input component, you can do the following:
- Create a test file, usually with the
.spec.jsextension, for the input component. - Import the input component and Vue Test Utils (or another testing utility library) into the test file.
- Create a component wrapper instance, passing in the props the component needs, including the placeholder.
- Use the wrapper's methods to check that the placeholder is being rendered correctly, for example:
expect(wrapper.find('input').attributes().placeholder).toBe(placeholder) - Run the tests using the corresponding Jest or Mocha command.
- Check whether the test passed or failed, and make the necessary corrections to make sure the component is working correctly.
It's important to remember to always test components in isolation, to make sure the component is working correctly and not affecting other components in your project.