diff --git a/lib/core/reporters/raw.js b/lib/core/reporters/raw.js index d6a41ef958..8ceab31d3a 100644 --- a/lib/core/reporters/raw.js +++ b/lib/core/reporters/raw.js @@ -1,8 +1,32 @@ axe.addReporter('raw', function(results, options, callback) { 'use strict'; + if (typeof options === 'function') { callback = options; options = {}; } - callback(results); + + // Guard against tests which don't pass an array as the first param here. + if (!results || !Array.isArray(results)) { + return callback(results); + } + + const transformedResults = results.map(result => { + const transformedResult = { ...result }; + const types = ['passes', 'violations', 'incomplete', 'inapplicable']; + for (const type of types) { + // Some tests don't include all of the types, so we have to guard against that here. + // TODO: ensure tests always use "proper" results to avoid having these hacks in production code paths. + if (transformedResult[type] && Array.isArray(transformedResult[type])) { + transformedResult[type] = transformedResult[type].map(typeResult => ({ + ...typeResult, + node: typeResult.node.toJSON() + })); + } + } + + return transformedResult; + }); + + callback(transformedResults); }); diff --git a/test/core/reporters/raw.js b/test/core/reporters/raw.js index 8f83312e15..8c02777581 100644 --- a/test/core/reporters/raw.js +++ b/test/core/reporters/raw.js @@ -1,18 +1,150 @@ describe('reporters - raw', function() { 'use strict'; - it('should pass through object', function() { + var fixture = document.getElementById('fixture'); + + function createDqElement() { + var node = document.createElement('div'); + fixture.appendChild(node); + return new axe.utils.DqElement(node); + } + + var mockResults; + var orig; + + before(function() { + mockResults = [ + { + id: 'gimmeLabel', + helpUrl: 'things', + description: 'something nifty', + tags: ['tag1'], + result: 'passed', + violations: [], + passes: [ + { + result: 'passed', + any: [ + { + result: true, + data: 'minkey' + } + ], + all: [], + none: [], + node: createDqElement() + } + ] + }, + { + id: 'idkStuff', + description: 'something more nifty', + pageLevel: true, + result: 'failed', + impact: 'cats', + tags: ['tag2'], + passes: [], + violations: [ + { + result: 'failed', + all: [ + { + result: false, + data: 'pillock', + impact: 'cats' + } + ], + any: [], + none: [], + node: createDqElement(), + impact: 'cats' + } + ] + }, + { + id: 'bypass', + description: 'something even more nifty', + tags: ['tag3'], + impact: 'monkeys', + result: 'failed', + passes: [], + violations: [ + { + result: 'failed', + impact: 'monkeys', + none: [ + { + data: 'foon', + impact: 'monkeys', + result: true + } + ], + any: [], + all: [], + node: createDqElement() + } + ] + }, + { + id: 'blinky', + description: 'something awesome', + tags: ['tag4'], + violations: [], + result: 'passed', + passes: [ + { + result: 'passed', + none: [ + { + data: 'clueso', + result: true + } + ], + node: createDqElement() + } + ] + } + ]; + + axe.testUtils.fixtureSetup(); + axe._load({}); - var orig = axe._runRules; + orig = axe._runRules; axe._runRules = function(_, __, cb) { - cb('foo', function noop() {}); + cb(mockResults, function noop() {}); }; + }); + after(function() { + axe._runRules = orig; + fixture.innerHTML = ''; + }); + + it('should pass through object', function(done) { axe.run({ reporter: 'raw' }, function(err, results) { - assert.isNull(err); - assert.equal(results, 'foo'); + if (err) { + return done(err); + } + assert.isTrue(Array.isArray(results)); + done(); }); + }); - axe._runRules = orig; + it('should serialize DqElements (#1195)', function(done) { + axe.run({ reporter: 'raw' }, function(err, results) { + if (err) { + return done(err); + } + + for (var i = 0; i < results.length; i++) { + var result = results[i]; + for (var j = 0; j < result.passes.length; j++) { + var p = result.passes[j]; + assert.notInstanceOf(p.node, axe.utils.DqElement); + } + } + + done(); + }); }); });