Back to Repositories

Testing Unit Testing Framework Selection in Vue CLI

This test suite validates the unit testing configuration prompts in Vue CLI, specifically testing the selection and integration of Jest and Mocha testing frameworks. It ensures proper plugin installation and configuration based on user choices.

Test Coverage Overview

The test suite provides comprehensive coverage of the unit testing prompt module functionality in Vue CLI. It validates:

  • Framework selection between Jest and Mocha
  • Correct plugin registration based on user choice
  • Expected prompt sequence and options
  • Plugin configuration accuracy

Implementation Analysis

The testing approach uses mock implementations for filesystem and inquirer interactions to isolate the prompt module functionality. It leverages the assertPromptModule utility to validate prompt sequences and resulting configurations.

  • Mock implementation of fs and inquirer dependencies
  • Structured test cases for each framework option
  • Assertion of expected plugin configurations

Technical Details

  • Testing Framework: Jest
  • Key Dependencies: @vue/cli-test-utils
  • Mocked Modules: fs, inquirer
  • Test Utilities: assertPromptModule
  • Configuration: Plugins-only mode testing

Best Practices Demonstrated

The test implementation showcases several testing best practices for CLI tooling. It demonstrates isolated testing through proper mocking, consistent test structure, and comprehensive validation of user interaction flows.

  • Isolated test environment through mocking
  • Consistent test case structure
  • Comprehensive prompt validation
  • Clear expected vs actual state comparison

vuejs/vue-cli

packages/@vue/cli/lib/promptModules/__tests__/unit.spec.js

            
jest.mock('fs')
jest.mock('inquirer')

const assertPromptModule = require('@vue/cli-test-utils/assertPromptModule')

const moduleToTest = require('../unit')

test('mocha', async () => {
  const expectedPrompts = [
    {
      message: 'features',
      choices: ['Unit Testing'],
      check: [0]
    },
    {
      message: 'Pick a unit testing solution',
      choices: ['Jest', 'Mocha'],
      choose: 0
    }
  ]

  const expectedOptions = {
    plugins: {
      '@vue/cli-plugin-unit-jest': {}
    }
  }

  await assertPromptModule(
    moduleToTest,
    expectedPrompts,
    expectedOptions,
    { pluginsOnly: true }
  )
})

test('jest', async () => {
  const expectedPrompts = [
    {
      message: 'features',
      choices: ['Unit Testing'],
      check: [0]
    },
    {
      message: 'Pick a unit testing solution',
      choices: ['Jest', 'Mocha'],
      choose: 1
    }
  ]

  const expectedOptions = {
    plugins: {
      '@vue/cli-plugin-unit-mocha': {}
    }
  }

  await assertPromptModule(
    moduleToTest,
    expectedPrompts,
    expectedOptions,
    { pluginsOnly: true }
  )
})