versions.spec.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /* Taken from https://gist.github.com/iwill/a83038623ba4fef6abb9efca87ae9ccb
  2. * returns -1 for smaller, 0 for equals, and 1 for greater than
  3. */
  4. import {semverCompare} from './versions';
  5. function testVersion(v1: string, operator: '<' | '>' | '=', v2: string) {
  6. const result = semverCompare(v1, v2);
  7. if (operator === '<') {
  8. expect(result).toBe(-1);
  9. }
  10. if (operator === '>') {
  11. expect(result).toBe(1);
  12. }
  13. if (operator === '=') {
  14. expect(result).toBe(0);
  15. }
  16. }
  17. describe('semverCompar', () => {
  18. it('compares versions', () => {
  19. // 1.0.0 < 2.0.0 < 2.1.0 < 2.1.1
  20. testVersion('1.0.0', '=', '1.0.0');
  21. testVersion('1.0.0', '<', '2.0.0');
  22. testVersion('2.0.0', '<', '2.1.0');
  23. testVersion('2.1.0', '<', '2.1.1');
  24. // 1.0.0-alpha < 1.0.0
  25. testVersion('1.0.0-alpha', '<', '1.0.0');
  26. // 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0
  27. testVersion('1.0.0-alpha', '<', '1.0.0-alpha.1');
  28. testVersion('1.0.0-alpha.1', '<', '1.0.0-alpha.beta');
  29. testVersion('1.0.0-alpha.beta', '<', '1.0.0-beta');
  30. testVersion('1.0.0-beta', '<', '1.0.0-beta.2');
  31. testVersion('1.0.0-beta.2', '<', '1.0.0-beta.11');
  32. testVersion('1.0.0-beta.11', '<', '1.0.0-rc.1');
  33. testVersion('1.0.0-rc.1', '<', '1.0.0');
  34. });
  35. });