Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@vue/cli-plugin-unit-jest

Package Overview
Dependencies
Maintainers
3
Versions
159
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vue/cli-plugin-unit-jest

unit-jest plugin for vue-cli

  • 5.0.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
258K
increased by1.01%
Maintainers
3
Weekly downloads
 
Created

What is @vue/cli-plugin-unit-jest?

@vue/cli-plugin-unit-jest is a plugin for Vue CLI that adds support for unit testing Vue components using Jest. It provides a seamless integration with Vue CLI projects, allowing developers to write and run unit tests with ease.

What are @vue/cli-plugin-unit-jest's main functionalities?

Unit Testing Vue Components

This feature allows you to write unit tests for your Vue components using Jest. The provided code sample shows how to configure Jest with Vue CLI and write a simple test for a Vue component.

module.exports = {
  preset: '@vue/cli-plugin-unit-jest',
  testMatch: ['**/tests/unit/**/*.spec.js'],
};

// Example test file: tests/unit/HelloWorld.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);
  });
});

Snapshot Testing

Snapshot testing allows you to capture the rendered output of a Vue component and compare it to a saved snapshot. This helps in ensuring that the UI does not change unexpectedly.

import { shallowMount } from '@vue/test-utils';
import HelloWorld from '@/components/HelloWorld.vue';

describe('HelloWorld.vue', () => {
  it('matches the snapshot', () => {
    const wrapper = shallowMount(HelloWorld, {
      propsData: { msg: 'Hello World' }
    });
    expect(wrapper.html()).toMatchSnapshot();
  });
});

Mocking Dependencies

This feature allows you to mock dependencies like API calls in your unit tests. The provided code sample shows how to mock an axios GET request and test a Vue component that fetches data from an API.

import { shallowMount } from '@vue/test-utils';
import axios from 'axios';
import MyComponent from '@/components/MyComponent.vue';

jest.mock('axios');

describe('MyComponent.vue', () => {
  it('fetches data from API', async () => {
    const data = { data: 'some data' };
    axios.get.mockResolvedValue(data);
    const wrapper = shallowMount(MyComponent);
    await wrapper.vm.fetchData();
    expect(wrapper.vm.data).toBe('some data');
  });
});

Other packages similar to @vue/cli-plugin-unit-jest

Keywords

FAQs

Package last updated on 07 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc