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

Demo getting coverage for JavaScript code #2264

Closed
wants to merge 1 commit into from
Closed

Conversation

benthorner
Copy link
Contributor

@benthorner benthorner commented Oct 26, 2020

The following steps show how we can get an estimate of coverage for
JavaScript (JS) code. Unfortunately it's not possible to do this
easily due to the fact that we execute all the tests for JavaScript
via Ruby code, in order to accommodate the Rails Asset Pipeline.

Instrument

First, we need to manually instrument all the JS code in the app,
so that it will log coverage stats when we run it.

Note that we can't instrument the top-level application.js file,
as this causes the test runner to go into an infinite loop (for
some reason!). Hopefully we have very little JS code in this file,
or it should be easy to move it elsewhere temporarily.

mv app/assets/javascripts{,.bak}

npx istanbul instrument app/assets/javascripts.bak -o app/assets/javascripts

mv app/assets/javascripts.bak/application.js app/assets/javascripts/

Run tests

We need to run these in the browser, so that we can extract the
coverage data later on.

govuk-docker run -p 8888:8888 finder-frontend-lite bundle exec rake jasmine

Now go to localhost:8888 and watch the tests run to completion.

Dump stats

Run the following in your browser console, and manually copy the
output into a JSON file e.g. "coverage/coverage.json".

JSON.stringify(window.__coverage__)

Generate report

Run the following to generate final report in "coverage/lcov-report/".

npx istanbul report --include coverage/coverage.json

The following steps show how we can get an estimate of coverage for
JavaScript (JS) code. Unfortunately it's not possible to do this
easily due to the fact that we execute all the tests for JavaScript
via Ruby code, in order to accommodate the Rails Asset Pipeline.

Instrument
==========

First, we need to manually instrument all the JS code in the app,
so that it will log coverage stats when we run it.

Note that we can't instrument the top-level application.js file,
as this causes the test runner to go into an infinite loop (for
some reason!). Hopefully we have very little JS code in this file,
or it should be easy to move it elsewhere temporarily.

```
mv app/assets/javascripts{,.bak}

npx istanbul instrument app/assets/javascripts.bak
app/assets/javascripts

mv app/assets/javascripts.bak/application.js
app/assets/javascripts/
```

Run tests
=========

We need to run these in the browser, so that we can extract the
coverage data later on.

govuk-docker run -p 8888:8888 finder-frontend-lite bundle exec rake
jasmine

Now go to localhost:8888 and watch the tests run to completion.

Dump stats
==========

Run the following in your browser console, and manually copy the
output into a JSON file e.g. "coverage/coverage.json".

```
JSON.stringify(window.__coverage__)
```

Generate report
===============

Run the following to generate final report in "coverage/".

```
npx istanbul report --include coverage/coverage.json
@bevanloon bevanloon temporarily deployed to finder-front-demo-js-co-fvgqws October 26, 2020 11:17 Inactive
@benthorner
Copy link
Contributor Author

Based on the method documented here, this repo has ~90% JavaScript code coverage.

screencapture-file-Users-benthorner-govuk-finder-frontend-coverage-lcov-report-index-html-2020-10-26-11_18_40

@benthorner benthorner closed this Oct 26, 2020
benthorner pushed a commit to alphagov/collections that referenced this pull request Oct 26, 2020
benthorner pushed a commit to alphagov/frontend that referenced this pull request Oct 26, 2020
Following the method in [1]. Note that I also had to preserve the
original (non-code) content of "all.js" to get the tests to run.

[1]: alphagov/finder-frontend#2264
benthorner pushed a commit to alphagov/content-publisher that referenced this pull request Oct 26, 2020
Following the method in [1]. Note that I also had to rescue to the
comment at the top of "reorderable-list.js" for the tests to all pass.

[1]: alphagov/finder-frontend#2264
benthorner pushed a commit to alphagov/manuals-frontend that referenced this pull request Oct 26, 2020
benthorner pushed a commit to alphagov/publisher that referenced this pull request Oct 26, 2020
benthorner pushed a commit to alphagov/service-manual-frontend that referenced this pull request Oct 26, 2020
benthorner pushed a commit to alphagov/service-manual-publisher that referenced this pull request Oct 26, 2020
benthorner pushed a commit to alphagov/transition that referenced this pull request Oct 26, 2020
@barrucadu barrucadu deleted the demo-js-coverage branch June 14, 2021 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants