Skip to content

Commit

Permalink
Merge pull request #37 from snyk/fix/jestify
Browse files Browse the repository at this point in the history
fix: jestify the tests
  • Loading branch information
orsagie committed Dec 2, 2020
2 parents d4de163 + 402f5c2 commit fb0ec7a
Show file tree
Hide file tree
Showing 50 changed files with 718 additions and 734 deletions.
8 changes: 8 additions & 0 deletions jest.config.js
@@ -0,0 +1,8 @@
// remove the noise from express server logs
process.env.LOG_LEVEL="fatal";

module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};

10 changes: 4 additions & 6 deletions package.json
Expand Up @@ -9,18 +9,19 @@
"prepare": "npm run build",
"build": "tsc",
"lint": "eslint 'lib/**/*.?s'",
"test": "ava --verbose"
"test": "jest \"test/.*\\.test\\.ts\""
},
"author": "Gareth Visagie <gareth@snyk.io>",
"license": "Apache-2.0",
"devDependencies": {
"@ava/babel": "^1.0.1",
"@types/jest": "^25.0.0",
"@typescript-eslint/eslint-plugin": "^3.5.0",
"@typescript-eslint/parser": "^3.5.0",
"ava": "^3.9.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.11.0",
"jest": "^25.0.0",
"prettier": "^2.0.5",
"ts-jest": "^25.0.0",
"typescript": "~3.8.3"
},
"repository": {
Expand All @@ -30,9 +31,6 @@
"engines": {
"node": ">=8"
},
"ava": {
"babel": true
},
"dependencies": {
"lodash.escaperegexp": "^4.1.0",
"lodash.flatten": "^4.4.0",
Expand Down
68 changes: 0 additions & 68 deletions test/comparison/cmp.test.js

This file was deleted.

62 changes: 62 additions & 0 deletions test/comparison/cmp.test.ts
@@ -0,0 +1,62 @@
import { cmp } from '../../';

// cmp(v1, comparator, v2): Pass in a comparison string, and it'll call the
// corresponding function above. "===" and "!==" do simple string comparison,
// but are included for completeness.
// Throws if an invalid comparison string is provided.

describe('test cmp', () => {
it('cmp(v1, ">", v2)', () => {
expect(cmp('2', '>', '1')).toBeTruthy();
expect(cmp('2', '>', '2')).toBeFalsy();
expect(cmp('1', '>', '2')).toBeFalsy();
});

it('cmp(v1, ">=", v2)', () => {
expect(cmp('2', '>=', '1')).toBeTruthy();
expect(cmp('2', '>=', '2')).toBeTruthy();
expect(cmp('1', '>=', '2')).toBeFalsy();
});

it('cmp(v1, "<", v2)', () => {
expect(cmp('1', '<', '2')).toBeTruthy();
expect(cmp('2', '<', '2')).toBeFalsy();
expect(cmp('2', '<', '1')).toBeFalsy();
});

it('cmp(v1, "<=", v2)', () => {
expect(cmp('1', '<=', '2')).toBeTruthy();
expect(cmp('2', '<=', '2')).toBeTruthy();
expect(cmp('2', '<=', '1')).toBeFalsy();
});

it('cmp(v1, "==", v2)', () => {
expect(cmp('2', '==', '2')).toBeTruthy();
expect(cmp('2', '==', '2.0')).toBeTruthy();
expect(cmp('2', '==', '1')).toBeFalsy();
});

it('cmp(v1, "!=", v2)', () => {
expect(cmp('2', '!=', '1')).toBeTruthy();
expect(cmp('2', '!=', '2')).toBeFalsy();
expect(cmp('2', '!=', '2.0')).toBeFalsy();
});

it('cmp(v1, "===", v2)', () => {
expect(cmp('2', '===', '2')).toBeTruthy();
expect(cmp('2', '===', '1')).toBeFalsy();
expect(cmp('2', '===', '2.0')).toBeFalsy();
});

it('cmp(v1, "!==", v2)', () => {
expect(cmp('2', '!==', '2')).toBeFalsy();
expect(cmp('2', '!==', '2.0')).toBeTruthy();
expect(cmp('2', '!==', '1')).toBeTruthy();
});

it('cmp(v1, "nonsense", v2)', () => {
expect(() => {cmp('2', 'nonsense', '2')}).toThrow(new Error('Invalid comparator: nonsense'));
expect(() => {cmp('2', '!====', '2')}).toThrow(new Error('Invalid comparator: !===='));
expect(() => {cmp('2', '>broken', '2')}).toThrow(new Error('Invalid comparator: >broken'));
});
});
33 changes: 0 additions & 33 deletions test/comparison/compare.test.js

This file was deleted.

33 changes: 33 additions & 0 deletions test/comparison/compare.test.ts
@@ -0,0 +1,33 @@
import { compare } from '../../';

// compare(v1, v2): Return 0 if v1 == v2, or 1 if v1 is greater, or -1 if v2 is
// greater. Sorts in ascending order if passed to Array.sort().

describe('test compare', () => {
it('compare(v1, v2): 0 if v1 == v2', () => {
expect(compare('1', '1')).toBe(0);
expect(compare('1.1', '1.1')).toBe(0);
expect(compare('1.1.0', '1.1.0')).toBe(0);
expect(compare('1.1.0.1', '1.1.0.1')).toBe(0);
expect(compare('1.1.0.1-alpha', '1.1.0.1-alpha')).toBe(0);
expect(compare('1.1.0.1-alpha.2', '1.1.0.1-alpha.2')).toBe(0);
});

it('compare(v1, v2): 1 if v1 > v2', () => {
expect(compare('2', '1')).toBe(1);
expect(compare('1.2', '1.1')).toBe(1);
expect(compare('1.1.1', '1.1.0')).toBe(1);
expect(compare('1.1.0.2', '1.1.0.1')).toBe(1);
expect(compare('1.1.0.1-beta', '1.1.0.1-alpha')).toBe(1);
expect(compare('1.1.0.1-alpha.3', '1.1.0.1-alpha.2')).toBe(1);
});

it('compare(v1, v2): -1 if v1 < v2', () => {
expect(compare('1', '2')).toBe(-1);
expect(compare('1.1', '1.2')).toBe(-1);
expect(compare('1.1.0', '1.1.1')).toBe(-1);
expect(compare('1.1.0.1', '1.1.0.2')).toBe(-1);
expect(compare('1.1.0.1-alpha', '1.1.0.1-beta')).toBe(-1);
expect(compare('1.1.0.1-alpha.2', '1.1.0.1-alpha.3')).toBe(-1);
})
});
72 changes: 0 additions & 72 deletions test/comparison/diff.test.js

This file was deleted.

73 changes: 73 additions & 0 deletions test/comparison/diff.test.ts
@@ -0,0 +1,73 @@
const semver = require('../../');
const diff = semver.diff;

// Not implemented as we don't use it.

// diff(v1, v2): Returns difference between two versions by the release type
// (major, premajor, minor, preminor, patch, prepatch, or prerelease), or null
// if the versions are the same.

describe('test diff', () => {

it('diff(v1, v2): same versions', () => {
expect(diff('1', '1')).toBe(null);
expect(diff('1.1', '1.1')).toBe(null);
expect(diff('1.1.2', '1.1.2')).toBe(null);
expect(diff('1.1.1.1', '1.1.1.1')).toBe(null);
expect(diff('1.0.0.alpha.1', '1.0.0.alpha.1')).toBe(null);
expect(diff('1.1.2-1', '1.1.2.pre.1')).toBe(null);
expect(diff('1.1.2.pre.1', '1.1.2-1')).toBe(null);
expect(diff('2', '2.0')).toBe(null);
expect(diff('2.0', '2')).toBe(null);
expect(diff('2', '2.0.0')).toBe(null);
expect(diff('2.0.0', '2')).toBe(null);
expect(diff('2', '2.0.0.0')).toBe(null);
expect(diff('2.0.0.0', '2')).toBe(null);
});

it('diff(v1, v2): major versions', () => {
expect(diff('1', '3')).toBe('major');
expect(diff('1.1', '3.1')).toBe('major');
expect(diff('1.1.2', '3.0.0')).toBe('major');
expect(diff('1.1.2', '2.0.0')).toBe('major');
expect(diff('1.1.1.1', '2.0.0')).toBe('major');
});

it('diff(v1, v2): minor versions', () => {
expect(diff('1.1', '1.2')).toBe('minor');
expect(diff('1.1.2', '1.2.1.1')).toBe('minor');
expect(diff('1.1.2', '1.2.0')).toBe('minor');
expect(diff('1.1.2.1', '1.2.0')).toBe('minor');
expect(diff('1.1.2.1', '1.2.0.1')).toBe('minor');
});

it('diff(v1, v2): patch versions', () => {
expect(diff('1.1.2', '1.1.3')).toBe('patch');
expect(diff('1.1.2', '1.1.2.1')).toBe('patch');
expect(diff('1.1.2.1', '1.1.3')).toBe('patch');
expect(diff('1.1.2.1', '1.1.3.2.1')).toBe('patch');
expect(diff('1.1.2.1', '1.1.2.1.1.1.2')).toBe('patch');
expect(diff('1.1.2.1.1.1.1', '1.1.2.1.1.1.2')).toBe('patch');
});

it('diff(v1, v2): premajor versions', () => {
expect(diff('1.0.0.alpha.1', '2.0.0')).toBe('premajor');
});

it('diff(v1, v2): preminor versions', () => {
expect(diff('1.1.2.alpha.1', '1.2.0')).toBe('preminor');
});

it('diff(v1, v2): prepatch versions', () => {
expect(diff('1.1.2.alpha.1', '1.1.3')).toBe('prepatch');
expect(diff('1.1.2.3.alpha.1', '1.1.2.alpha.2')).toBe('prepatch');
expect(diff('1.1.2.3.alpha.1', '1.1.2.4.alpha.2')).toBe('prepatch');
expect(diff('1.1.2.alpha.1', '1.1.2.1')).toBe('prepatch');
});

it('diff(v1, v2): prerelease versions', () => {
expect(diff('1.1.2.alpha.1', '1.1.2.alpha.2')).toBe('prerelease');
expect(diff('1.1.2.3.alpha.1', '1.1.2.3.alpha.2')).toBe('prerelease');
expect(diff('1.alpha.1', '1')).toBe('prerelease');
})
});

0 comments on commit fb0ec7a

Please sign in to comment.