diff --git a/.jshintrc b/.jshintrc index 300d6de..c61d857 100644 --- a/.jshintrc +++ b/.jshintrc @@ -35,6 +35,7 @@ "require": true, "Chromath": false, "setImmediate": true, + "jest": true, "expect": true, "it": true, "describe": true, diff --git a/Gruntfile.js b/Gruntfile.js index 05c3a14..4c2c138 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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', [ diff --git a/circle.yml b/circle.yml index c61417e..49b7310 100644 --- a/circle.yml +++ b/circle.yml @@ -4,4 +4,4 @@ machine: test: override: - - npm run build + - npm run ci-test diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..9287769 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,11 @@ +coverage: + precision: 2 + round: down + range: "50...100" + + status: + project: yes + patch: yes + changes: no + +comment: false diff --git a/dist/datasource-zabbix/specs/datasource_specs.js b/dist/datasource-zabbix/specs/datasource.spec.js similarity index 76% rename from dist/datasource-zabbix/specs/datasource_specs.js rename to dist/datasource-zabbix/specs/datasource.spec.js index d81bd19..abe5757 100644 --- a/dist/datasource-zabbix/specs/datasource_specs.js +++ b/dist/datasource-zabbix/specs/datasource.spec.js @@ -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; }); }); diff --git a/dist/datasource-zabbix/specs/test-main.js b/dist/datasource-zabbix/specs/test-main.js deleted file mode 100644 index c77bdce..0000000 --- a/dist/datasource-zabbix/specs/test-main.js +++ /dev/null @@ -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('
'); -// 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; diff --git a/src/datasource-zabbix/specs/timeseries_specs.js b/dist/datasource-zabbix/specs/timeseries.spec.js similarity index 89% rename from src/datasource-zabbix/specs/timeseries_specs.js rename to dist/datasource-zabbix/specs/timeseries.spec.js index fdfb37c..f1311bd 100644 --- a/src/datasource-zabbix/specs/timeseries_specs.js +++ b/dist/datasource-zabbix/specs/timeseries.spec.js @@ -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(); }); }); diff --git a/src/datasource-zabbix/specs/utils_specs.js b/dist/datasource-zabbix/specs/utils.spec.js similarity index 93% rename from src/datasource-zabbix/specs/utils_specs.js rename to dist/datasource-zabbix/specs/utils.spec.js index c540db2..d460baa 100644 --- a/src/datasource-zabbix/specs/utils_specs.js +++ b/dist/datasource-zabbix/specs/utils.spec.js @@ -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(); }); diff --git a/dist/test-setup/cssStub.js b/dist/test-setup/cssStub.js new file mode 100644 index 0000000..f053ebf --- /dev/null +++ b/dist/test-setup/cssStub.js @@ -0,0 +1 @@ +module.exports = {}; diff --git a/dist/test-setup/jest-setup.js b/dist/test-setup/jest-setup.js new file mode 100644 index 0000000..ff528b8 --- /dev/null +++ b/dist/test-setup/jest-setup.js @@ -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(''); +// Setup jsdom +global.window = dom.window; +global.document = global.window.document; +global.Node = window.Node; diff --git a/dist/datasource-zabbix/specs/modules/datemath.js b/dist/test-setup/modules/datemath.js similarity index 100% rename from dist/datasource-zabbix/specs/modules/datemath.js rename to dist/test-setup/modules/datemath.js diff --git a/dist/test/datasource-zabbix/specs/datasource_specs.js b/dist/test/datasource-zabbix/specs/datasource.spec.js similarity index 79% rename from dist/test/datasource-zabbix/specs/datasource_specs.js rename to dist/test/datasource-zabbix/specs/datasource.spec.js index be3419f..f4101e6 100644 --- a/dist/test/datasource-zabbix/specs/datasource_specs.js +++ b/dist/test/datasource-zabbix/specs/datasource.spec.js @@ -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; }); }); diff --git a/dist/test/datasource-zabbix/specs/test-main.js b/dist/test/datasource-zabbix/specs/test-main.js deleted file mode 100644 index 439f912..0000000 --- a/dist/test/datasource-zabbix/specs/test-main.js +++ /dev/null @@ -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(''); -// 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; diff --git a/dist/test/datasource-zabbix/specs/timeseries_specs.js b/dist/test/datasource-zabbix/specs/timeseries.spec.js similarity index 91% rename from dist/test/datasource-zabbix/specs/timeseries_specs.js rename to dist/test/datasource-zabbix/specs/timeseries.spec.js index 35301f3..f5db4a6 100644 --- a/dist/test/datasource-zabbix/specs/timeseries_specs.js +++ b/dist/test/datasource-zabbix/specs/timeseries.spec.js @@ -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(); }); }); diff --git a/dist/test/datasource-zabbix/specs/utils_specs.js b/dist/test/datasource-zabbix/specs/utils.spec.js similarity index 93% rename from dist/test/datasource-zabbix/specs/utils_specs.js rename to dist/test/datasource-zabbix/specs/utils.spec.js index c381ca8..42f2c67 100644 --- a/dist/test/datasource-zabbix/specs/utils_specs.js +++ b/dist/test/datasource-zabbix/specs/utils.spec.js @@ -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(); }); diff --git a/dist/test/test-setup/cssStub.js b/dist/test/test-setup/cssStub.js new file mode 100644 index 0000000..1f5824a --- /dev/null +++ b/dist/test/test-setup/cssStub.js @@ -0,0 +1,3 @@ +"use strict"; + +module.exports = {}; diff --git a/dist/test/test-setup/jest-setup.js b/dist/test/test-setup/jest-setup.js new file mode 100644 index 0000000..31d1125 --- /dev/null +++ b/dist/test/test-setup/jest-setup.js @@ -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(''); +// Setup jsdom +global.window = dom.window; +global.document = global.window.document; +global.Node = window.Node; diff --git a/dist/test/datasource-zabbix/specs/modules/datemath.js b/dist/test/test-setup/modules/datemath.js similarity index 100% rename from dist/test/datasource-zabbix/specs/modules/datemath.js rename to dist/test/test-setup/modules/datemath.js diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..2a9df0f --- /dev/null +++ b/jest.config.js @@ -0,0 +1,13 @@ +module.exports = { + "roots": [ + "