Constellation / ibrik

CoffeeScript meets Istanbul - code coverage tool for CoffeeScript
BSD 2-Clause "Simplified" License
79 stars 32 forks source link

"when" clauses in for loops have incorrect line/column branch mapping #41

Open samuelhorwitz opened 8 years ago

samuelhorwitz commented 8 years ago

The result is a very hard to track down branch coverage shortcoming with no visual indication in the reports. Once stepping through the coverage report JSON, you find a line 0/column 0 branch.

Test Case

Code

window.foo = (arr) ->
    for val in arr when val?
        window.handleExisting val

    return

window.handleExisting = (i) ->
    return i + 1

Test

describe 'Foo', ->
    it 'window.foo', ->
        spyOn window, 'handleExisting'

        window.foo [1, 2, 3]

        expect(window.handleExisting).toHaveBeenCalledWith 1
        expect(window.handleExisting).toHaveBeenCalledWith 2
        expect(window.handleExisting).toHaveBeenCalledWith 3

    it 'window.handleExisting', ->
        expect(window.handleExisting 1).toEqual 2

The coverage reports incomplete branch coverage. In this simple code, it is obvious why: when val? never gets hit with an undefined value. However, this does not get indicated in the coverage report.

Further investigation shows the following JSON (relevant parts only):

{
    "branchMap": {
        "1":{
            "line":0,
            "type":"if",
            "locations":[
                {
                    "start":{
                        "line":0,
                        "column":0
                    },
                    "end":{
                        "line":0,
                        "column":0
                    }
                },
                {
                    "start":{
                        "line":0,
                        "column":0
                    },
                    "end":{
                        "line":0,
                        "column":0
                    }
                }
            ]
        }
    },
    "b": {
        "1": [3,0]
    }
}

Obviously, this branching is not occurring on the non-existent line 0.

To other people finding this issue and need a quick way to track down the uncovered branch: