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

Adding code coverage to Cypress (AngularX) #14465

Merged
merged 36 commits into from
Apr 2, 2021
Merged

Adding code coverage to Cypress (AngularX) #14465

merged 36 commits into from
Apr 2, 2021

Conversation

jidma
Copy link
Contributor

@jidma jidma commented Mar 26, 2021

PR description

The PR adds code coverage to Cypress e2e Testing (AngularX front end).

image

When the option is selected, two new npm scripts are added:

The first command allows the user to start the Angular dev server with instrumented code (using istanbul). The dev server is started without ghostMode:

npm run webapp:instrumenter

The second command runs the e2e tests and generates a coverage report:

npm run e2e:cypress:coverage

Cypress will then run against the dev server on port 9000 (started with npm run start-insrt)

image

After the tests are finished, a report is generated under ./coverage/lcov-report/

image

Known issue

If running against a non instrumented code (ie. started with npm run e2e:cypress), Cypress will issue a warning complaining about the code not being instrumented.

Readme added section:

(...)

Code Coverage

When using Cypress with AngularX, you can generate code coverage report by running your dev server with instrumented code:

Start your backend (option -P-webapp to skip compiling the Angular Front):

./mvnw -P-webapp

Start your Angular dev server with instrumented code:

npm run webapp:instrumenter

Start your Cypress end to end testing:

npm run e2e:cypress:coverage

The coverage report is generated under ./coverage/lcov-report/
(...)


Please make sure the below checklist is followed for Pull Requests.

When you are still working on the PR, consider converting it to Draft (bellow reviewers) and adding skip-ci label, you can still see CI build result at your branch.

@CLAassistant
Copy link

CLAassistant commented Mar 26, 2021

CLA assistant check
All committers have signed the CLA.

@jidma jidma changed the title Adding code coverage Adding code coverage to Cypress (AngularX) Mar 26, 2021
@jidma jidma marked this pull request as ready for review March 29, 2021 21:17
Copy link
Member

@mshima mshima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is interesting and you did a great job.
We should make it more modular and simplify it.

generators/app/prompts.js Outdated Show resolved Hide resolved
generators/client/templates/angular/package.json.ejs Outdated Show resolved Hide resolved
generators/common/templates/gitignore.ejs Outdated Show resolved Hide resolved
generators/common/templates/gitignore.ejs Outdated Show resolved Hide resolved
generators/client/templates/angular/package.json.ejs Outdated Show resolved Hide resolved
generators/generator-base.js Outdated Show resolved Hide resolved
@jidma jidma marked this pull request as draft March 30, 2021 13:24
generators/cypress/index.js Outdated Show resolved Hide resolved
generators/cypress/index.js Outdated Show resolved Hide resolved
generators/cypress/index.js Outdated Show resolved Hide resolved
@jidma
Copy link
Contributor Author

jidma commented Mar 30, 2021

@mshima thank you for the code reviews ans suggestions.

@jidma jidma marked this pull request as ready for review March 30, 2021 22:17
@jidma jidma requested a review from mshima March 31, 2021 00:48
generators/cypress/index.js Show resolved Hide resolved
generators/cypress/index.js Outdated Show resolved Hide resolved
generators/cypress/index.js Outdated Show resolved Hide resolved
generators/cypress/index.js Outdated Show resolved Hide resolved
generators/client/templates/common/package.json Outdated Show resolved Hide resolved
generators/common/templates/README.md.ejs Show resolved Hide resolved
generators/common/templates/README.md.ejs Outdated Show resolved Hide resolved
generators/common/templates/README.md.ejs Outdated Show resolved Hide resolved
generators/common/templates/README.md.ejs Outdated Show resolved Hide resolved
generators/common/templates/README.md.ejs Outdated Show resolved Hide resolved
@jidma jidma marked this pull request as draft March 31, 2021 01:00
generators/cypress/index.js Outdated Show resolved Hide resolved
@jidma
Copy link
Contributor Author

jidma commented Mar 31, 2021

@mshima thank once again you for your code review.

After adding and testing your commit, I can't seem to get the instrumented code:

npm run webapp:instrumenter

./mvnw -P-webapp   (because I'm currently on windows and npm run backend:start doesn't work for some reason)

The code doesn't seem to be instrumented:

image

I wanted to let you know first (am I missing something obvious?) before I investigate further later.

Copy link
Member

@mshima mshima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loader is inside if (targetOptions.target === 'serve' || config.watch) but target is build.

Rebase against main for webpack needle support.

generators/cypress/index.js Show resolved Hide resolved
generators/cypress/index.js Outdated Show resolved Hide resolved
generators/cypress/index.js Show resolved Hide resolved
generators/cypress/files.js Outdated Show resolved Hide resolved
generators/cypress/index.js Outdated Show resolved Hide resolved
generators/cypress/index.js Outdated Show resolved Hide resolved
@jidma
Copy link
Contributor Author

jidma commented Mar 31, 2021

@mshima it's working now. Thanks!

jidma and others added 4 commits March 31, 2021 18:35
Co-authored-by: Marcelo Shima <marceloshima@gmail.com>
@jidma jidma marked this pull request as ready for review April 1, 2021 09:55
Copy link
Member

@mshima mshima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@jidma can you add an option to generator/app/index.js please?
See #14465 (comment).

generators/client/templates/common/package.json Outdated Show resolved Hide resolved
generators/client/templates/common/package.json Outdated Show resolved Hide resolved
generators/cypress/index.js Outdated Show resolved Hide resolved
generators/cypress/index.js Outdated Show resolved Hide resolved
Co-authored-by: Marcelo Shima <marceloshima@gmail.com>
@jidma jidma marked this pull request as draft April 1, 2021 18:47
jidma and others added 5 commits April 1, 2021 19:47
Co-authored-by: Marcelo Shima <marceloshima@gmail.com>
Co-authored-by: Marcelo Shima <marceloshima@gmail.com>
Co-authored-by: Marcelo Shima <marceloshima@gmail.com>
@jidma jidma marked this pull request as ready for review April 2, 2021 07:08
generators/generator-base.js Outdated Show resolved Hide resolved
generators/generator-base.js Outdated Show resolved Hide resolved
generators/common/templates/README.md.ejs Outdated Show resolved Hide resolved
@mshima mshima merged commit cc6e770 into jhipster:main Apr 2, 2021
@mshima
Copy link
Member

mshima commented Apr 2, 2021

@jidma congratulations on your first contribution.

@jidma
Copy link
Contributor Author

jidma commented Apr 3, 2021

@jidma congratulations on your first contribution.

@mshima thank you the support!

@pascalgrimaud pascalgrimaud added this to the 7.1.0 milestone May 28, 2021
@marcolafranca
Copy link

marcolafranca commented Jan 6, 2022

Hi,
thank you for your work.
I have a problem when I enable this generator in jhipster wizard.
Jhipster 7.5.0

image

ERROR! Error parsing file src\test\javascript\cypress\plugins\index.ts: SyntaxError: ';' expected. (27:4)
  25 |       return launchOptions;
  26 |     }
