diff --git a/lib/runner/util.js b/lib/runner/util.js index e195a1e5a..6fb7c0e2b 100644 --- a/lib/runner/util.js +++ b/lib/runner/util.js @@ -315,12 +315,18 @@ module.exports = { * @return {Any} - The data for the iteration */ getIterationData (data, iteration) { - // if iteration has a corresponding data element use that + if (!Array.isArray(data) || !data.length) { + return; + } + + if (iteration < 0) { + return data[0]; + } + if (iteration < data.length) { return data[iteration]; } - // otherwise use the last data element return data[data.length - 1]; }, diff --git a/test/unit/runner-util.test.js b/test/unit/runner-util.test.js index a1c0c7e31..d42418086 100644 --- a/test/unit/runner-util.test.js +++ b/test/unit/runner-util.test.js @@ -131,6 +131,16 @@ describe('runner util', function () { }); describe('.getIterationData', function () { + it('should handle negative iteration index', function () { + var data = [ + { value: 'first' }, + { value: 'second' } + ], + result = runnerUtil.getIterationData(data, -1); + + expect(result).to.equal(data[0]); + }); + it('should return data for the specified iteration', function () { var data = [ { iteration: 0, value: 'first' },