migrate tests to Jest

This commit is contained in:
Alexander Zobnin
2017-10-30 19:59:28 +03:00
parent 89b5c9e135
commit e80a814880
26 changed files with 346 additions and 376 deletions

View File

@@ -35,6 +35,7 @@
"require": true,
"Chromath": false,
"setImmediate": true,
"jest": true,
"expect": true,
"it": true,
"describe": true,

View File

@@ -112,18 +112,6 @@ module.exports = function(grunt) {
}
},
mochaTest: {
test: {
options: {
reporter: 'spec'
},
src: [
'dist/test/datasource-zabbix/specs/test-main.js',
'dist/test/datasource-zabbix/specs/*_specs.js'
]
}
},
sass: {
options: {
sourceMap: true
@@ -178,8 +166,7 @@ module.exports = function(grunt) {
'copy',
'jshint',
'jscs',
'babel',
'mochaTest'
'babel'
]);
grunt.registerTask('watchTask', [
@@ -193,12 +180,11 @@ module.exports = function(grunt) {
'jscs'
]);
grunt.registerTask('test', [
grunt.registerTask('buildTest', [
'clean:test',
'clean:tmp',
'babel:distTestNoSystemJs',
'babel:distTestsSpecsNoSystemJs',
'mochaTest'
'babel:distTestsSpecsNoSystemJs'
]);
grunt.registerTask('bench', [

11
codecov.yml Normal file
View File

@@ -0,0 +1,11 @@
coverage:
precision: 2
round: down
range: "50...100"
status:
project: yes
patch: yes
changes: no
comment: false

View File

@@ -1,12 +1,10 @@
import _ from 'lodash';
import Q from "q";
import {Datasource} from "../module";
import {zabbixTemplateFormat} from "../datasource";
import Q from "q";
import sinon from 'sinon';
import _ from 'lodash';
describe('ZabbixDatasource', () => {
let ctx = {};
let defined = sinon.match.defined;
beforeEach(() => {
ctx.instanceSettings = {
@@ -55,7 +53,7 @@ describe('ZabbixDatasource', () => {
range: {from: 'now-6h', to: 'now'}
};
ctx.ds.query(options).then(result => {
expect(result.data).to.have.length(0);
expect(result.data.length).toBe(0);
done();
});
});
@@ -65,12 +63,13 @@ describe('ZabbixDatasource', () => {
_.forEach(ranges, range => {
ctx.options.range.from = range;
ctx.ds.queryNumericData = sinon.spy();
ctx.ds.queryNumericData = jest.fn();
ctx.ds.query(ctx.options);
// Check that useTrends options is true
expect(ctx.ds.queryNumericData)
.to.have.been.calledWith(defined, defined, true, sinon.match.any);
let callArgs = ctx.ds.queryNumericData.mock.calls[0];
expect(callArgs[2]).toBe(true);
ctx.ds.queryNumericData.mockClear();
});
done();
@@ -81,12 +80,13 @@ describe('ZabbixDatasource', () => {
_.forEach(ranges, range => {
ctx.options.range.from = range;
ctx.ds.queryNumericData = sinon.spy();
ctx.ds.queryNumericData = jest.fn();
ctx.ds.query(ctx.options);
// Check that useTrends options is false
expect(ctx.ds.queryNumericData)
.to.have.been.calledWith(defined, defined, false, sinon.match.any);
let callArgs = ctx.ds.queryNumericData.mock.calls[0];
expect(callArgs[2]).toBe(false);
ctx.ds.queryNumericData.mockClear();
});
done();
});
@@ -101,7 +101,7 @@ describe('ZabbixDatasource', () => {
};
let result = ctx.ds.replaceTemplateVars(target);
expect(result).to.equal(expectedResult);
expect(result).toBe(expectedResult);
done();
}
@@ -149,10 +149,10 @@ describe('ZabbixDatasource', () => {
beforeEach(() => {
ctx.ds.replaceTemplateVars = (str) => str;
ctx.ds.zabbix = {
getGroups: () => Q.when([]),
getHosts: () => Q.when([]),
getApps: () => Q.when([]),
getItems: () => Q.when([])
getGroups: jest.fn().mockReturnValue(Q.when([])),
getHosts: jest.fn().mockReturnValue(Q.when([])),
getApps: jest.fn().mockReturnValue(Q.when([])),
getItems: jest.fn().mockReturnValue(Q.when([]))
};
});
@@ -161,66 +161,62 @@ describe('ZabbixDatasource', () => {
{query: '*', expect: '/.*/'},
{query: '', expect: ''},
{query: 'Backend', expect: 'Backend'},
{query: 'Back*', expect: 'Back*'}
{query: 'Back*', expect: 'Back*'},
];
let getGroups = sinon.spy(ctx.ds.zabbix, 'getGroups');
for (const test of tests) {
ctx.ds.metricFindQuery(test.query);
expect(getGroups).to.have.been.calledWith(test.expect);
getGroups.reset();
expect(ctx.ds.zabbix.getGroups).toBeCalledWith(test.expect);
ctx.ds.zabbix.getGroups.mockClear();
}
done();
});
it('should return hosts', (done) => {
const tests = [
{query: '*.*', expect: '/.*/'},
{query: '.', expect: ''},
{query: 'Backend.*', expect: 'Backend'},
{query: 'Back*.', expect: 'Back*'}
{query: '*.*', expect: ['/.*/', '/.*/']},
{query: '.', expect: ['', '']},
{query: 'Backend.*', expect: ['Backend', '/.*/']},
{query: 'Back*.', expect: ['Back*', '']},
];
let getHosts = sinon.spy(ctx.ds.zabbix, 'getHosts');
for (const test of tests) {
ctx.ds.metricFindQuery(test.query);
expect(getHosts).to.have.been.calledWith(test.expect);
getHosts.reset();
expect(ctx.ds.zabbix.getHosts).toBeCalledWith(test.expect[0], test.expect[1]);
ctx.ds.zabbix.getHosts.mockClear();
}
done();
});
it('should return applications', (done) => {
const tests = [
{query: '*.*.*', expect: ['/.*/', '/.*/']},
{query: '.*.', expect: ['', '/.*/']},
{query: 'Backend.backend01.*', expect: ['Backend', 'backend01']},
{query: 'Back*.*.', expect: ['Back*', '/.*/']}
{query: '*.*.*', expect: ['/.*/', '/.*/', '/.*/']},
{query: '.*.', expect: ['', '/.*/', '']},
{query: 'Backend.backend01.*', expect: ['Backend', 'backend01', '/.*/']},
{query: 'Back*.*.', expect: ['Back*', '/.*/', '']}
];
let getApps = sinon.spy(ctx.ds.zabbix, 'getApps');
for (const test of tests) {
ctx.ds.metricFindQuery(test.query);
expect(getApps).to.have.been.calledWith(test.expect[0], test.expect[1]);
getApps.reset();
expect(ctx.ds.zabbix.getApps).toBeCalledWith(test.expect[0], test.expect[1], test.expect[2]);
ctx.ds.zabbix.getApps.mockClear();
}
done();
});
it('should return items', (done) => {
const tests = [
{query: '*.*.*.*', expect: ['/.*/', '/.*/', '']},
{query: '.*.*.*', expect: ['', '/.*/', '']},
{query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', '']},
{query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu']}
{query: '*.*.*.*', expect: ['/.*/', '/.*/', '', '/.*/']},
{query: '.*.*.*', expect: ['', '/.*/', '', '/.*/']},
{query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', '', '/.*/']},
{query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu', '/.*/']}
];
let getItems = sinon.spy(ctx.ds.zabbix, 'getItems');
for (const test of tests) {
ctx.ds.metricFindQuery(test.query);
expect(getItems)
.to.have.been.calledWith(test.expect[0], test.expect[1], test.expect[2]);
getItems.reset();
expect(ctx.ds.zabbix.getItems)
.toBeCalledWith(test.expect[0], test.expect[1], test.expect[2], test.expect[3]);
ctx.ds.zabbix.getItems.mockClear();
}
done();
});
@@ -228,9 +224,8 @@ describe('ZabbixDatasource', () => {
it('should invoke method with proper arguments', (done) => {
let query = '*.*';
let getHosts = sinon.spy(ctx.ds.zabbix, 'getHosts');
ctx.ds.metricFindQuery(query);
expect(getHosts).to.have.been.calledWith('/.*/');
expect(ctx.ds.zabbix.getHosts).toBeCalledWith('/.*/', '/.*/');
done();
});
});
@@ -277,8 +272,8 @@ describe('ZabbixDatasource', () => {
return ctx.ds.alertQuery(options)
.then(resp => {
expect(resp.thresholds.length).to.equal(1);
expect(resp.thresholds[0]).to.equal(100);
expect(resp.thresholds).toHaveLength(1);
expect(resp.thresholds[0]).toBe(100);
return resp;
});
});
@@ -295,8 +290,8 @@ describe('ZabbixDatasource', () => {
return ctx.ds.alertQuery(options)
.then(resp => {
expect(resp.thresholds.length).to.equal(1);
expect(resp.thresholds[0]).to.equal(100);
expect(resp.thresholds.length).toBe(1);
expect(resp.thresholds[0]).toBe(100);
return resp;
});
});
@@ -313,8 +308,8 @@ describe('ZabbixDatasource', () => {
return ctx.ds.alertQuery(options)
.then(resp => {
expect(resp.thresholds.length).to.equal(1);
expect(resp.thresholds[0]).to.equal(30);
expect(resp.thresholds.length).toBe(1);
expect(resp.thresholds[0]).toBe(30);
return resp;
});
});
@@ -331,8 +326,8 @@ describe('ZabbixDatasource', () => {
return ctx.ds.alertQuery(options)
.then(resp => {
expect(resp.thresholds.length).to.equal(1);
expect(resp.thresholds[0]).to.equal(50);
expect(resp.thresholds.length).toBe(1);
expect(resp.thresholds[0]).toBe(50);
return resp;
});
});

View File

@@ -1,50 +0,0 @@
// JSHint options
/* globals global: false */
import prunk from 'prunk';
import {JSDOM} from 'jsdom';
import chai from 'chai';
// import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import * as dateMath from './modules/datemath';
// Mock angular module
var angularMocks = {
module: function() {
return {
directive: function() {},
service: function() {},
factory: function() {}
};
}
};
var datemathMock = {
parse: dateMath.parse,
parseDateMath: dateMath.parseDateMath,
isValid: dateMath.isValid
};
// Mock Grafana modules that are not available outside of the core project
// Required for loading module.js
prunk.mock('./css/query-editor.css!', 'no css, dude.');
prunk.mock('app/plugins/sdk', {
QueryCtrl: null
});
prunk.mock('app/core/utils/datemath', datemathMock);
prunk.mock('app/core/table_model', {});
prunk.mock('angular', angularMocks);
prunk.mock('jquery', 'module not found');
// Required for loading angularjs
let dom = new JSDOM('<html><head><script></script></head><body></body></html>');
// Setup jsdom
global.window = dom.window;
global.document = global.window.document;
global.Node = window.Node;
// Setup Chai
chai.should();
chai.use(sinonChai);
global.assert = chai.assert;
global.expect = chai.expect;

View File

@@ -13,7 +13,7 @@ describe('timeseries processing functions', () => {
let expected = [[2, 1], [4, 2], [5, 3]];
let result = ts.sumSeries(series);
expect(result).to.eql(expected);
expect(result).toEqual(expected);
done();
});
@@ -27,7 +27,7 @@ describe('timeseries processing functions', () => {
let expected = [[1, 1], [4, 2], [5, 3]];
let result = ts.sumSeries(series);
expect(result).to.eql(expected);
expect(result).toEqual(expected);
done();
});
});

View File

@@ -26,7 +26,7 @@ describe('Utils', () => {
_.each(test_cases, test_case => {
let expandedName = utils.expandItemName(test_case.name, test_case.key);
expect(expandedName).to.equal(test_case.expected);
expect(expandedName).toBe(test_case.expected);
});
done();
});
@@ -57,7 +57,7 @@ describe('Utils', () => {
_.each(test_cases, test_case => {
let expandedName = utils.expandItemName(test_case.name, test_case.key);
expect(expandedName).to.equal(test_case.expected);
expect(expandedName).toBe(test_case.expected);
});
done();
});
@@ -83,7 +83,7 @@ describe('Utils', () => {
_.each(test_cases, test_case => {
let expandedName = utils.expandItemName(test_case.name, test_case.key);
expect(expandedName).to.equal(test_case.expected);
expect(expandedName).toBe(test_case.expected);
});
done();
});
@@ -106,7 +106,7 @@ describe('Utils', () => {
_.each(test_cases, test_case => {
let splitQuery = utils.splitTemplateQuery(test_case.query);
expect(splitQuery).to.eql(test_case.expected);
expect(splitQuery).toEqual(test_case.expected);
});
done();
});
@@ -133,7 +133,7 @@ describe('Utils', () => {
_.each(test_cases, test_case => {
let splitQuery = utils.splitTemplateQuery(test_case.query);
expect(splitQuery).to.eql(test_case.expected);
expect(splitQuery).toEqual(test_case.expected);
});
done();
});

1
dist/test-setup/cssStub.js vendored Normal file
View File

@@ -0,0 +1 @@
module.exports = {};

50
dist/test-setup/jest-setup.js vendored Normal file
View File

@@ -0,0 +1,50 @@
// JSHint options
/* globals global: false */
import {JSDOM} from 'jsdom';
// Mock Grafana modules that are not available outside of the core project
// Required for loading module.js
jest.mock('angular', () => {
return {
module: function() {
return {
directive: function() {},
service: function() {},
factory: function() {}
};
}
};
}, {virtual: true});
jest.mock('app/plugins/sdk', () => {
return {
QueryCtrl: null
};
}, {virtual: true});
jest.mock('app/core/utils/datemath', () => {
const datemath = require('./modules/datemath');
return {
parse: datemath.parse,
parseDateMath: datemath.parseDateMath,
isValid: datemath.isValid
};
}, {virtual: true});
jest.mock('app/core/table_model', () => {
return {};
}, {virtual: true});
jest.mock('./css/query-editor.css!', () => {
return "";
}, {virtual: true});
jest.mock('jquery', () => 'module not found', {virtual: true});
// Required for loading angularjs
let dom = new JSDOM('<html><head><script></script></head><body></body></html>');
// Setup jsdom
global.window = dom.window;
global.document = global.window.document;
global.Node = window.Node;

View File

@@ -1,26 +1,21 @@
"use strict";
var _module = require("../module");
var _datasource = require("../datasource");
var _q = require("q");
var _q2 = _interopRequireDefault(_q);
var _sinon = require("sinon");
var _sinon2 = _interopRequireDefault(_sinon);
var _lodash = require("lodash");
var _lodash2 = _interopRequireDefault(_lodash);
var _q = require("q");
var _q2 = _interopRequireDefault(_q);
var _module = require("../module");
var _datasource = require("../datasource");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
describe('ZabbixDatasource', function () {
var ctx = {};
var defined = _sinon2.default.match.defined;
beforeEach(function () {
ctx.instanceSettings = {
@@ -71,7 +66,7 @@ describe('ZabbixDatasource', function () {
range: { from: 'now-6h', to: 'now' }
};
ctx.ds.query(options).then(function (result) {
expect(result.data).to.have.length(0);
expect(result.data.length).toBe(0);
done();
});
});
@@ -81,11 +76,13 @@ describe('ZabbixDatasource', function () {
_lodash2.default.forEach(ranges, function (range) {
ctx.options.range.from = range;
ctx.ds.queryNumericData = _sinon2.default.spy();
ctx.ds.queryNumericData = jest.fn();
ctx.ds.query(ctx.options);
// Check that useTrends options is true
expect(ctx.ds.queryNumericData).to.have.been.calledWith(defined, defined, true, _sinon2.default.match.any);
var callArgs = ctx.ds.queryNumericData.mock.calls[0];
expect(callArgs[2]).toBe(true);
ctx.ds.queryNumericData.mockClear();
});
done();
@@ -96,11 +93,13 @@ describe('ZabbixDatasource', function () {
_lodash2.default.forEach(ranges, function (range) {
ctx.options.range.from = range;
ctx.ds.queryNumericData = _sinon2.default.spy();
ctx.ds.queryNumericData = jest.fn();
ctx.ds.query(ctx.options);
// Check that useTrends options is false
expect(ctx.ds.queryNumericData).to.have.been.calledWith(defined, defined, false, _sinon2.default.match.any);
var callArgs = ctx.ds.queryNumericData.mock.calls[0];
expect(callArgs[2]).toBe(false);
ctx.ds.queryNumericData.mockClear();
});
done();
});
@@ -114,7 +113,7 @@ describe('ZabbixDatasource', function () {
};
var result = ctx.ds.replaceTemplateVars(target);
expect(result).to.equal(expectedResult);
expect(result).toBe(expectedResult);
done();
}
@@ -164,25 +163,16 @@ describe('ZabbixDatasource', function () {
return str;
};
ctx.ds.zabbix = {
getGroups: function getGroups() {
return _q2.default.when([]);
},
getHosts: function getHosts() {
return _q2.default.when([]);
},
getApps: function getApps() {
return _q2.default.when([]);
},
getItems: function getItems() {
return _q2.default.when([]);
}
getGroups: jest.fn().mockReturnValue(_q2.default.when([])),
getHosts: jest.fn().mockReturnValue(_q2.default.when([])),
getApps: jest.fn().mockReturnValue(_q2.default.when([])),
getItems: jest.fn().mockReturnValue(_q2.default.when([]))
};
});
it('should return groups', function (done) {
var tests = [{ query: '*', expect: '/.*/' }, { query: '', expect: '' }, { query: 'Backend', expect: 'Backend' }, { query: 'Back*', expect: 'Back*' }];
var getGroups = _sinon2.default.spy(ctx.ds.zabbix, 'getGroups');
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
@@ -192,8 +182,8 @@ describe('ZabbixDatasource', function () {
var test = _step.value;
ctx.ds.metricFindQuery(test.query);
expect(getGroups).to.have.been.calledWith(test.expect);
getGroups.reset();
expect(ctx.ds.zabbix.getGroups).toBeCalledWith(test.expect);
ctx.ds.zabbix.getGroups.mockClear();
}
} catch (err) {
_didIteratorError = true;
@@ -214,9 +204,8 @@ describe('ZabbixDatasource', function () {
});
it('should return hosts', function (done) {
var tests = [{ query: '*.*', expect: '/.*/' }, { query: '.', expect: '' }, { query: 'Backend.*', expect: 'Backend' }, { query: 'Back*.', expect: 'Back*' }];
var tests = [{ query: '*.*', expect: ['/.*/', '/.*/'] }, { query: '.', expect: ['', ''] }, { query: 'Backend.*', expect: ['Backend', '/.*/'] }, { query: 'Back*.', expect: ['Back*', ''] }];
var getHosts = _sinon2.default.spy(ctx.ds.zabbix, 'getHosts');
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
@@ -226,8 +215,8 @@ describe('ZabbixDatasource', function () {
var test = _step2.value;
ctx.ds.metricFindQuery(test.query);
expect(getHosts).to.have.been.calledWith(test.expect);
getHosts.reset();
expect(ctx.ds.zabbix.getHosts).toBeCalledWith(test.expect[0], test.expect[1]);
ctx.ds.zabbix.getHosts.mockClear();
}
} catch (err) {
_didIteratorError2 = true;
@@ -248,9 +237,8 @@ describe('ZabbixDatasource', function () {
});
it('should return applications', function (done) {
var tests = [{ query: '*.*.*', expect: ['/.*/', '/.*/'] }, { query: '.*.', expect: ['', '/.*/'] }, { query: 'Backend.backend01.*', expect: ['Backend', 'backend01'] }, { query: 'Back*.*.', expect: ['Back*', '/.*/'] }];
var tests = [{ query: '*.*.*', expect: ['/.*/', '/.*/', '/.*/'] }, { query: '.*.', expect: ['', '/.*/', ''] }, { query: 'Backend.backend01.*', expect: ['Backend', 'backend01', '/.*/'] }, { query: 'Back*.*.', expect: ['Back*', '/.*/', ''] }];
var getApps = _sinon2.default.spy(ctx.ds.zabbix, 'getApps');
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
@@ -260,8 +248,8 @@ describe('ZabbixDatasource', function () {
var test = _step3.value;
ctx.ds.metricFindQuery(test.query);
expect(getApps).to.have.been.calledWith(test.expect[0], test.expect[1]);
getApps.reset();
expect(ctx.ds.zabbix.getApps).toBeCalledWith(test.expect[0], test.expect[1], test.expect[2]);
ctx.ds.zabbix.getApps.mockClear();
}
} catch (err) {
_didIteratorError3 = true;
@@ -282,9 +270,8 @@ describe('ZabbixDatasource', function () {
});
it('should return items', function (done) {
var tests = [{ query: '*.*.*.*', expect: ['/.*/', '/.*/', ''] }, { query: '.*.*.*', expect: ['', '/.*/', ''] }, { query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', ''] }, { query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu'] }];
var tests = [{ query: '*.*.*.*', expect: ['/.*/', '/.*/', '', '/.*/'] }, { query: '.*.*.*', expect: ['', '/.*/', '', '/.*/'] }, { query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', '', '/.*/'] }, { query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu', '/.*/'] }];
var getItems = _sinon2.default.spy(ctx.ds.zabbix, 'getItems');
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
@@ -294,8 +281,8 @@ describe('ZabbixDatasource', function () {
var test = _step4.value;
ctx.ds.metricFindQuery(test.query);
expect(getItems).to.have.been.calledWith(test.expect[0], test.expect[1], test.expect[2]);
getItems.reset();
expect(ctx.ds.zabbix.getItems).toBeCalledWith(test.expect[0], test.expect[1], test.expect[2], test.expect[3]);
ctx.ds.zabbix.getItems.mockClear();
}
} catch (err) {
_didIteratorError4 = true;
@@ -318,9 +305,8 @@ describe('ZabbixDatasource', function () {
it('should invoke method with proper arguments', function (done) {
var query = '*.*';
var getHosts = _sinon2.default.spy(ctx.ds.zabbix, 'getHosts');
ctx.ds.metricFindQuery(query);
expect(getHosts).to.have.been.calledWith('/.*/');
expect(ctx.ds.zabbix.getHosts).toBeCalledWith('/.*/', '/.*/');
done();
});
});
@@ -372,8 +358,8 @@ describe('ZabbixDatasource', function () {
};
return ctx.ds.alertQuery(options).then(function (resp) {
expect(resp.thresholds.length).to.equal(1);
expect(resp.thresholds[0]).to.equal(100);
expect(resp.thresholds).toHaveLength(1);
expect(resp.thresholds[0]).toBe(100);
return resp;
});
});
@@ -391,8 +377,8 @@ describe('ZabbixDatasource', function () {
};
return ctx.ds.alertQuery(options).then(function (resp) {
expect(resp.thresholds.length).to.equal(1);
expect(resp.thresholds[0]).to.equal(100);
expect(resp.thresholds.length).toBe(1);
expect(resp.thresholds[0]).toBe(100);
return resp;
});
});
@@ -410,8 +396,8 @@ describe('ZabbixDatasource', function () {
};
return ctx.ds.alertQuery(options).then(function (resp) {
expect(resp.thresholds.length).to.equal(1);
expect(resp.thresholds[0]).to.equal(30);
expect(resp.thresholds.length).toBe(1);
expect(resp.thresholds[0]).toBe(30);
return resp;
});
});
@@ -429,8 +415,8 @@ describe('ZabbixDatasource', function () {
};
return ctx.ds.alertQuery(options).then(function (resp) {
expect(resp.thresholds.length).to.equal(1);
expect(resp.thresholds[0]).to.equal(50);
expect(resp.thresholds.length).toBe(1);
expect(resp.thresholds[0]).toBe(50);
return resp;
});
});

View File

@@ -1,67 +0,0 @@
'use strict';
var _prunk = require('prunk');
var _prunk2 = _interopRequireDefault(_prunk);
var _jsdom = require('jsdom');
var _chai = require('chai');
var _chai2 = _interopRequireDefault(_chai);
var _sinonChai = require('sinon-chai');
var _sinonChai2 = _interopRequireDefault(_sinonChai);
var _datemath = require('./modules/datemath');
var dateMath = _interopRequireWildcard(_datemath);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Mock angular module
// import sinon from 'sinon';
var angularMocks = {
module: function module() {
return {
directive: function directive() {},
service: function service() {},
factory: function factory() {}
};
}
}; // JSHint options
/* globals global: false */
var datemathMock = {
parse: dateMath.parse,
parseDateMath: dateMath.parseDateMath,
isValid: dateMath.isValid
};
// Mock Grafana modules that are not available outside of the core project
// Required for loading module.js
_prunk2.default.mock('./css/query-editor.css!', 'no css, dude.');
_prunk2.default.mock('app/plugins/sdk', {
QueryCtrl: null
});
_prunk2.default.mock('app/core/utils/datemath', datemathMock);
_prunk2.default.mock('app/core/table_model', {});
_prunk2.default.mock('angular', angularMocks);
_prunk2.default.mock('jquery', 'module not found');
// Required for loading angularjs
var dom = new _jsdom.JSDOM('<html><head><script></script></head><body></body></html>');
// Setup jsdom
global.window = dom.window;
global.document = global.window.document;
global.Node = window.Node;
// Setup Chai
_chai2.default.should();
_chai2.default.use(_sinonChai2.default);
global.assert = _chai2.default.assert;
global.expect = _chai2.default.expect;

View File

@@ -15,7 +15,7 @@ describe('timeseries processing functions', function () {
var expected = [[2, 1], [4, 2], [5, 3]];
var result = _timeseries2.default.sumSeries(series);
expect(result).to.eql(expected);
expect(result).toEqual(expected);
done();
});
@@ -26,7 +26,7 @@ describe('timeseries processing functions', function () {
var expected = [[1, 1], [4, 2], [5, 3]];
var result = _timeseries2.default.sumSeries(series);
expect(result).to.eql(expected);
expect(result).toEqual(expected);
done();
});
});

View File

@@ -33,7 +33,7 @@ describe('Utils', function () {
_lodash2.default.each(test_cases, function (test_case) {
var expandedName = utils.expandItemName(test_case.name, test_case.key);
expect(expandedName).to.equal(test_case.expected);
expect(expandedName).toBe(test_case.expected);
});
done();
});
@@ -59,7 +59,7 @@ describe('Utils', function () {
_lodash2.default.each(test_cases, function (test_case) {
var expandedName = utils.expandItemName(test_case.name, test_case.key);
expect(expandedName).to.equal(test_case.expected);
expect(expandedName).toBe(test_case.expected);
});
done();
});
@@ -81,7 +81,7 @@ describe('Utils', function () {
_lodash2.default.each(test_cases, function (test_case) {
var expandedName = utils.expandItemName(test_case.name, test_case.key);
expect(expandedName).to.equal(test_case.expected);
expect(expandedName).toBe(test_case.expected);
});
done();
});
@@ -101,7 +101,7 @@ describe('Utils', function () {
_lodash2.default.each(test_cases, function (test_case) {
var splitQuery = utils.splitTemplateQuery(test_case.query);
expect(splitQuery).to.eql(test_case.expected);
expect(splitQuery).toEqual(test_case.expected);
});
done();
});
@@ -123,7 +123,7 @@ describe('Utils', function () {
_lodash2.default.each(test_cases, function (test_case) {
var splitQuery = utils.splitTemplateQuery(test_case.query);
expect(splitQuery).to.eql(test_case.expected);
expect(splitQuery).toEqual(test_case.expected);
});
done();
});

3
dist/test/test-setup/cssStub.js vendored Normal file
View File

@@ -0,0 +1,3 @@
"use strict";
module.exports = {};

52
dist/test/test-setup/jest-setup.js vendored Normal file
View File

@@ -0,0 +1,52 @@
'use strict';
var _jsdom = require('jsdom');
// Mock Grafana modules that are not available outside of the core project
// Required for loading module.js
jest.mock('angular', function () {
return {
module: function module() {
return {
directive: function directive() {},
service: function service() {},
factory: function factory() {}
};
}
};
}, { virtual: true }); // JSHint options
/* globals global: false */
jest.mock('app/plugins/sdk', function () {
return {
QueryCtrl: null
};
}, { virtual: true });
jest.mock('app/core/utils/datemath', function () {
var datemath = require('./modules/datemath');
return {
parse: datemath.parse,
parseDateMath: datemath.parseDateMath,
isValid: datemath.isValid
};
}, { virtual: true });
jest.mock('app/core/table_model', function () {
return {};
}, { virtual: true });
jest.mock('./css/query-editor.css!', function () {
return "";
}, { virtual: true });
jest.mock('jquery', function () {
return 'module not found';
}, { virtual: true });
// Required for loading angularjs
var dom = new _jsdom.JSDOM('<html><head><script></script></head><body></body></html>');
// Setup jsdom
global.window = dom.window;
global.document = global.window.document;
global.Node = window.Node;

11
jest.config.js Normal file
View File

@@ -0,0 +1,11 @@
module.exports = {
"roots": [
"<rootDir>/dist/test"
],
"setupFiles": [
"<rootDir>/dist/test/test-setup/jest-setup.js"
],
"moduleNameMapper": {
"^[./a-zA-Z0-9$_-]+\.css\!?$": "<rootDir>/dist/test/test-setup/cssStub.js",
}
};

View File

@@ -4,8 +4,8 @@
"version": "3.6.0",
"description": "Zabbix plugin for Grafana",
"scripts": {
"test": "./node_modules/.bin/grunt test",
"build": "./node_modules/.bin/grunt",
"test": "./node_modules/.bin/grunt buildTest && jest",
"build": "./node_modules/.bin/grunt && jest",
"watch": "./node_modules/.bin/grunt watch"
},
"repository": {
@@ -18,12 +18,12 @@
"url": "https://github.com/alexanderzobnin/grafana-zabbix/issues"
},
"devDependencies": {
"babel": "~6.5.1",
"babel-plugin-transform-es2015-for-of": "^6.6.0",
"babel-plugin-transform-es2015-modules-systemjs": "^6.5.0",
"babel-preset-es2015": "^6.5.0",
"babel": "~6.5.1",
"benchmark":"^2.1.4",
"chai": "~3.5.0",
"benchmark": "^2.1.4",
"grunt": "~0.4.5",
"grunt-babel": "~6.0.0",
"grunt-benchmark": "^1.0.0",
"grunt-cli": "~1.2.0",
@@ -34,19 +34,14 @@
"grunt-contrib-watch": "^0.6.1",
"grunt-execute": "~0.2.2",
"grunt-jscs": "^2.8.0",
"grunt-mocha-test": "~0.12.7",
"grunt-sass": "^1.1.0",
"grunt-systemjs-builder": "^0.2.5",
"grunt": "~0.4.5",
"jest": "^21.2.1",
"jsdom": "~11.3.0",
"jshint-stylish": "^2.1.0",
"load-grunt-tasks": "~3.2.0",
"mocha": "^2.4.5",
"moment": "~2.14.1",
"prunk": "~1.2.1",
"q": "~1.4.1",
"sinon-chai": "~2.13.0",
"sinon":"^3.2.1",
"tether-drop": "^1.4.2"
},
"dependencies": {

View File

@@ -1,12 +1,10 @@
import _ from 'lodash';
import Q from "q";
import {Datasource} from "../module";
import {zabbixTemplateFormat} from "../datasource";
import Q from "q";
import sinon from 'sinon';
import _ from 'lodash';
describe('ZabbixDatasource', () => {
let ctx = {};
let defined = sinon.match.defined;
beforeEach(() => {
ctx.instanceSettings = {
@@ -55,7 +53,7 @@ describe('ZabbixDatasource', () => {
range: {from: 'now-6h', to: 'now'}
};
ctx.ds.query(options).then(result => {
expect(result.data).to.have.length(0);
expect(result.data.length).toBe(0);
done();
});
});
@@ -65,12 +63,13 @@ describe('ZabbixDatasource', () => {
_.forEach(ranges, range => {
ctx.options.range.from = range;
ctx.ds.queryNumericData = sinon.spy();
ctx.ds.queryNumericData = jest.fn();
ctx.ds.query(ctx.options);
// Check that useTrends options is true
expect(ctx.ds.queryNumericData)
.to.have.been.calledWith(defined, defined, true, sinon.match.any);
let callArgs = ctx.ds.queryNumericData.mock.calls[0];
expect(callArgs[2]).toBe(true);
ctx.ds.queryNumericData.mockClear();
});
done();
@@ -81,12 +80,13 @@ describe('ZabbixDatasource', () => {
_.forEach(ranges, range => {
ctx.options.range.from = range;
ctx.ds.queryNumericData = sinon.spy();
ctx.ds.queryNumericData = jest.fn();
ctx.ds.query(ctx.options);
// Check that useTrends options is false
expect(ctx.ds.queryNumericData)
.to.have.been.calledWith(defined, defined, false, sinon.match.any);
let callArgs = ctx.ds.queryNumericData.mock.calls[0];
expect(callArgs[2]).toBe(false);
ctx.ds.queryNumericData.mockClear();
});
done();
});
@@ -101,7 +101,7 @@ describe('ZabbixDatasource', () => {
};
let result = ctx.ds.replaceTemplateVars(target);
expect(result).to.equal(expectedResult);
expect(result).toBe(expectedResult);
done();
}
@@ -149,10 +149,10 @@ describe('ZabbixDatasource', () => {
beforeEach(() => {
ctx.ds.replaceTemplateVars = (str) => str;
ctx.ds.zabbix = {
getGroups: () => Q.when([]),
getHosts: () => Q.when([]),
getApps: () => Q.when([]),
getItems: () => Q.when([])
getGroups: jest.fn().mockReturnValue(Q.when([])),
getHosts: jest.fn().mockReturnValue(Q.when([])),
getApps: jest.fn().mockReturnValue(Q.when([])),
getItems: jest.fn().mockReturnValue(Q.when([]))
};
});
@@ -161,66 +161,62 @@ describe('ZabbixDatasource', () => {
{query: '*', expect: '/.*/'},
{query: '', expect: ''},
{query: 'Backend', expect: 'Backend'},
{query: 'Back*', expect: 'Back*'}
{query: 'Back*', expect: 'Back*'},
];
let getGroups = sinon.spy(ctx.ds.zabbix, 'getGroups');
for (const test of tests) {
ctx.ds.metricFindQuery(test.query);
expect(getGroups).to.have.been.calledWith(test.expect);
getGroups.reset();
expect(ctx.ds.zabbix.getGroups).toBeCalledWith(test.expect);
ctx.ds.zabbix.getGroups.mockClear();
}
done();
});
it('should return hosts', (done) => {
const tests = [
{query: '*.*', expect: '/.*/'},
{query: '.', expect: ''},
{query: 'Backend.*', expect: 'Backend'},
{query: 'Back*.', expect: 'Back*'}
{query: '*.*', expect: ['/.*/', '/.*/']},
{query: '.', expect: ['', '']},
{query: 'Backend.*', expect: ['Backend', '/.*/']},
{query: 'Back*.', expect: ['Back*', '']},
];
let getHosts = sinon.spy(ctx.ds.zabbix, 'getHosts');
for (const test of tests) {
ctx.ds.metricFindQuery(test.query);
expect(getHosts).to.have.been.calledWith(test.expect);
getHosts.reset();
expect(ctx.ds.zabbix.getHosts).toBeCalledWith(test.expect[0], test.expect[1]);
ctx.ds.zabbix.getHosts.mockClear();
}
done();
});
it('should return applications', (done) => {
const tests = [
{query: '*.*.*', expect: ['/.*/', '/.*/']},
{query: '.*.', expect: ['', '/.*/']},
{query: 'Backend.backend01.*', expect: ['Backend', 'backend01']},
{query: 'Back*.*.', expect: ['Back*', '/.*/']}
{query: '*.*.*', expect: ['/.*/', '/.*/', '/.*/']},
{query: '.*.', expect: ['', '/.*/', '']},
{query: 'Backend.backend01.*', expect: ['Backend', 'backend01', '/.*/']},
{query: 'Back*.*.', expect: ['Back*', '/.*/', '']}
];
let getApps = sinon.spy(ctx.ds.zabbix, 'getApps');
for (const test of tests) {
ctx.ds.metricFindQuery(test.query);
expect(getApps).to.have.been.calledWith(test.expect[0], test.expect[1]);
getApps.reset();
expect(ctx.ds.zabbix.getApps).toBeCalledWith(test.expect[0], test.expect[1], test.expect[2]);
ctx.ds.zabbix.getApps.mockClear();
}
done();
});
it('should return items', (done) => {
const tests = [
{query: '*.*.*.*', expect: ['/.*/', '/.*/', '']},
{query: '.*.*.*', expect: ['', '/.*/', '']},
{query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', '']},
{query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu']}
{query: '*.*.*.*', expect: ['/.*/', '/.*/', '', '/.*/']},
{query: '.*.*.*', expect: ['', '/.*/', '', '/.*/']},
{query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', '', '/.*/']},
{query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu', '/.*/']}
];
let getItems = sinon.spy(ctx.ds.zabbix, 'getItems');
for (const test of tests) {
ctx.ds.metricFindQuery(test.query);
expect(getItems)
.to.have.been.calledWith(test.expect[0], test.expect[1], test.expect[2]);
getItems.reset();
expect(ctx.ds.zabbix.getItems)
.toBeCalledWith(test.expect[0], test.expect[1], test.expect[2], test.expect[3]);
ctx.ds.zabbix.getItems.mockClear();
}
done();
});
@@ -228,9 +224,8 @@ describe('ZabbixDatasource', () => {
it('should invoke method with proper arguments', (done) => {
let query = '*.*';
let getHosts = sinon.spy(ctx.ds.zabbix, 'getHosts');
ctx.ds.metricFindQuery(query);
expect(getHosts).to.have.been.calledWith('/.*/');
expect(ctx.ds.zabbix.getHosts).toBeCalledWith('/.*/', '/.*/');
done();
});
});
@@ -277,8 +272,8 @@ describe('ZabbixDatasource', () => {
return ctx.ds.alertQuery(options)
.then(resp => {
expect(resp.thresholds.length).to.equal(1);
expect(resp.thresholds[0]).to.equal(100);
expect(resp.thresholds).toHaveLength(1);
expect(resp.thresholds[0]).toBe(100);
return resp;
});
});
@@ -295,8 +290,8 @@ describe('ZabbixDatasource', () => {
return ctx.ds.alertQuery(options)
.then(resp => {
expect(resp.thresholds.length).to.equal(1);
expect(resp.thresholds[0]).to.equal(100);
expect(resp.thresholds.length).toBe(1);
expect(resp.thresholds[0]).toBe(100);
return resp;
});
});
@@ -313,8 +308,8 @@ describe('ZabbixDatasource', () => {
return ctx.ds.alertQuery(options)
.then(resp => {
expect(resp.thresholds.length).to.equal(1);
expect(resp.thresholds[0]).to.equal(30);
expect(resp.thresholds.length).toBe(1);
expect(resp.thresholds[0]).toBe(30);
return resp;
});
});
@@ -331,8 +326,8 @@ describe('ZabbixDatasource', () => {
return ctx.ds.alertQuery(options)
.then(resp => {
expect(resp.thresholds.length).to.equal(1);
expect(resp.thresholds[0]).to.equal(50);
expect(resp.thresholds.length).toBe(1);
expect(resp.thresholds[0]).toBe(50);
return resp;
});
});

View File

@@ -1,50 +0,0 @@
// JSHint options
/* globals global: false */
import prunk from 'prunk';
import {JSDOM} from 'jsdom';
import chai from 'chai';
// import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import * as dateMath from './modules/datemath';
// Mock angular module
var angularMocks = {
module: function() {
return {
directive: function() {},
service: function() {},
factory: function() {}
};
}
};
var datemathMock = {
parse: dateMath.parse,
parseDateMath: dateMath.parseDateMath,
isValid: dateMath.isValid
};
// Mock Grafana modules that are not available outside of the core project
// Required for loading module.js
prunk.mock('./css/query-editor.css!', 'no css, dude.');
prunk.mock('app/plugins/sdk', {
QueryCtrl: null
});
prunk.mock('app/core/utils/datemath', datemathMock);
prunk.mock('app/core/table_model', {});
prunk.mock('angular', angularMocks);
prunk.mock('jquery', 'module not found');
// Required for loading angularjs
let dom = new JSDOM('<html><head><script></script></head><body></body></html>');
// Setup jsdom
global.window = dom.window;
global.document = global.window.document;
global.Node = window.Node;
// Setup Chai
chai.should();
chai.use(sinonChai);
global.assert = chai.assert;
global.expect = chai.expect;

View File

@@ -13,7 +13,7 @@ describe('timeseries processing functions', () => {
let expected = [[2, 1], [4, 2], [5, 3]];
let result = ts.sumSeries(series);
expect(result).to.eql(expected);
expect(result).toEqual(expected);
done();
});
@@ -27,7 +27,7 @@ describe('timeseries processing functions', () => {
let expected = [[1, 1], [4, 2], [5, 3]];
let result = ts.sumSeries(series);
expect(result).to.eql(expected);
expect(result).toEqual(expected);
done();
});
});

View File

@@ -26,7 +26,7 @@ describe('Utils', () => {
_.each(test_cases, test_case => {
let expandedName = utils.expandItemName(test_case.name, test_case.key);
expect(expandedName).to.equal(test_case.expected);
expect(expandedName).toBe(test_case.expected);
});
done();
});
@@ -57,7 +57,7 @@ describe('Utils', () => {
_.each(test_cases, test_case => {
let expandedName = utils.expandItemName(test_case.name, test_case.key);
expect(expandedName).to.equal(test_case.expected);
expect(expandedName).toBe(test_case.expected);
});
done();
});
@@ -83,7 +83,7 @@ describe('Utils', () => {
_.each(test_cases, test_case => {
let expandedName = utils.expandItemName(test_case.name, test_case.key);
expect(expandedName).to.equal(test_case.expected);
expect(expandedName).toBe(test_case.expected);
});
done();
});
@@ -106,7 +106,7 @@ describe('Utils', () => {
_.each(test_cases, test_case => {
let splitQuery = utils.splitTemplateQuery(test_case.query);
expect(splitQuery).to.eql(test_case.expected);
expect(splitQuery).toEqual(test_case.expected);
});
done();
});
@@ -133,7 +133,7 @@ describe('Utils', () => {
_.each(test_cases, test_case => {
let splitQuery = utils.splitTemplateQuery(test_case.query);
expect(splitQuery).to.eql(test_case.expected);
expect(splitQuery).toEqual(test_case.expected);
});
done();
});

View File

@@ -0,0 +1 @@
module.exports = {};

View File

@@ -0,0 +1,50 @@
// JSHint options
/* globals global: false */
import {JSDOM} from 'jsdom';
// Mock Grafana modules that are not available outside of the core project
// Required for loading module.js
jest.mock('angular', () => {
return {
module: function() {
return {
directive: function() {},
service: function() {},
factory: function() {}
};
}
};
}, {virtual: true});
jest.mock('app/plugins/sdk', () => {
return {
QueryCtrl: null
};
}, {virtual: true});
jest.mock('app/core/utils/datemath', () => {
const datemath = require('./modules/datemath');
return {
parse: datemath.parse,
parseDateMath: datemath.parseDateMath,
isValid: datemath.isValid
};
}, {virtual: true});
jest.mock('app/core/table_model', () => {
return {};
}, {virtual: true});
jest.mock('./css/query-editor.css!', () => {
return "";
}, {virtual: true});
jest.mock('jquery', () => 'module not found', {virtual: true});
// Required for loading angularjs
let dom = new JSDOM('<html><head><script></script></head><body></body></html>');
// Setup jsdom
global.window = dom.window;
global.document = global.window.document;
global.Node = window.Node;