From 45ad3e124b9bbdd80fdfe1ae5ce7a2ae0cace9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kan=20Canberger?= Date: Mon, 26 Oct 2015 08:56:26 +0100 Subject: [PATCH 1/2] Add test to prove that issue #470 is correct, i.e. cannot handle for-of I got numbers in test/coverage.js (percent, sloc & hits) by first adding another for-in loop into partial.js below the existing one: for (var o in j) { ++l; } When the test worked and 100% code coverage was achieved, the loop was changed to for (var o of j) --- test/coverage.js | 6 +++--- test/coverage/partial.js | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/test/coverage.js b/test/coverage.js index ad0e41fd..2019d27f 100755 --- a/test/coverage.js +++ b/test/coverage.js @@ -49,10 +49,10 @@ describe('Coverage', function () { Test.method(1, 2, 3); var cov = Lab.coverage.analyze({ coveragePath: Path.join(__dirname, 'coverage/partial') }); - expect(Math.floor(cov.percent)).to.equal(59); - expect(cov.sloc).to.equal(47); + expect(Math.floor(cov.percent)).to.equal(62); + expect(cov.sloc).to.equal(51); expect(cov.misses).to.equal(19); - expect(cov.hits).to.equal(28); + expect(cov.hits).to.equal(32); done(); }); diff --git a/test/coverage/partial.js b/test/coverage/partial.js index d1d19fe0..38786ba3 100755 --- a/test/coverage/partial.js +++ b/test/coverage/partial.js @@ -59,6 +59,10 @@ exports.method = function (a, b, c) { for (var k in j) { ++l; } + + for (var o of j) { + ++l; + } var m = (a ? b : c) || (c ? d : e); From 32e9c1c1d0e6cd6416391fcad3247ac6b6a1fe80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kan=20Canberger?= Date: Mon, 26 Oct 2015 09:18:51 +0100 Subject: [PATCH 2/2] Do not add tracking code before "var" in "for(var x of xs)" This occurs when this is true: node.type == "VariableDeclaration" node.parent.type == "ForOfStatement" Fixes issue #470 --- lib/coverage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/coverage.js b/lib/coverage.js index 5a7de594..6f5c7f79 100755 --- a/lib/coverage.js +++ b/lib/coverage.js @@ -162,7 +162,7 @@ internals.instrument = function (filename) { ]; if (trackedTypes.indexOf(node.type) !== -1 && - (node.type !== 'VariableDeclaration' || (node.parent.type !== 'ForStatement' && node.parent.type !== 'ForInStatement')) && + (node.type !== 'VariableDeclaration' || (node.parent.type !== 'ForStatement' && node.parent.type !== 'ForInStatement' && node.parent.type !== 'ForOfStatement')) && (node.type !== 'ExpressionStatement' || node.expression.value !== 'use strict') && node.parent.type !== 'LabeledStatement') {