> 27 |   });
     |    ^
  28 |
  29 |   on('task', {
  30 |     lighthouse: lighthouse((lighthouseReport) => {

At: 1: // ***********************************************************
2: // This example plugins/index.js can be used to load plugins
3: //
4: // You can change the location of this file or turn off loading
5: // the plugins file with the 'pluginsFile' configuration option.
6: //
7: // You can read more here:
8: // https://on.cypress.io/plugins-guide
9: // ***********************************************************
10:
11: // This function is called when a project is opened or re-opened (e.g. due to
12: // the project's config changing)
13: import fs = require('fs');
14: import { lighthouse, pa11y, prepareAudit } from 'cypress-audit';
15: import ReportGenerator = require('lighthouse/report/generator/report-generator');
16: /**
17:  * @type {Cypress.PluginConfig}
18:  */
19: module.exports = (on, config) => {
20:   // `on` is used to hook into various events Cypress emits
21: // `config` is the resolved Cypress config  on('before:browser:launch', (browser, launchOptions) => {
22:     prepareAudit(launchOptions);
23:     if (browser.name === 'chrome' && browser.isHeadless) {
24:       launchOptions.args.push('--disable-gpu');
25:       return launchOptions;
26:     }
27:   });
28:
29:   on('task', {
30:     lighthouse: lighthouse((lighthouseReport) => {
31:       !fs.existsSync('target/cypress') && fs.mkdirSync('target/cypress', { recursive: true });
32:       fs.writeFileSync('target/cypress/lhreport.html', ReportGenerator.generateReport(lighthouseReport.lhr, 'html'));
33:     }),
34:     pa11y: pa11y(),
35:   });
36:     require('@cypress/code-coverage/task')(on, config);
37:     return config;
38: }
39:
Error: Error parsing file src\test\javascript\cypress\plugins\index.ts: SyntaxError: ';' expected. (27:4)
  25 |       return launchOptions;
  26 |     }
> 27 |   });
     |    ^
  28 |
  29 |   on('task', {
  30 |     lighthouse: lighthouse((lighthouseReport) => {

At: 1: // ***********************************************************
2: // This example plugins/index.js can be used to load plugins
3: //
4: // You can change the location of this file or turn off loading
5: // the plugins file with the 'pluginsFile' configuration option.
6: //
7: // You can read more here:
8: // https://on.cypress.io/plugins-guide
9: // ***********************************************************
10:
11: // This function is called when a project is opened or re-opened (e.g. due to
12: // the project's config changing)
13: import fs = require('fs');
14: import { lighthouse, pa11y, prepareAudit } from 'cypress-audit';
15: import ReportGenerator = require('lighthouse/report/generator/report-generator');
16: /**
17:  * @type {Cypress.PluginConfig}
18:  */
19: module.exports = (on, config) => {
20:   // `on` is used to hook into various events Cypress emits
21: // `config` is the resolved Cypress config  on('before:browser:launch', (browser, launchOptions) => {
22:     prepareAudit(launchOptions);
23:     if (browser.name === 'chrome' && browser.isHeadless) {
24:       launchOptions.args.push('--disable-gpu');
25:       return launchOptions;
26:     }
27:   });
28:
29:   on('task', {
30:     lighthouse: lighthouse((lighthouseReport) => {
31:       !fs.existsSync('target/cypress') && fs.mkdirSync('target/cypress', { recursive: true });
32:       fs.writeFileSync('target/cypress/lhreport.html', ReportGenerator.generateReport(lighthouseReport.lhr, 'html'));
33:     }),
34:     pa11y: pa11y(),
35:   });
36:     require('@cypress/code-coverage/task')(on, config);
37:     return config;
38: }
39:
    at patternSpy.dot (C:\Users\marco_ye\AppData\Roaming\npm\node_modules\generator-jhipster\generators\generator-transforms.js:79:15)
    at async PTransform.transform [as _transform] (C:\Users\marco_ye\AppData\Roaming\npm\node_modules\generator-jhipster\node_modules\p-transform\index.js:127:7)
    at async PTransform.queuedTransform (C:\Users\marco_ye\AppData\Roaming\npm\node_modules\generator-jhipster\node_modules\p-transform\index.js:88:26)
    at async run (C:\Users\marco_ye\AppData\Roaming\npm\node_modules\generator-jhipster\node_modules\p-queue\dist\index.js:163:29)
   create src\main\webapp\i18n\en\logs.json
   create src\test\javascript\cypress\fixtures\integration-test.png

Can you help me? Thank you very much :-)

@jidma jidma deleted the adding-code-coverage branch January 6, 2022 11:02
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

5 participants