Skip to content

Commit

Permalink
Use jest for tests (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored and siimon committed Jun 18, 2017
1 parent 84acbb3 commit 8b64435
Show file tree
Hide file tree
Showing 20 changed files with 528 additions and 550 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
node_modules
.idea/
*.log
coverage/
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"scripts": {
"test": "npm run lint && npm run compile-typescript && npm run test-unit",
"lint": "node-version-gte-4 && eslint . || node-version-lt-4",
"test-unit": "mocha --recursive test/",
"test-unit": "jest",
"compile-typescript": "tsc index.d.ts --noImplicitAny"
},
"repository": {
Expand All @@ -30,11 +30,9 @@
"license": "Apache-2.0",
"homepage": "https://github.com/siimon/prom-client",
"devDependencies": {
"chai": "^3.4.1",
"eslint": "^3.5.0",
"express": "^4.13.3",
"mocha": "^3.2.0",
"mockery": "^2.0.0",
"jest": "^20.0.4",
"nock": "^9.0.11",
"node-version-check": "^2.1.1",
"sinon": "^2.0.0",
Expand All @@ -44,5 +42,9 @@
"tdigest": "^0.1.1",
"util-extend": "^1.0.1"
},
"types": "./index.d.ts"
"types": "./index.d.ts",
"jest": {
"testEnvironment": "node",
"testRegex": ".*Test\\.js$"
}
}
2 changes: 1 addition & 1 deletion test/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"env": {
"mocha": true
"jest": true
},
"rules": {
"no-console": 0,
Expand Down
28 changes: 13 additions & 15 deletions test/bucketGeneratorsTest.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

describe('bucketGenerators', function() {
var mocha = require('mocha');
var expect = require('chai').expect;
var linearBuckets = require('../index').linearBuckets;
var exponentialBuckets = require('../index').exponentialBuckets;

Expand All @@ -13,19 +11,19 @@ describe('bucketGenerators', function() {
});

it('should start on 0', function() {
expect(result[0]).to.equal(0);
expect(result[0]).toEqual(0);
});
it('should return 10 buckets', function() {
expect(result).to.have.length(10);
expect(result).toHaveLength(10);
});
it('should have width 50 between buckets', function() {
expect(result[1] - result[0]).to.equal(50);
expect(result[9] - result[8]).to.equal(50);
expect(result[4] - result[3]).to.equal(50);
expect(result[1] - result[0]).toEqual(50);
expect(result[9] - result[8]).toEqual(50);
expect(result[4] - result[3]).toEqual(50);
});
it('should not allow negative count', function() {
var fn = function() { linearBuckets(2, 1, 0); };
expect(fn).to.throw(Error);
expect(fn).toThrowError(Error);
});
});

Expand All @@ -35,27 +33,27 @@ describe('bucketGenerators', function() {
});

it('should start at start value', function() {
expect(result[0]).to.equal(1);
expect(result[0]).toEqual(1);
});
it('should return 5 items', function() {
expect(result).to.have.length(5);
expect(result).toHaveLength(5);
});
it('should increment with a factor of 2', function() {
expect(result[1] / result[0]).to.equal(2);
expect(result[3] / result[2]).to.equal(2);
expect(result[1] / result[0]).toEqual(2);
expect(result[3] / result[2]).toEqual(2);
});

it('should not allow factor of equal or less than 1', function() {
var fn = function() { exponentialBuckets(1, 1, 5); };
expect(fn).to.throw(Error);
expect(fn).toThrowError(Error);
});
it('should not allow negative start', function() {
var fn = function() { exponentialBuckets(0, 1, 5); };
expect(fn).to.throw(Error);
expect(fn).toThrowError(Error);
});
it('should not allow negative count', function() {
var fn = function() { exponentialBuckets(2, 10, 0); };
expect(fn).to.throw(Error);
expect(fn).toThrowError(Error);
});
});
});
77 changes: 38 additions & 39 deletions test/counterTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ describe('counter', function() {
var Counter = require('../index').Counter;
var Registry = require('../index').Registry;
var globalRegistry = require('../index').register;
var expect = require('chai').expect;
var instance;

describe('global registry', function() {
Expand All @@ -19,40 +18,40 @@ describe('counter', function() {
});
it('should increment counter', function() {
instance.inc();
expect(instance.get().values[0].value).to.equal(1);
expect(instance.get().values[0].timestamp).to.equal(undefined);
expect(instance.get().values[0].value).toEqual(1);
expect(instance.get().values[0].timestamp).toEqual(undefined);
});
it('should increment with a provided value', function() {
instance.inc(100);
expect(instance.get().values[0].value).to.equal(100);
expect(instance.get().values[0].timestamp).to.equal(undefined);
expect(instance.get().values[0].value).toEqual(100);
expect(instance.get().values[0].timestamp).toEqual(undefined);
});
it('should increment with a provided value and timestamp', function() {
instance.inc(100, 1485392700000);
expect(instance.get().values[0].value).to.equal(100);
expect(instance.get().values[0].timestamp).to.equal(1485392700000);
expect(instance.get().values[0].value).toEqual(100);
expect(instance.get().values[0].timestamp).toEqual(1485392700000);
});
it('should not allow non number as timestamp', function() {
var fn = function() {
instance.inc(1, 'blah');
};
expect(fn).to.throw(Error);
expect(fn).toThrowError(Error);
});
it('should not allow invalid date as timestamp', function() {
var fn = function() {
instance.inc(1, new Date('blah'));
};
expect(fn).to.throw(Error);
expect(fn).toThrowError(Error);
});
it('should not be possible to decrease a counter', function() {
var fn = function() {
instance.inc(-100);
};
expect(fn).to.throw(Error);
expect(fn).toThrowError(Error);
});
it('should handle incrementing with 0', function() {
instance.inc(0);
expect(instance.get().values[0].value).to.equal(0);
expect(instance.get().values[0].value).toEqual(0);
});

describe('labels', function() {
Expand All @@ -62,36 +61,36 @@ describe('counter', function() {

it('should increment counter', function() {
instance.inc();
expect(instance.get().values[0].value).to.equal(1);
expect(instance.get().values[0].timestamp).to.equal(undefined);
expect(instance.get().values[0].value).toEqual(1);
expect(instance.get().values[0].timestamp).toEqual(undefined);
});
it('should handle 1 value per label', function() {
instance.labels('GET', '/test').inc();
instance.labels('POST', '/test').inc();

var values = instance.get().values;
expect(values).to.have.length(2);
expect(values).toHaveLength(2);
});

it('should handle labels which are provided as arguments to inc()', function() {
instance.inc({method: 'GET', endpoint: '/test'});
instance.inc({method: 'POST', endpoint: '/test'});

var values = instance.get().values;
expect(values).to.have.length(2);
expect(values).toHaveLength(2);
});

it('should throw error if label lengths does not match', function() {
var fn = function() {
instance.labels('GET').inc();
};
expect(fn).to.throw(Error);
expect(fn).toThrowError(Error);
});

it('should increment label value with provided value', function() {
instance.labels('GET', '/test').inc(100);
var values = instance.get().values;
expect(values[0].value).to.equal(100);
expect(values[0].value).toEqual(100);
});
});
});
Expand All @@ -107,40 +106,40 @@ describe('counter', function() {

it('should increment counter', function() {
instance.inc();
expect(instance.get().values[0].value).to.equal(1);
expect(instance.get().values[0].timestamp).to.equal(undefined);
expect(instance.get().values[0].value).toEqual(1);
expect(instance.get().values[0].timestamp).toEqual(undefined);
});
it('should increment with a provided value', function() {
instance.inc(100);
expect(instance.get().values[0].value).to.equal(100);
expect(instance.get().values[0].timestamp).to.equal(undefined);
expect(instance.get().values[0].value).toEqual(100);
expect(instance.get().values[0].timestamp).toEqual(undefined);
});
it('should increment with a provided value and timestamp', function() {
instance.inc(100, 1485392700000);
expect(instance.get().values[0].value).to.equal(100);
expect(instance.get().values[0].timestamp).to.equal(1485392700000);
expect(instance.get().values[0].value).toEqual(100);
expect(instance.get().values[0].timestamp).toEqual(1485392700000);
});
it('should not allow non number as timestamp', function() {
var fn = function() {
instance.inc(1, 'blah');
};
expect(fn).to.throw(Error);
expect(fn).toThrowError(Error);
});
it('should not allow invalid date as timestamp', function() {
var fn = function() {
instance.inc(1, new Date('blah'));
};
expect(fn).to.throw(Error);
expect(fn).toThrowError(Error);
});
it('should not be possible to decrease a counter', function() {
var fn = function() {
instance.inc(-100);
};
expect(fn).to.throw(Error);
expect(fn).toThrowError(Error);
});
it('should handle incrementing with 0', function() {
instance.inc(0);
expect(instance.get().values[0].value).to.equal(0);
expect(instance.get().values[0].value).toEqual(0);
});

describe('labels', function() {
Expand All @@ -153,35 +152,35 @@ describe('counter', function() {
instance.labels('POST', '/test').inc();

var values = instance.get().values;
expect(values).to.have.length(2);
expect(values).toHaveLength(2);
});

it('should handle labels which are provided as arguments to inc()', function() {
instance.inc({method: 'GET', endpoint: '/test'});
instance.inc({method: 'POST', endpoint: '/test'});

var values = instance.get().values;
expect(values).to.have.length(2);
expect(values).toHaveLength(2);
});

it('should throw error if label lengths does not match', function() {
var fn = function() {
instance.labels('GET').inc();
};
expect(fn).to.throw(Error);
expect(fn).toThrowError(Error);
});

it('should throw error if label lengths does not match', function() {
var fn = function() {
instance.labels('GET').inc();
};
expect(fn).to.throw(Error);
expect(fn).toThrowError(Error);
});

it('should increment label value with provided value', function() {
instance.labels('GET', '/test').inc(100);
var values = instance.get().values;
expect(values[0].value).to.equal(100);
expect(values[0].value).toEqual(100);
});
});
});
Expand All @@ -191,9 +190,9 @@ describe('counter', function() {
});
it('should increment counter', function() {
instance.inc();
expect(globalRegistry.getMetricsAsJSON().length).to.equal(0);
expect(instance.get().values[0].value).to.equal(1);
expect(instance.get().values[0].timestamp).to.equal(undefined);
expect(globalRegistry.getMetricsAsJSON().length).toEqual(0);
expect(instance.get().values[0].value).toEqual(1);
expect(instance.get().values[0].timestamp).toEqual(undefined);
});
});
describe('registry instance', function() {
Expand All @@ -204,10 +203,10 @@ describe('counter', function() {
});
it('should increment counter', function() {
instance.inc();
expect(globalRegistry.getMetricsAsJSON().length).to.equal(0);
expect(registryInstance.getMetricsAsJSON().length).to.equal(1);
expect(instance.get().values[0].value).to.equal(1);
expect(instance.get().values[0].timestamp).to.equal(undefined);
expect(globalRegistry.getMetricsAsJSON().length).toEqual(0);
expect(registryInstance.getMetricsAsJSON().length).toEqual(1);
expect(instance.get().values[0].value).toEqual(1);
expect(instance.get().values[0].timestamp).toEqual(undefined);
});
});
});
16 changes: 7 additions & 9 deletions test/defaultMetrics.js → test/defaultMetricsTest.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict';

describe('collectDefaultMetrics', function() {
var expect = require('chai').expect;
var register = require('../index').register;
var collectDefaultMetrics = require('../index').collectDefaultMetrics;
var platform;
var cpuUsage;
var interval;

before(function() {
beforeAll(function() {
platform = process.platform;
cpuUsage = process.cpuUsage;

Expand All @@ -31,7 +30,7 @@ describe('collectDefaultMetrics', function() {
register.clear();
});

after(function() {
afterAll(function() {
Object.defineProperty(process, 'platform', {
value: platform
});
Expand All @@ -51,16 +50,16 @@ describe('collectDefaultMetrics', function() {
});

it('should add metrics to the registry', function() {
expect(register.getMetricsAsJSON()).to.have.length(0);
expect(register.getMetricsAsJSON()).toHaveLength(0);
interval = collectDefaultMetrics();
expect(register.getMetricsAsJSON()).to.not.have.length(0);
expect(register.getMetricsAsJSON()).not.toHaveLength(0);
});

it('should allow blacklisting all metrics', function() {
expect(register.getMetricsAsJSON()).to.have.length(0);
expect(register.getMetricsAsJSON()).toHaveLength(0);
clearInterval(collectDefaultMetrics());
register.clear();
expect(register.getMetricsAsJSON()).to.have.length(0);
expect(register.getMetricsAsJSON()).toHaveLength(0);
});


Expand All @@ -73,8 +72,7 @@ describe('collectDefaultMetrics', function() {
register.clear();
};

expect(fn).to.not.throw(Error);
expect(fn).not.toThrowError(Error);
});
});

});
Loading

0 comments on commit 8b64435

Please sign in to comment.