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

useage with mocha --watch #310

Closed
graingert opened this issue Jul 11, 2016 · 14 comments
Closed

useage with mocha --watch #310

graingert opened this issue Jul 11, 2016 · 14 comments

Comments

@graingert
Copy link

How can I use nyc with mocha --watch?

When run under nyc, mocha --watch doesn't quit and so I don't ever get to see the coverage.

@jamestalmage
Copy link
Member

The coverage data gets written on process exit, so basically it's not usable for --watch mode.

Coverage of a watch session is fairly useless anyways.

@graingert
Copy link
Author

I figured that, but it works with karma with the coverage plugin because karma is managing the coverage status.

Is it possible to create a JS API for getting and resetting coverage snapshots, I could then call it in my mocha (or other test framework) afterAll hooks

@graingert
Copy link
Author

I'm after a new coverage result each watch cycle, not the union of all the coverages

@jamestalmage
Copy link
Member

Actually, it is because karma is managing the file watching and full test lifecycle. It knows when to delete coverage and start over at the beginning of a run, when to combine the collected coverage at the end of the run, etc.

NYC doesn't have that kind of insight into the underlying test lifecycle of the bins it launches.

@bcoe
Copy link
Member

bcoe commented Jul 12, 2016

Might make sense as another open source tool you run in parallel with karma and nyc. I think it might beyond our scope -- we try to be fairly tool agnostic.

@jamestalmage
Copy link
Member

I think this goes back to splitting up NYC into reusable parts. Specifically, gathering reports and combining them should be separate from the spawn-wrap instrumentation/write-on-exit bit.

With just those two things split out it would be pretty easy to write a --coverage flag into mocha that was lifecycle aware. AVA would probably be interested in such a feature as well.

@nicosommi
Copy link

@graingert I ran into this as well.

I'm using Visual Studio Code, and I installed an extension called Save And Run with the following settings:

  "saveAndRun": {
      "commands": [
          {
              "match": "\\.js$",
              "cmd": "npm test"
          }
      ]
    }

Maybe you can do the same on your IDE with a similar plugin (or a set of plugins).
Now, each time I save a file, the test is executed along with the coverage report that you configured on your npm task.

Actually this approach has one advantage since you can test specifically what you just modified relatively easily.

With Atom, you can use
https://atom.io/packages/on-save

@tconroy
Copy link

tconroy commented Feb 6, 2017

curious if anyone has found a good work around / solution for this. Would love to have my coverage update in real-time via mocha --watch.

@novemberborn
Copy link
Contributor

I've been thinking about this in the context of AVA. I think the test runner needs to provide the coverage reports.

In AVA's case, since each test file runs in a new process, and since nyc combines multiple coverage reports into the final report, AVA would have to remove reports for previous test runs.

Mocha would have to do something similar to clean up previous runs, but that's probably harder since there's only a single process.

@laggingreflex
Copy link

I've written mochista that does exactly this.

@bcoe
Copy link
Member

bcoe commented Mar 22, 2017

@laggingreflex this is really cool 👍 any interest in potentially merging this into the istanbuljs organization?

Should this be part of nyc, should it remain separate? (my leaning is having it as a separate tool).

@bcoe
Copy link
Member

bcoe commented May 14, 2017

@laggingreflex @graingert I think mochista outlines a pretty good approach -- @laggingreflex would happily add a tutorial to https://istanbul.js.org/ showing off your library, would love a pull request if you have the time.

@bcoe bcoe closed this as completed May 14, 2017
@cklanac
Copy link

cklanac commented Aug 9, 2017

@laggingreflex Nice work on mochista, maybe I'll give it a try. For anyone else who's looking for any easy solution, the following is working well for me.

nodemon --exec nyc --reporter=lcov --reporter=text mocha

It's a full restart of nyc and mocha so it runs a little slower than mocha --watch but it works great with VS Code's Coverage Gutter extension

@bcoe
Copy link
Member

bcoe commented Aug 9, 2017

@cklanac that's really slick 👍 if you have a few moments, and feel like making an OSS contribution to the Istanbul project, it would be awesome to add some documentation regarding this approach here:

https://github.com/istanbuljs/istanbuljs.github.io

Maybe a tutorial along the lines of "Watching for Test Coverage Changes with Nodemon"?

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

No branches or pull requests

8 participants