Back to Repositories

Testing PWA Feature Selection Module in Vue CLI

This test suite validates the Progressive Web App (PWA) prompt module functionality in Vue CLI. It ensures proper feature selection and plugin configuration during project setup using mock interactions and assertions.

Test Coverage Overview

The test coverage focuses on the PWA prompt module’s core functionality.

  • Verifies correct PWA feature selection in the CLI prompts
  • Tests plugin configuration generation
  • Validates expected options structure
  • Covers mock filesystem and user input scenarios

Implementation Analysis

The testing approach utilizes Jest’s mocking capabilities to isolate the prompt module functionality. It implements a structured test pattern using the Vue CLI test utilities for prompt validation.

  • Uses jest.mock() for filesystem and inquirer dependencies
  • Leverages assertPromptModule utility for standardized testing
  • Implements async/await pattern for prompt resolution

Technical Details

  • Testing Framework: Jest
  • Mock Utilities: fs and inquirer mocks
  • Test Helper: @vue/cli-test-utils/assertPromptModule
  • Configuration: Plugins-only mode enabled
  • Test Structure: Single test case with expected prompts and options

Best Practices Demonstrated

The test demonstrates clean and efficient testing practices for CLI modules.

  • Proper dependency isolation through mocking
  • Clear expected vs actual value comparison
  • Structured test data organization
  • Focused test scope with single responsibility
  • Effective use of helper utilities for reduced code duplication

vuejs/vue-cli

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

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

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

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

test('pwa', async () => {
  const expectedPrompts = [
    {
      message: 'features',
      choices: ['PWA'],
      check: [0]
    }
  ]

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

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