Back to Repositories

Testing Plugin Resolution Utilities in Vue CLI

This test suite validates Vue CLI plugin resolution utilities, focusing on plugin identification, official plugin detection, and plugin ID handling. The tests ensure proper functionality of core plugin naming conventions and resolution mechanisms in the Vue CLI ecosystem.

Test Coverage Overview

The test suite provides comprehensive coverage of plugin resolution utilities with 5 distinct test cases.

Key functionality tested includes:
  • Plugin identification validation
  • Official Vue CLI plugin detection
  • Plugin ID shortening
  • Full plugin ID resolution
  • Plugin ID matching across formats
Edge cases cover various plugin naming patterns including scoped packages and namespaced identifiers.

Implementation Analysis

The testing approach employs Jest’s expect assertions to verify plugin utility functions across multiple scenarios. The implementation uses systematic test patterns to validate each utility function with both valid and invalid inputs.

Technical patterns include:
  • Boolean validation for plugin detection
  • String transformation for ID resolution
  • Pattern matching for plugin identification
  • Scoped package handling

Technical Details

Testing tools and configuration:
  • Jest testing framework
  • Module-level imports for utility functions
  • Individual test blocks for each utility
  • Multiple assertions per test case
  • Direct function testing without mocking

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through clear organization and comprehensive coverage.

Notable practices include:
  • Isolated function testing
  • Consistent test case structure
  • Comprehensive edge case coverage
  • Clear test descriptions
  • Efficient test grouping

vuejs/vue-cli

packages/@vue/cli-shared-utils/__tests__/pluginResolution.spec.js

            
const {
  isPlugin,
  isOfficialPlugin,
  toShortPluginId,
  resolvePluginId,
  matchesPluginId
} = require('../lib/pluginResolution')

test('isPlugin', () => {
  expect(isPlugin('foobar')).toBe(false)
  expect(isPlugin('@vue/cli-plugin-foo')).toBe(true)
  expect(isPlugin('vue-cli-plugin-foo')).toBe(true)
  expect(isPlugin('@foo/vue-cli-plugin-foo')).toBe(true)
  expect(isPlugin('@foo.bar/vue-cli-plugin-foo')).toBe(true)
})

test('isOfficialPlugin', () => {
  expect(isOfficialPlugin('@vue/foo')).toBe(false)
  expect(isOfficialPlugin('@vue/cli-plugin-foo')).toBe(true)
  expect(isOfficialPlugin('vue-cli-plugin-foo')).toBe(false)
  expect(isOfficialPlugin('@foo/vue-cli-plugin-foo')).toBe(false)
  expect(isOfficialPlugin('@foo.bar/vue-cli-plugin-foo')).toBe(false)
})

test('toShortPluginId', () => {
  expect(toShortPluginId('@vue/cli-plugin-foo')).toBe('foo')
  expect(toShortPluginId('vue-cli-plugin-foo')).toBe('foo')
  expect(toShortPluginId('@foo/vue-cli-plugin-foo')).toBe('foo')
  expect(toShortPluginId('@foo.bar/vue-cli-plugin-foo')).toBe('foo')
})

test('resolvePluginId', () => {
  // already full
  expect(resolvePluginId('@vue/cli-plugin-foo')).toBe('@vue/cli-plugin-foo')
  expect(resolvePluginId('vue-cli-plugin-foo')).toBe('vue-cli-plugin-foo')
  expect(resolvePluginId('@foo/vue-cli-plugin-foo')).toBe('@foo/vue-cli-plugin-foo')
  expect(resolvePluginId('@foo.bar/vue-cli-plugin-foo')).toBe('@foo.bar/vue-cli-plugin-foo')

  // scoped short
  expect(resolvePluginId('@vue/foo')).toBe('@vue/cli-plugin-foo')
  expect(resolvePluginId('@foo/foo')).toBe('@foo/vue-cli-plugin-foo')
  expect(resolvePluginId('@foo.bar/foo')).toBe('@foo.bar/vue-cli-plugin-foo')

  // default short
  expect(resolvePluginId('foo')).toBe('vue-cli-plugin-foo')
})

test('matchesPluginId', () => {
  // full
  expect(matchesPluginId('@vue/cli-plugin-foo', '@vue/cli-plugin-foo')).toBe(true)
  expect(matchesPluginId('vue-cli-plugin-foo', 'vue-cli-plugin-foo')).toBe(true)
  expect(matchesPluginId('@foo/vue-cli-plugin-foo', '@foo/vue-cli-plugin-foo')).toBe(true)
  expect(matchesPluginId('@foo.bar/vue-cli-plugin-foo', '@foo.bar/vue-cli-plugin-foo')).toBe(true)

  // short without scope
  expect(matchesPluginId('foo', '@vue/cli-plugin-foo')).toBe(true)
  expect(matchesPluginId('foo', 'vue-cli-plugin-foo')).toBe(true)
  expect(matchesPluginId('foo', '@foo/vue-cli-plugin-foo')).toBe(true)
  expect(matchesPluginId('foo', '@foo.bar/vue-cli-plugin-foo')).toBe(true)

  // short with scope
  expect(matchesPluginId('@vue/foo', '@vue/cli-plugin-foo')).toBe(true)
  expect(matchesPluginId('@foo/foo', '@foo/vue-cli-plugin-foo')).toBe(true)
  expect(matchesPluginId('@foo.bar/foo', '@foo.bar/vue-cli-plugin-foo')).toBe(true)
})