Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coffee-coverage and CoffeeScript 1.11.x (ES2015 modules) #82

Open
wcjohnson opened this issue Oct 29, 2016 · 6 comments
Open

coffee-coverage and CoffeeScript 1.11.x (ES2015 modules) #82

wcjohnson opened this issue Oct 29, 2016 · 6 comments

Comments

@wcjohnson
Copy link

With the release of 1.11, CoffeeScript has begun moving toward the ES2015+ standard. In particular, CoffeeScript now supports ES2015 import/export directives.

These do not play well with coffee-coverage right now, producing errors like this:

> mocha --recursive --compilers coffee:coffee-script/register --require coffee-coverage/register-istanbul


/Users/wcj/dev/ormojo/node_modules/ormojo/node_modules/coffee-coverage/lib/coffeeCoverage.js:321
      throw new CoverageError("Could not compile " + fileName + " after instrumenting: " + err.stack);
      ^
CoverageError: Could not compile /Users/wcj/dev/ormojo/node_modules/ormojo/src/Backend.coffee after instrumenting: SyntaxError: export statements must be at top-level scope

Presumably, export and import statements should simply be ignored when instrumenting, as they are meant to be declarative.

@Florian-R
Copy link

Florian-R commented Nov 18, 2016

I'm currently having troubles with this too. I've toyed with this a bit, this error is caused because the code is instrumented by default with the bare options set to false by default.

It's still unclear to me how we can set this options when using coffee-coverage/register-istanbul.

Anyway, code compiled with bare blow mocha anyway, as mocha doesn't understand the import statement, and we can't chain coffee-coverage/register-istanbul or coffee-script/register with babel-register.

Out of curiosity, @wcjohnson did you find a solution which doesn't involve to compile your sources before running the tests?

@wcjohnson
Copy link
Author

@Florian-R

I'm afraid not. I've just been running coverage on the transpiled code, then mentally translating it back to the CoffeeScript. It's workable but ugly.

@Florian-R
Copy link

Florian-R commented Nov 24, 2016

@wcjohnson I've achieved something that does the job. See chaplinjs/chaplin#895. This one use grunt but I think you could achieve something similar from the CLI, maybe by wrapping the instrument part in a script.

I'd like to get rid of having to define the global variable manually, but this breaks with babel-register. Here, the coverageVar is set as a global if not defined, and this don't play well with use strict.

@jwalton will you be OK with a PR that switch to something like ?

init = """
  if (typeof #{@coverageVar} === 'undefined') global.#{@coverageVar} = {};

This is a simple example. This will need some tweaks to catch the correct global for browsers.

@jwalton
Copy link
Contributor

jwalton commented Feb 23, 2017

@Florian-R Yeah, if you can do this correctly for browsers, I'm happy with it.

If you want to try compiling with bare: true you need to write a custom loader to use in place of coffee-coverage/register-istanbul.

@wcjohnson coffee-script compiles this to a JS file with imports and exports in it, yes? But node 6/7 don't support import and export natively, so how are you even going to run this? Does it go through Babel afterwards? If you have a sample project, I'd be interested in seeing it.

@wcjohnson
Copy link
Author

@jwalton

Yes, coffee-script passes the statement through, and indeed Node does not handle ES2015 modules (and won't for awhile, according to the word on the street) -- but I'm writing isomorphic code and on the browser side things are moving more quickly.

There is a Babel step at the end of my build chain that generates a CommonJS build and an ES2015 build. The tests are run in Node under the CommonJS build.

This project uses my current best build and coverage chain:
https://github.com/wcjohnson/redux-components

You will need to have nyc installed; then npm run coverage should work.

As I said above, since moving away from coffee-coverage I'm stuck just transpiling and running coverage on the resulting JS, then "mentally translating" the coverage reports back to CS. I've tried source maps and they just don't work right. It would be nice to run through coffee-coverage for proper instrumentation, then through Babel.

@jwalton
Copy link
Contributor

jwalton commented Feb 26, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants