Best JavaScript code snippet using stryker-parent
test-gateway.js
Source:test-gateway.js  
...112            gw.ingestMetrics(typeMetrics + valueMetrics, function(err) {113                t.ifError(err);114                t.contains(gw.helpInfo, { t2: '# HELP t2 my-help' });115                t.contains(gw.typeInfo, { t1: '# TYPE t1 my-type-t1' });116                var report = gw.reportMetrics().join('\n') + '\n';117                t.contains(report, '# HELP t1 custom metric\n# TYPE t1 my-type-t1\nt1 1 1500000001000\n');118                t.contains(report, '# HELP t2 my-help\n# TYPE t2 my-type-t2\nt2 2 1500000002000\n');119                var report2 = gw.reportMetrics().join('\n') + '\n';120                t.contains(report2, '# HELP t1 custom metric\n# TYPE t1 my-type-t1\nt1 1 1500000001000\n');121                t.contains(report2, '# HELP t2 my-help\n# TYPE t2 my-type-t2\nt2 2 1500000002000\n');122                gw.clear();123                gw.ingestMetrics(valueMetrics, function(err) {124                    t.ifError(err);125                    var report3 = gw.reportMetrics().join('\n') + '\n';126                    t.contains(report3, '# HELP t1 custom metric\n# TYPE t1 gauge\nt1 1 1500000001000\n');127                    t.contains(report3, '# HELP t2 custom metric\n# TYPE t2 gauge\nt2 2 1500000002000\n');128                    t.done();129                })130            })131        },132        'should associate remembered type attributes with labeled metric name': function(t) {133            var gw = this.gw;134            gw.ingestMetrics('# TYPE t1 my-type', function(err) {135                t.ifError(err);136                gw.ingestMetrics('\n\nt2 23\n\nt1{x="1"} 12\n', function(err) {137                    t.ifError(err);138                    t.contains(gw.reportMetrics().join('\n'), '# TYPE t1 my-type\nt1{x="1"} 12');139                    t.done();140                })141            })142        },143        'should retain last metric value until cleared': function(t) {144            var gw = this.gw;145            var metrics =146                't1 1 1500000001000\n' +147                't2 2 1500000002000\n' +148                '';149            gw.ingestMetrics(metrics, function(err) {150                t.ifError(err);151                var report = gw.reportMetrics().join('\n');152                t.contains(report, 't1 1 1500000001000');153                t.contains(report, 't2 2 1500000002000');154                var report2 = gw.reportMetrics().join('\n');155                t.contains(report2, 't1 1 1500000001000');156                t.contains(report2, 't2 2 1500000002000');157                gw.clear();158                var report3 = gw.reportMetrics().join('\n');159                t.equal(report3, '');160                gw.ingestMetrics('t3 3', function(err) {161                    t.ifError(err);162                    var report4 = gw.reportMetrics().join('\n');163                    t.contains(report4, 't3 3');164                    t.done();165                })166            })167        },168        'should discard samples if cleared': function(t) {169            var gw = this.gw;170            gw.ingestMetrics('t1 1', function(err) {171                gw.clear();172                t.equal(gw.reportMetrics().join('\n'), '');173                t.done();174            })175        },176    },177    'ingestMetricsStackdriver': {178        'should throw on wrong protocol': function(t) {179            var gw = this.gw;180            var metrics = {181                timestamp: 2000000001,182                proto_version: 2,183                data: [184                    { name: 'metric1', value: 1, collected_at: 1234 },185                ]186            };187            t.throws(function(){ gw.ingestMetricsStackdriver(JSON.stringify(metrics), function(){}) }, /unsupported.* proto_version/);188            t.done();189        },190        'should convert and ingest metrics, retaining instance name': function(t) {191            var gw = this.gw;192            var metrics = {193                timestamp: 2111111111,194                proto_version: 1,195                data: [196                    { name: 'metric1', value: 1.5, collected_at: 1234, instance: 'i-000123' },197                    { name: 'metric2', value: 2.5, collected_at: 1235 },198                    { name: 'metric3', value: NaN },199                ]200            };201            gw.ingestMetricsStackdriver(JSON.stringify(metrics), function(err) {202                t.equal(gw.samples.length, 3);203                t.deepEqual(gw.samples[0], { id: 'metric1{instance="i-000123"}', name: 'metric1', value: '1.5', ts: '1234000', labels: 'instance="i-000123"' });204                t.deepEqual(gw.samples[1], { id: 'metric2', name: 'metric2', value: '2.5', ts: '1235000', labels: '' });205                t.deepEqual(gw.samples[2], { id: 'metric3', name: 'metric3', value: 'NaN', ts: '2111111111000', labels: '' });206                t.done();207            })208        },209    },210    'parseMetricsLines': {211        beforeEach: function(done) {212            this.lines = (213                'metric1 1 2111111111000\n' +214                '# TYPE counter\n' +215                'metric2 2 2111111112000\n' +216                '# comment\n' +217                'bad_metric\n' +218                '# TYPE gauge\n' +219                'metric3 3\n' +220                '').split('\n');221            this.badLines = [];222            this.goodLines = [];223            this.samples = [];224            done();225        },226        'should retain existing timestamp': function(t) {227            this.gw.parseMetricsLines(this.lines, this.badLines, this.goodLines, this.samples);228            t.contains(this.samples, { name: 'metric1', value: '1', ts: '2111111111000' });229            t.contains(this.samples, { name: 'metric2', value: '2', ts: '2111111112000' });230            t.done();231        },232        'should parse lines into samples': function(t) {233            var lines = ['metric1_name 1.25 2000000001000', 'metric2_name{a="one",b="two",} 2.5'];234            var now = Date.now();235            this.gw.parseMetricsLines(lines, this.badLines, this.goodLines, this.samples);236            t.equal(this.samples.length, 2);237            t.contains(this.samples[0], { name: 'metric1_name', value: '1.25', ts: '2000000001000', id: 'metric1_name' });238            t.contains(this.samples[1], { name: 'metric2_name', value: '2.5', id: 'metric2_name{a="one",b="two",}', labels: 'a="one",b="two",' });239            t.ok(this.samples[1].ts >= now);240            t.done();241        },242        'should assign timestamp if not present': function(t) {243            var lines = [ 'metric1 1', 'metric2 2' ];244            var now = Date.now();245            this.gw.parseMetricsLines(lines, this.badLines, this.goodLines, this.samples);246            t.equal(this.samples.length, 2);247            t.ok(this.samples[0].ts >= now);248            t.ok(this.samples[1].ts >= now);249            t.done();250        },251        'should separate bad lines': function(t) {252            this.gw.parseMetricsLines(this.lines, this.badLines, this.goodLines, this.samples);253            t.contains(this.badLines, 'bad_metric');254            t.done();255        },256        'should separate and timestamp good metrics lines': function(t) {257            this.gw.parseMetricsLines(this.lines, this.badLines, this.goodLines, this.samples);258            t.equal(this.goodLines.length, 3);259            t.equal(this.goodLines[0], 'metric1 1 2111111111000');260            t.equal(this.goodLines[1], 'metric2 2 2111111112000');261            t.ok(/^metric3 3 \d+$/.test(this.goodLines[2]));262            t.done();263        },264    },265    'reportMetrics': {266        beforeEach: function(done) {267            this.metrics =268                '# comment\n' +269                'metric1 1   1500000002000\n' +270                'metric2 2   1500000002000\n' +271                'metric1 1.2 1500000001000\n' +272                'metric2 3   1500000003000\n' +273                'metric3{name="value"} 3' +274                '';275            done();276        },277        'should sort samples into by time asc': function(t) {278            var gw = this.gw;279            var metrics =280                'm1 1 2\n' +281                'm2 2 1\n' +282                'm3 3 4\n' +283                'm4 4 3\n' +284                '';285            gw.ingestMetrics(metrics, function(err) {286                t.ifError(err);287                var report = gw.reportMetrics();288                t.contains(report, ['m2 2 1', 'm1 1 2', 'm4 4 3',  'm3 3 4']);289                t.done();290            })291        },292        'should consume samples from samples array': function(t) {293            var gw = this.gw;294            gw.ingestMetrics(this.metrics, function(err) {295                t.ifError(err);296                var lenPre = gw.samples.length;297                var report = gw.reportMetrics();298                var lenPost = gw.samples.length;299                t.equal(lenPre, 5);300                t.ok(lenPre > lenPost, "should have fewer samples");301                t.done();302            })303        },304        'should average samples with the same id': function(t) {305            var gw = this.gw;306            gw.ingestMetrics(this.metrics, function(err) {307                t.ifError(err);308                var report = gw.reportMetrics();309                t.contains(report, 'metric1 1.1 1500000002000');310                t.contains(report, 'metric2 2.5 1500000003000');311                t.done();312            })313        },314        'should honor omitTimestamps': function(t) {315            var gw = new Gateway({ omitTimestamps: true, maxMetricAgeMs: Infinity });316            gw.ingestMetrics(this.metrics, function(err) {317                t.ifError(err);318                var report = gw.reportMetrics();319                t.contains(report, 'metric1 1.1');320                t.contains(report, 'metric2 2.5');321                t.done();322            })323        },324        'should discard old samples': function(t) {325            var gw = new Gateway({ omitTimestamps: true, maxMetricAgeMs: 1000 });326            t.stub(gw, 'getTimestamp', function() { return 1500000003000 });327            gw.ingestMetrics(this.metrics, function(err) {328                t.ifError(err);329                var report = gw.reportMetrics();330                t.contains(report, 'metric1 1');331                t.contains(report, 'metric2 2.5');332                t.contains(report, 'metric3{name="value"} 3');333                t.done();334            })335        },336        'should report metrics with most recent timestamp': function(t) {337            var start = Date.now();338            var gw = this.gw;339            gw.ingestMetrics(this.metrics, function(err) {340                t.ifError(err);341                var report = gw.reportMetrics();342                t.contains(report, 'metric1 1.1 1500000002000');343                t.contains(report, 'metric2 2.5 1500000003000');344                t.ok('metric3{name="value"} 3 ' + start <= report[10] && report[10] <= 'metric3{name="value"} 3 ' + Date.now());345                t.done();346            })347        },348        'should group same-named metrics': function(t) {349            var gw = this.gw;350            var metrics =351                'metric1{host="host-01"} 1 1500000001000\n' +352                'metric2{host="host-02"} 2 1500000002000\n' +353                'metric1{host="host-03"} 3 1500000003000\n' +354                '';355            gw.ingestMetrics(metrics, function(err) {356                t.ifError(err);357                var report = gw.reportMetrics().join('\n') + '\n';358                t.contains(report, '# HELP metric1 custom metric\n# TYPE metric1 gauge\nmetric1{host="host-01"} 1 1500000001000\nmetric1{host="host-03"} 3 1500000003000\n\n');359                t.contains(report, '# HELP metric2 custom metric\n# TYPE metric2 gauge\n');360                t.contains(report, 'metric2{host="host-02"} 2 1500000002000\n');361                t.done();362            })363        },364        'should preserve prom HELP and TYPE': function(t) {365            var gw = this.gw;366            var metrics =367                'metric1 1 1500000001000\n' +368                '\n' +369                '# HELP metric2 my metric\n' +370                '# TYPE metric2 my-type\n' +371                'metric2{a="1"} 1\n' +372                'metric2{a="2"} 2\n' +373                '\n' +374                '# HELP metric3 my other metric\n' +375                '# TYPE metric3 my-other-type\n' +376                'metric3{b="1"} 3 1500000003000\n' +377                '\n' +378                'metric4 4 1500000004000\n' +379                '';380            gw.ingestMetrics(metrics, function(err) {381                t.ifError(err);382                var report = gw.reportMetrics().join('\n') + '\n';383                t.contains(report, '# HELP metric1 custom metric\n# TYPE metric1 gauge\nmetric1 1 1500000001000\n');384                t.contains(report, '# HELP metric2 my metric\n# TYPE metric2 my-type\nmetric2{a="1"} 1');385                t.contains(report, '# HELP metric3 my other metric\n# TYPE metric3 my-other-type\nmetric3{b="1"} 3 1500000003000');386                t.contains(report, '# HELP metric4 custom metric\n# TYPE metric4 gauge\nmetric4 4 1500000004000\n');387                t.done();388            })389        },390        'should include readPromMetrics() values': function(t) {391            var called;392            var promMetrics = "other metrics";393            var gw = new Gateway({ readPromMetrics: function() { called = true; return promMetrics } });394            var spyIngest = t.spyOnce(gw, 'ingestMetrics');395            gw.reportMetrics();396            t.ok(called);397            t.ok(spyIngest.called);398            t.equal(spyIngest.args[0][0], promMetrics);399            t.done();400        },401        'should tolerate readPromMetrics errors': function(t) {402            var promMetrics = "other metrics";403            var readPromMetrics = function() { throw new Error('readPromMetrics error') };404            var spy = t.spyOnce(readPromMetrics);405            var gw = new Gateway({ readPromMetrics: spy, omitTimestamps: true });406            gw.ingestMetrics('my_metric 123', function(err) {407                t.ifError(err);408                var report = gw.reportMetrics();409                t.ok(spy.called);410                t.equal(report.length, 3);411                t.equal(report[2], 'my_metric 123');412                t.done();413            })414        },415        'should report previous values': function(t) {416            var gw = this.gw;417            gw.ingestMetrics('metric1 1 1500000001000\n', function(err) {418                t.ifError(err);419                var report1 = gw.reportMetrics();420                gw.ingestMetrics('metric2 2 1500000002000\n', function(err) {421                    t.ifError(err);422                    var report2 = gw.reportMetrics();423                    t.equal(report1.length, 3);424                    t.contains(report1[0], 'HELP');425                    t.contains(report1[1], 'TYPE');426                    t.contains(report1[2], 'metric1 1');427                    t.equal(report2.length, 7);428                    t.contains(report2[2], 'metric1 1');429                    t.contains(report2[6], 'metric2 2');430                    var report3 = gw.reportMetrics();431                    t.deepEqual(report3, report2);432                    t.done();433                })434            })435        },436        'should merge configured labels with metrics labels': function(t) {437            var metrics = 'metric1{host="host-name-01"} 11.5 1500000001000\n' + 'metric1{host="host-name-01"} 13.5 1500000021000\n';438            var gw, report;439            gw = new Gateway({ labels: { host: 'host-02' } });440            gw.ingestMetrics('metric1 11.5 1600000001000\n', function(err) {441                t.ifError(err);442                report = gw.reportMetrics();443                t.contains(report[2], 'metric1{host="host-02",} 11.5 1600000001000');444            gw = new Gateway({ labels: { } });445            gw.ingestMetrics(metrics, function(err) {446                t.ifError(err);447                report = gw.reportMetrics();448                t.equal(report.length, 3);449                t.contains(report[2], 'metric1{host="host-name-01"} 12.5 1500000021000');450            gw = new Gateway({ labels: { one: 1, } });451            gw.ingestMetrics(metrics, function(err) {452                t.ifError(err);453                report = gw.reportMetrics();454                t.equal(report.length, 3);455                t.contains(report[2], 'metric1{one="1",host="host-name-01"} 12.5 1500000021000');456            gw = new Gateway({ labels: { a: 'one', b: 'two' } });457            gw.ingestMetrics(metrics, function(err) {458                t.ifError(err);459                report = gw.reportMetrics();460                t.equal(report.length, 3);461                t.contains(report[2], 'metric1{a="one",b="two",host="host-name-01"} 12.5 1500000021000');462                t.done();463            }) }) }) })464        },465    },...reportMetrics.spec.ts
Source:reportMetrics.spec.ts  
1import * as reportMetrics from './reportMetrics';2import { MockMetricsDb, MockMetricReporting, MockWorkflowRegister } from '../../__mock__/dal';3const metricsReporting = new MockMetricReporting();4const metricsDb = new MockMetricsDb();5const register = new MockWorkflowRegister();6describe('handler', () => {7    process.env.environment = 'unit-test';8    reportMetrics.setMetricsReporting(metricsReporting as any);9    reportMetrics.setMetricsDb(metricsDb as any);10    reportMetrics.setWorkflowRegister(register as any);11    test('Null db result', async () => {12        metricsReporting.reset();13        metricsDb.reset();14        register.reset();15        metricsDb.getIssueFailureCountRetval = null;16        register.listResult = ['test'];17        await reportMetrics.handler(null, null, null);18        expect(metricsReporting.reportFailuresInput.length).toBe(1);19        expect(metricsReporting.reportFailuresInput[0].count).toBe(0);20        expect(metricsReporting.reportFailuresInput[0].workflow).toBe('test');21    });22    test('No registered workflows', async () => {23        metricsReporting.reset();24        metricsDb.reset();25        register.reset();26        register.listResult = [];27        metricsDb.getIssueFailureCountRetval = [{workflow: 'test'}];28        await reportMetrics.handler(null, null, null);29        expect(metricsReporting.reportFailuresInput.length).toBe(0);30    });31    test('Workflow not registered', async () => {32        metricsReporting.reset();33        metricsDb.reset();34        register.reset();35        register.listResult = ['test'];36        metricsDb.getIssueFailureCountRetval = [{workflow: 'test'}, {workflow: 'test2'}];37        await reportMetrics.handler(null, null, null);38        expect(metricsReporting.reportFailuresInput.length).toBe(1);39        expect(metricsReporting.reportFailuresInput[0].count).toBe(1);40        expect(metricsReporting.reportFailuresInput[0].workflow).toBe('test');41    });42    test('Count returned', async () => {43        metricsReporting.reset();44        metricsDb.reset();45        register.reset();46        register.listResult = ['test'];47        metricsDb.getIssueFailureCountRetval = [{workflow: 'test'}];48        await reportMetrics.handler(null, null, null);49        expect(metricsReporting.reportFailuresInput.length).toBe(1);50        expect(metricsReporting.reportFailuresInput[0].count).toBe(1);51        expect(metricsReporting.reportFailuresInput[0].workflow).toBe('test');52    });53    test('Count returned', async () => {54        metricsReporting.reset();55        metricsDb.reset();56        register.reset();57        register.listResult = ['test'];58        metricsDb.getIssueFailureCountRetval = [59            {workflow: 'test'},60            {workflow: 'test'},61            {workflow: 'test'},62            {workflow: 'test'},63            {workflow: 'test'},64            {workflow: 'test'},65            {workflow: 'test'},66            {workflow: 'test'},67            {workflow: 'test'},68            {workflow: 'test'},69            {workflow: 'test'}70        ];71        await reportMetrics.handler(null, null, null);72        expect(metricsReporting.reportFailuresInput.length).toBe(1);73        expect(metricsReporting.reportFailuresInput[0].count).toBe(11);74        expect(metricsReporting.reportFailuresInput[0].workflow).toBe('test');75    });76});77describe('unit-test utils', () => {78    process.env.environment = 'not unit test';79    test('setMetricsReporting', async () => {80        let error = null;81        try {82            reportMetrics.setMetricsReporting(null);83        } catch (err) {84            error = err.message;85        }86        expect(error).toBe('A system is trying to use a unit test capability');87    });88    test('setMetricsDb', async () => {89        let error = null;90        try {91            reportMetrics.setMetricsDb(null);92        } catch (err) {93            error = err.message;94        }95        expect(error).toBe('A system is trying to use a unit test capability');96    });97    test('setWorkflowRegister', async () => {98        let error = null;99        try {100            reportMetrics.setWorkflowRegister(null);101        } catch (err) {102            error = err.message;103        }104        expect(error).toBe('A system is trying to use a unit test capability');105    });...Using AI Code Generation
1const stryker = require('stryker-parent');2stryker.reportMetrics({3    metrics: {4    }5});6module.exports = function(config) {7    config.set({8        dashboard: {9        }10    });11};12module.exports = {13};14{15    "scripts": {16    },17    "devDependencies": {18    }19}Using AI Code Generation
1var reportMetrics = require('stryker-parent').reportMetrics;2reportMetrics('test', 10, 100);3var reportMetrics = require('stryker-parent').reportMetrics;4reportMetrics('test', 10, 100);5module.exports = function(config) {6  config.set({7    metrics: {8    }9  });10};11module.exports = function(config) {12  config.set({13    metrics: {14    }15  });16};17module.exports = function(config) {18  config.set({19    metrics: {Using AI Code Generation
1const stryker = require('stryker')2const child = require('stryker-child')3const grandchild = require('stryker-grandchild')4function reportMetrics() {5  stryker.reportMetrics()6  child.reportMetrics()7  grandchild.reportMetrics()8}9module.exports = {10}11const stryker = require('stryker')12const grandchild = require('stryker-grandchild')13function reportMetrics() {14  stryker.reportMetrics()15  grandchild.reportMetrics()16}17module.exports = {18}19const stryker = require('stryker')20function reportMetrics() {21  stryker.reportMetrics()22}23module.exports = {24}Using AI Code Generation
1const reportMetrics = require('stryker-parent').reportMetrics;2reportMetrics('test', 'test', 100, 0, 0, 0, 0, 0);3const reportMetrics = require('stryker-parent').reportMetrics;4reportMetrics('test', 'test', 100, 0, 0, 0, 0, 0);5{ metrics: { 'test': { name: 'test', status: 'test', duration: 100, killed: 0, survived: 0, noCoverage: 0, timeout: 0, error: 0 } } }6const { fork } = require('child_process');7const stryker = fork('stryker', ['run', '--reporters', 'stryker-metrics-reporter'], { stdio: 'inherit' });8const { reportMetrics } = require('stryker-parent');9reportMetrics.subscribe((metrics) => {10});11{ metrics: { 'test': { name: 'test', status: 'test', duration: 100, killed: 0, survived: 0, noCoverage: 0, timeout: 0, error: 0 } } }12{ metrics: { 'test': { name: 'test', status: 'test', duration: 100, killed: 0, survived: 0, noCoverage: 0, timeout: 0, error: 0 } } }Using AI Code Generation
1const { reportMetrics } = require('stryker-parent');2reportMetrics({ metrics: { 'mutationScore': 0.5 } });3const { reportMetrics } = require('stryker-parent');4reportMetrics({ metrics: { 'mutationScore': 0.5 } });5const { reportMetrics } = require('stryker-parent');6reportMetrics({ metrics: { 'mutationScore': 0.5 } });7const { reportMetrics } = require('stryker-parent');8reportMetrics({ metrics: { 'mutationScore': 0.5 } });9const { reportMetrics } = require('stryker-parent');10reportMetrics({ metrics: { 'mutationScore': 0.5 } });11const { reportMetrics } = require('stryker-parent');12reportMetrics({ metrics: { 'mutationScore': 0.5 } });13const { reportMetrics } = require('stryker-parent');14reportMetrics({ metrics: { 'mutationScore': 0.5 } });15const { reportMetrics } = require('stryker-parent');16reportMetrics({ metrics: { 'mutationScore': 0.5 } });17const { reportMetrics } = require('stryker-parent');18reportMetrics({ metrics: { 'mutationScore': 0.5 } });Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
