jeffrifwald / babel-istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
144 stars 23 forks source link

code transformation fails #44

Closed silkentrance closed 8 years ago

silkentrance commented 8 years ago

Given the following source

function ProtoError(message) {
    this._base = new Error(message);
}

ProtoError.prototype = new Error();
Object.setPrototypeOf(ProtoError, Error);

export default class EsError extends ProtoError
{
    constructor(message)
    {   
        super(message);
    }   

    get message()
    {   
        return this._base.message;
    }   

    toString()
    {   
        return '[' + this.constructor.name + ': ' + this.message + ']'; 
    }   
}

and trying to generate coverage information for it using the following dummy test case

import EsError from 'error';

babel-istanbul will fail and output the following debug info/stack trace

gathering coverage data...
Transformation error; return original code
{ [SyntaxError: /var/workspaces/coldrye/coldrye-es/esbases/esbases/src/error.es: super() is only allowed in a derived constructor]
  loc: Position { line: 15, column: 8 },
  _babel: true,
  codeFrame: '\u001b[0m  13 |     constructor\u001b[34m\u001b[1m(\u001b[22m\u001b[39mmessage\u001b[34m\u001b[1m)\u001b[22m\u001b[39m\n  14 |     \u001b[32m{\u001b[39m\n> 15 |         \u001b[36msuper\u001b[39m\u001b[34m\u001b[1m(\u001b[22m\u001b[39mmessage\u001b[34m\u001b[1m)\u001b[22m\u001b[39m\u001b[1m;\u001b[22m\n     |         ^\n  16 |     \u001b[32m}\u001b[39m\n  17 | \n  18 |     get message\u001b[34m\u001b[1m(\u001b[22m\u001b[39m\u001b[34m\u001b[1m)\u001b[22m\u001b[39m\u001b[0m' }
=============================================================================
Writing coverage object [/var/workspaces/coldrye/coldrye-es/esbases/esbases/build/cover/coverage.json]
Writing coverage reports at [/var/workspaces/coldrye/coldrye-es/esbases/esbases/build/cover]
=============================================================================

=============================== Coverage summary ===============================
Statements   : 35.19% ( 19/54 )
Branches     : 8.33% ( 1/12 )
Functions    : 0% ( 0/15 )
Lines        : 29.79% ( 14/47 )
================================================================================
/var/workspaces/coldrye/coldrye-es/esbases/esbases/src/error.es:11
export default class EsError
^^^^^^

SyntaxError: Unexpected reserved word
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:414:25)
    at Object.Module._extensions..js [as .es] (module.js:442:10)
    at Object.Module._extensions.(anonymous function) [as .es] (/usr/lib/node_modules/babel-istanbul/lib/hook.js:109:37)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/var/workspaces/coldrye/coldrye-es/esbases/esbases/build/test/covercurried-test.js:1:208)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at /usr/lib/node_modules/mocha/lib/mocha.js:216:27
    at Array.forEach (native)
    at Mocha.loadFiles (/usr/lib/node_modules/mocha/lib/mocha.js:213:14)
    at Mocha.run (/usr/lib/node_modules/mocha/lib/mocha.js:453:10)
    at Object.<anonymous> (/usr/lib/node_modules/mocha/bin/_mocha:393:18)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at runFn (/usr/lib/node_modules/babel-istanbul/lib/command/common/run-with-cover.js:125:16)
    at /usr/lib/node_modules/babel-istanbul/lib/command/common/run-with-cover.js:258:17
    at /usr/lib/node_modules/babel-istanbul/lib/util/file-matcher.js:68:16
    at /usr/lib/node_modules/babel-istanbul/node_modules/async/lib/async.js:52:16
    at /usr/lib/node_modules/babel-istanbul/node_modules/async/lib/async.js:361:13
    at /usr/lib/node_modules/babel-istanbul/node_modules/async/lib/async.js:52:16
    at done (/usr/lib/node_modules/babel-istanbul/node_modules/async/lib/async.js:246:17)
    at /usr/lib/node_modules/babel-istanbul/node_modules/async/lib/async.js:44:16
    at /usr/lib/node_modules/babel-istanbul/node_modules/async/lib/async.js:358:17
    at LOOP (fs.js:1534:14)
    at doNTCallback0 (node.js:419:9)
    at process._tickCallback (node.js:348:13)

As I am currently trying to migrate my existing projects over to babel 6.x so that might be a problem here?

silkentrance commented 8 years ago

using babel 6.3.x here, while babel-istanbul uses babel 6.1.x

silkentrance commented 8 years ago

nah, I think I found the reason for that. I am creating a dummy test file that will import all existing sources so that the coverage data is correct. After that I removed that file, babel-istanbul will run just fine. Investigating further.

ghost commented 8 years ago

@silkentrance I got the same issue. Can you elaborate how you solved it?

silkentrance commented 8 years ago

@Moezalez it has been some time now so I will try to remember. I think that it finally boiled down to some problem with babel-traverse. Installing a different version than that which the original babel compiler required did solve that problem for me. After a while, i.e. several babel releases later, the problem was gone.

That process included also cleaning out the local node_modules and the globally installed ones so that all dependencies get reinstalled.