You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { compareVersion, getLatestVersion, isEqualOrLaterThanVersion } from './semver'
  2. describe('semver utilities', () => {
  3. describe('getLatestVersion', () => {
  4. it('should return the latest version from a list of versions', () => {
  5. expect(getLatestVersion(['1.0.0', '1.1.0', '1.0.1'])).toBe('1.1.0')
  6. expect(getLatestVersion(['2.0.0', '1.9.9', '1.10.0'])).toBe('2.0.0')
  7. expect(getLatestVersion(['1.0.0-alpha', '1.0.0-beta', '1.0.0'])).toBe('1.0.0')
  8. })
  9. it('should handle patch versions correctly', () => {
  10. expect(getLatestVersion(['1.0.1', '1.0.2', '1.0.0'])).toBe('1.0.2')
  11. expect(getLatestVersion(['1.0.10', '1.0.9', '1.0.11'])).toBe('1.0.11')
  12. })
  13. it('should handle mixed version formats', () => {
  14. expect(getLatestVersion(['v1.0.0', '1.1.0', 'v1.2.0'])).toBe('v1.2.0')
  15. expect(getLatestVersion(['1.0.0-rc.1', '1.0.0', '1.0.0-beta'])).toBe('1.0.0')
  16. })
  17. it('should return the only version if only one version is provided', () => {
  18. expect(getLatestVersion(['1.0.0'])).toBe('1.0.0')
  19. })
  20. })
  21. describe('compareVersion', () => {
  22. it('should return 1 when first version is greater', () => {
  23. expect(compareVersion('1.1.0', '1.0.0')).toBe(1)
  24. expect(compareVersion('2.0.0', '1.9.9')).toBe(1)
  25. expect(compareVersion('1.0.1', '1.0.0')).toBe(1)
  26. })
  27. it('should return -1 when first version is less', () => {
  28. expect(compareVersion('1.0.0', '1.1.0')).toBe(-1)
  29. expect(compareVersion('1.9.9', '2.0.0')).toBe(-1)
  30. expect(compareVersion('1.0.0', '1.0.1')).toBe(-1)
  31. })
  32. it('should return 0 when versions are equal', () => {
  33. expect(compareVersion('1.0.0', '1.0.0')).toBe(0)
  34. expect(compareVersion('2.1.3', '2.1.3')).toBe(0)
  35. })
  36. it('should handle pre-release versions correctly', () => {
  37. expect(compareVersion('1.0.0-beta', '1.0.0-alpha')).toBe(1)
  38. expect(compareVersion('1.0.0', '1.0.0-beta')).toBe(1)
  39. expect(compareVersion('1.0.0-alpha', '1.0.0-beta')).toBe(-1)
  40. })
  41. })
  42. describe('isEqualOrLaterThanVersion', () => {
  43. it('should return true when baseVersion is greater than targetVersion', () => {
  44. expect(isEqualOrLaterThanVersion('1.1.0', '1.0.0')).toBe(true)
  45. expect(isEqualOrLaterThanVersion('2.0.0', '1.9.9')).toBe(true)
  46. expect(isEqualOrLaterThanVersion('1.0.1', '1.0.0')).toBe(true)
  47. })
  48. it('should return true when baseVersion is equal to targetVersion', () => {
  49. expect(isEqualOrLaterThanVersion('1.0.0', '1.0.0')).toBe(true)
  50. expect(isEqualOrLaterThanVersion('2.1.3', '2.1.3')).toBe(true)
  51. })
  52. it('should return false when baseVersion is less than targetVersion', () => {
  53. expect(isEqualOrLaterThanVersion('1.0.0', '1.1.0')).toBe(false)
  54. expect(isEqualOrLaterThanVersion('1.9.9', '2.0.0')).toBe(false)
  55. expect(isEqualOrLaterThanVersion('1.0.0', '1.0.1')).toBe(false)
  56. })
  57. it('should handle pre-release versions correctly', () => {
  58. expect(isEqualOrLaterThanVersion('1.0.0', '1.0.0-beta')).toBe(true)
  59. expect(isEqualOrLaterThanVersion('1.0.0-beta', '1.0.0-alpha')).toBe(true)
  60. expect(isEqualOrLaterThanVersion('1.0.0-alpha', '1.0.0')).toBe(false)
  61. })
  62. })
  63. })