lastcall-nightcrawler
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -33,2 +33,10 @@ 'use strict'; | ||
var _console = require('../../formatters/console'); | ||
var _console2 = _interopRequireDefault(_console); | ||
var _analysis = require('../../../analysis'); | ||
var _analysis2 = _interopRequireDefault(_analysis); | ||
var _yargs = require('yargs'); | ||
@@ -41,2 +49,5 @@ | ||
jest.mock('../../formatters/junit'); | ||
jest.mock('../../formatters/console', () => { | ||
return jest.fn((_, opts) => `CONSOLE_ANALYSIS:${opts.minLevel}${opts.color ? ':color' : ''}`); | ||
}); | ||
@@ -98,2 +109,7 @@ function runWithHandler(argv, _handler) { | ||
describe('Crawl Handler', function () { | ||
var stdout; | ||
beforeEach(function () { | ||
stdout = new _stream2.default.PassThrough(); | ||
}); | ||
it('Executes the crawl', function () { | ||
@@ -106,3 +122,3 @@ let called = 0; | ||
crawlerfile: crawler, | ||
stdout: new _stream2.default.PassThrough() | ||
stdout | ||
}).then(function () { | ||
@@ -114,4 +130,2 @@ expect(called).toEqual(1); | ||
it('Displays output to indicate the success of the crawl', function () { | ||
var stdout = new _stream2.default.PassThrough(); | ||
const crawler = new _crawler2.default('', new _dummy2.default()); | ||
@@ -127,13 +141,9 @@ crawler.on('setup', () => { | ||
}).then(function () { | ||
expect(stdout.read().toString()).toMatchSnapshot(); | ||
var output = stdout.read().toString(); | ||
expect(output).toMatch('Setup\nCrawl\nAnalyze\n'); | ||
}); | ||
}); | ||
it('Outputs analysis at the end of the crawl if requested', function () { | ||
var stdout = new _stream2.default.PassThrough(); | ||
it('Outputs analysis at the end of the crawl if the output is not silent', function () { | ||
const crawler = new _crawler2.default('', new _dummy2.default()); | ||
crawler.on('analyze', function (r, a) { | ||
a.addMetric('foo', new _metrics.Number('MyTestMetric', 0, 1)); | ||
}); | ||
@@ -144,3 +154,3 @@ return (0, _crawl.handler)({ | ||
}).then(function () { | ||
expect(stdout.read().toString()).toContain('MyTestMetric'); | ||
expect(stdout.read().toString()).toContain('CONSOLE_ANALYSIS:1:color'); | ||
}); | ||
@@ -157,3 +167,3 @@ }); | ||
crawlerfile: crawler, | ||
stdout: new _stream2.default.PassThrough() | ||
stdout | ||
}); | ||
@@ -169,3 +179,3 @@ return expect(p).rejects.toBeInstanceOf(_errors.FailedAnalysisError); | ||
crawlerfile: crawler, | ||
stdout: new _stream2.default.PassThrough() | ||
stdout | ||
}); | ||
@@ -176,3 +186,3 @@ return expect(p).rejects.toBe('Oh no!'); | ||
it('Should save a junit report if requested', function () { | ||
var filename = `${_os2.default.tmpdir()}/nightcrawler-${Math.floor(Math.random() * 10000)}`; | ||
var filename = 'foo'; | ||
const crawler = new _crawler2.default('', new _dummy2.default()); | ||
@@ -183,6 +193,7 @@ | ||
junit: filename, | ||
stdout: new _stream2.default.PassThrough() | ||
stdout | ||
}).then(function () { | ||
expect(_junit2.default).toHaveBeenCalledTimes(1); | ||
expect(_junit2.default.mock.calls[0]).toEqual([filename]); | ||
expect(_junit2.default.mock.calls[0][0]).toBeInstanceOf(_analysis2.default); | ||
expect(_junit2.default.mock.calls[0][1]).toEqual({ filename: 'foo' }); | ||
}); | ||
@@ -197,3 +208,3 @@ }); | ||
json: filename, | ||
stdout: new _stream2.default.PassThrough() | ||
stdout | ||
}).then(function () { | ||
@@ -200,0 +211,0 @@ expect(_fs2.default.existsSync(filename)).toEqual(true); |
@@ -87,3 +87,3 @@ 'use strict'; | ||
stdout.write(new _console2.default().format(analysis) + _os.EOL); | ||
stdout.write((0, _console2.default)(analysis, { color: true, minLevel: 1 }) + _os.EOL); | ||
@@ -95,3 +95,3 @@ if (json.length) { | ||
if (junit.length) { | ||
new _junit2.default(junit).format(analysis); | ||
(0, _junit2.default)(analysis, { filename: junit }); | ||
} | ||
@@ -98,0 +98,0 @@ |
@@ -16,3 +16,3 @@ 'use strict'; | ||
describe('Console Formatter', function () { | ||
it('Should output a listing of results marked according to level', function () { | ||
describe('Results', function () { | ||
var a = new _analysis2.default('test', new Date()); | ||
@@ -22,5 +22,15 @@ a.addResult('OK', 0, 10, 'm1'); | ||
a.addResult('ERR', 2, 10, 'm1'); | ||
expect(new _console2.default().format(a)).toMatchSnapshot(); | ||
it('Should output results in color if requested', function () { | ||
expect((0, _console.formatResults)(a.results, { color: true, minLevel: 0 })).toMatchSnapshot(); | ||
}); | ||
it('Should output results without color if requested', function () { | ||
expect((0, _console.formatResults)(a.results, { color: false, minLevel: 0 })).toMatchSnapshot(); | ||
}); | ||
it('Should filter out success results if verbosity is warning', function () { | ||
expect((0, _console.formatResults)(a.results, { color: false, minLevel: 1 })).toMatchSnapshot(); | ||
}); | ||
}); | ||
it('Should output aggregates, marked by level', function () { | ||
describe('Metrics', function () { | ||
var a = new _analysis2.default('test', new Date()); | ||
@@ -30,8 +40,32 @@ a.addMetric('ok', new _metrics.Number('OK metric', 0, 0)); | ||
a.addMetric('err', new _metrics.Number('ERR metric', 2, 5)); | ||
expect(new _console2.default().format(a)).toMatchSnapshot(); | ||
it('Should output metrics in color if requested', function () { | ||
expect((0, _console.formatMetrics)(a.metrics, { color: true })).toMatchSnapshot(); | ||
}); | ||
it('Should output metrics without color if requested', function () { | ||
expect((0, _console.formatMetrics)(a.metrics, { color: false })).toMatchSnapshot(); | ||
}); | ||
it('Should display all metrics, even when verbosity is limited to errors', function () { | ||
var output = (0, _console.formatMetrics)(a.metrics, { color: false, minLevel: 2 }); | ||
expect(output).toMatch('OK metric'); | ||
expect(output).toMatch('ERR metric'); | ||
}); | ||
}); | ||
it('Should show no results when there are no results or metrics', function () { | ||
var a = new _analysis2.default('test', new Date()); | ||
expect(new _console2.default().format(a)).toMatchSnapshot(); | ||
describe('Format', function () { | ||
it('Should show output when there are no results or metrics', function () { | ||
expect((0, _console2.default)(new _analysis2.default(), { color: false, minLevel: 0 })).toMatchSnapshot(); | ||
}); | ||
it('Should show output in color when there are no results or metrics and color is requested', function () { | ||
expect((0, _console2.default)(new _analysis2.default(), { color: true, minLevel: 0 })).toMatchSnapshot(); | ||
}); | ||
it('Should show results and metrics when given an analysis containing results and metrics', function () { | ||
var a = new _analysis2.default('test', new Date()); | ||
a.addMetric('ok', new _metrics.Number('OK Metric', 0, 0)); | ||
a.addResult('OK Result', 0, 10, 'r1'); | ||
var formatted = (0, _console2.default)(a, { color: false, minLevel: 0 }); | ||
expect(formatted).toMatch('OK Metric'); | ||
expect(formatted).toMatch('OK Result'); | ||
}); | ||
}); | ||
}); |
@@ -29,3 +29,3 @@ 'use strict'; | ||
a.addResult('ERR', 2, 10, 'm1'); | ||
expect(new _junit2.default().format(a)).toMatchSnapshot(); | ||
expect((0, _junit2.default)(a)).toMatchSnapshot(); | ||
}); | ||
@@ -37,3 +37,3 @@ it('Should output aggregates, marked by level', function () { | ||
a.addMetric('err', new _metrics.Number('ERR metric', 2, 5)); | ||
expect(new _junit2.default().format(a)).toMatchSnapshot(); | ||
expect((0, _junit2.default)(a)).toMatchSnapshot(); | ||
}); | ||
@@ -43,3 +43,3 @@ it('Should output to a file', function () { | ||
var a = new _analysis2.default('test', new Date()); | ||
new _junit2.default(filename).format(a); | ||
(0, _junit2.default)(a, { filename }); | ||
expect(_fs2.default.existsSync(filename)).toEqual(true); | ||
@@ -46,0 +46,0 @@ _fs2.default.unlinkSync(filename); |
@@ -6,2 +6,5 @@ 'use strict'; | ||
}); | ||
exports.default = format; | ||
exports.formatResults = formatResults; | ||
exports.formatMetrics = formatMetrics; | ||
@@ -16,32 +19,51 @@ var _util = require('../util'); | ||
class ConsoleFormatter { | ||
format(analysis) { | ||
const listing = this.buildResults(analysis); | ||
const aggregate = this.buildAggregate(analysis); | ||
const DefaultOptions = { | ||
color: true, | ||
minLevel: 0 | ||
}; | ||
return `Results\n======\n${listing}\n\nMetrics\n=======\n${aggregate}`; | ||
function format(analysis, options = DefaultOptions) { | ||
const listing = formatResults(analysis.results, options); | ||
const aggregate = formatMetrics(analysis.metrics, options); | ||
return `Results\n======\n${listing}\n\nMetrics\n=======\n${aggregate}`; | ||
} | ||
function formatIcon(level) { | ||
switch (level) { | ||
case 2: | ||
return '✖'; | ||
case 1: | ||
return '!'; | ||
case 0: | ||
return '✔'; | ||
} | ||
buildAggregate(analysis) { | ||
const rows = this.buildAggregateRows(analysis); | ||
if (rows.length) { | ||
return (0, _markdownTable2.default)([['Name', 'Value']].concat(rows), { stringLength: _util.stringLength }); | ||
} | ||
return (0, _util.consoleDisplayValue)(1, 'No Results'); | ||
} | ||
function formatValue(level, value, options) { | ||
return options.color ? (0, _util.consoleDisplayValue)(level, value) : value; | ||
} | ||
function formatResults(results, options = DefaultOptions) { | ||
const rows = buildResults(results, options); | ||
if (rows.length) { | ||
return (0, _markdownTable2.default)([['', 'Url']].concat(rows), { stringLength: _util.stringLength }); | ||
} | ||
buildAggregateRows(report) { | ||
return Array.from(report.metrics).map(([name, metric]) => { | ||
return [metric.displayName, (0, _util.consoleDisplayValue)(metric.level, metric.toString())]; | ||
}); | ||
return formatValue(1, 'No results to display', options); | ||
} | ||
function buildResults(results, options) { | ||
return results.filter(res => res.level >= options.minLevel).map(res => [formatIcon(res.level), formatValue(res.level, res.url, options)]); | ||
} | ||
function formatMetrics(metrics, options = DefaultOptions) { | ||
const rows = buildMetrics(metrics, options); | ||
if (rows.length) { | ||
return (0, _markdownTable2.default)([['', 'Name', 'Value']].concat(rows), { stringLength: _util.stringLength }); | ||
} | ||
buildResults(analysis) { | ||
const rows = this.buildResultRows(analysis); | ||
if (rows.length) { | ||
return (0, _markdownTable2.default)([['Url']].concat(rows), { stringLength: _util.stringLength }); | ||
} | ||
return (0, _util.consoleDisplayValue)(1, 'No Results'); | ||
} | ||
buildResultRows(analysis) { | ||
return analysis.results.map(res => [(0, _util.consoleDisplayValue)(res.level, res.url)]); | ||
} | ||
return formatValue(1, 'No metrics to display', options); | ||
} | ||
exports.default = ConsoleFormatter; | ||
function buildMetrics(metrics, options) { | ||
return Array.from(metrics).map(([name, metric]) => { | ||
return [formatIcon(metric.level), metric.displayName, formatValue(metric.level, metric.toString(), options)]; | ||
}); | ||
} |
@@ -6,2 +6,3 @@ 'use strict'; | ||
}); | ||
exports.default = formatJUnit; | ||
@@ -14,32 +15,27 @@ var _factory = require('junit-report-builder/src/factory'); | ||
class JUnitFormatter { | ||
constructor(filename) { | ||
this.filename = filename; | ||
} | ||
format(report) { | ||
var builder = new _factory2.default().newBuilder(); | ||
function formatJUnit(analysis, options = {}) { | ||
var builder = new _factory2.default().newBuilder(); | ||
if (report.metrics.size) { | ||
this.buildAggregates(report, builder); | ||
} | ||
if (report.results.length) { | ||
this.buildResults(report, builder); | ||
} | ||
buildMetrics(analysis.metrics, builder); | ||
if (this.filename) { | ||
builder.writeTo(this.filename); | ||
} else { | ||
return builder.build(); | ||
} | ||
buildResults(analysis.results, builder); | ||
if (options.filename) { | ||
builder.writeTo(options.filename); | ||
} else { | ||
return builder.build(); | ||
} | ||
buildAggregates(analysis, builder) { | ||
const suite = builder.testSuite().name(`Aggregates`); | ||
analysis.metrics.forEach((metric, name) => { | ||
let tc = suite.testCase().className(name).name(metric.displayName).standardOutput(metric.toString()); | ||
switch (metric.level) { | ||
} | ||
function buildResults(results, builder) { | ||
if (results.length) { | ||
const suite = builder.testSuite().name(`Results`); | ||
results.forEach(res => { | ||
let tc = suite.testCase().className(res.url).time(res.time / 1000); | ||
switch (res.level) { | ||
case 2: | ||
tc.failure(); | ||
tc.failure(res.message); | ||
break; | ||
case 1: | ||
tc.error(); | ||
tc.error(res.message); | ||
break; | ||
@@ -49,12 +45,15 @@ } | ||
} | ||
buildResults(analysis, builder) { | ||
const suite = builder.testSuite().name(`Results`); | ||
analysis.results.forEach(res => { | ||
let tc = suite.testCase().className(res.url).time(res.time / 1000); | ||
switch (res.level) { | ||
} | ||
function buildMetrics(metrics, builder) { | ||
if (metrics.size) { | ||
const suite = builder.testSuite().name(`Aggregates`); | ||
metrics.forEach((metric, name) => { | ||
let tc = suite.testCase().className(name).name(metric.displayName).standardOutput(metric.toString()); | ||
switch (metric.level) { | ||
case 2: | ||
tc.failure(res.message); | ||
tc.failure(); | ||
break; | ||
case 1: | ||
tc.error(res.message); | ||
tc.error(); | ||
break; | ||
@@ -64,3 +63,2 @@ } | ||
} | ||
} | ||
exports.default = JUnitFormatter; | ||
} |
'use strict'; | ||
var crawler = require('./crawler'); | ||
crawler.drivers = { | ||
request: require('./driver/request') | ||
var _crawler = require('./crawler'); | ||
var _crawler2 = _interopRequireDefault(_crawler); | ||
var _request = require('./driver/request'); | ||
var _request2 = _interopRequireDefault(_request); | ||
var _metrics = require('./metrics'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
_crawler2.default.drivers = { | ||
request: _request2.default | ||
}; | ||
crawler.metrics = require('./metrics'); | ||
module.exports = crawler; | ||
_crawler2.default.metrics = { | ||
Number: _metrics.Number, | ||
Milliseconds: _metrics.Milliseconds, | ||
Percent: _metrics.Percent | ||
}; | ||
module.exports = _crawler2.default; |
{ | ||
"name": "lastcall-nightcrawler", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
52014
29
1175