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. import {semverCompare} from './versions';
  4. function testVersion(v1: string, operator: '<' | '>' | '=', v2: string) {
  5. const result = semverCompare(v1, v2);
  6. if (operator === '<') {
  7. expect(result).toBe(-1);
  8. }
  9. if (operator === '>') {
  10. expect(result).toBe(1);
  11. }
  12. if (operator === '=') {
  13. expect(result).toBe(0);
  14. }
  15. }
  16. describe('semverCompar', () => {
  17. it('compares versions', () => {
  18. // 1.0.0 < 2.0.0 < 2.1.0 < 2.1.1
  19. testVersion('1.0.0', '=', '1.0.0');
  20. testVersion('1.0.0', '<', '2.0.0');
  21. testVersion('2.0.0', '<', '2.1.0');
  22. testVersion('2.1.0', '<', '2.1.1');
  23. // 1.0.0-alpha < 1.0.0
  24. testVersion('1.0.0-alpha', '<', '1.0.0');
  25. // 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
  26. testVersion('1.0.0-alpha', '<', '1.0.0-alpha.1');
  27. testVersion('1.0.0-alpha.1', '<', '1.0.0-alpha.beta');
  28. testVersion('1.0.0-alpha.beta', '<', '1.0.0-beta');
  29. testVersion('1.0.0-beta', '<', '1.0.0-beta.2');
  30. testVersion('1.0.0-beta.2', '<', '1.0.0-beta.11');
  31. testVersion('1.0.0-beta.11', '<', '1.0.0-rc.1');
  32. testVersion('1.0.0-rc.1', '<', '1.0.0');
  33. });
  34. });