|  | 
| 1 | 1 | /// <reference types="cypress" /> | 
| 2 |  | -before(() => { | 
| 3 |  | -  // we need to reset the coverage when running | 
| 4 |  | -  // in the interactive mode, otherwise the counters will | 
| 5 |  | -  // keep increasing every time we rerun the tests | 
| 6 |  | -  cy.task('resetCoverage', { isInteractive: Cypress.config('isInteractive') }) | 
| 7 |  | -}) | 
| 8 | 2 | 
 | 
| 9 |  | -afterEach(() => { | 
| 10 |  | -  // save coverage after each test | 
| 11 |  | -  // because the entire "window" object is about | 
| 12 |  | -  // to be recycled by Cypress before next test | 
| 13 |  | -  cy.window().then(win => { | 
| 14 |  | -    // if application code has been instrumented, the app iframe "window" has an object | 
| 15 |  | -    const applicationSourceCoverage = win.__coverage__ | 
| 16 |  | - | 
| 17 |  | -    if (applicationSourceCoverage) { | 
| 18 |  | -      cy.task('combineCoverage', applicationSourceCoverage) | 
| 19 |  | -    } | 
|  | 3 | +// to disable code coverage commands and save time | 
|  | 4 | +// pass environment variable coverage=false | 
|  | 5 | +//  cypress run --env coverage=false | 
|  | 6 | +// see https://on.cypress.io/environment-variables | 
|  | 7 | +if (Cypress.env('coverage') === false) { | 
|  | 8 | +  console.log('Skipping code coverage hooks') | 
|  | 9 | +} else { | 
|  | 10 | +  before(() => { | 
|  | 11 | +    // we need to reset the coverage when running | 
|  | 12 | +    // in the interactive mode, otherwise the counters will | 
|  | 13 | +    // keep increasing every time we rerun the tests | 
|  | 14 | +    cy.task('resetCoverage', { isInteractive: Cypress.config('isInteractive') }) | 
| 20 | 15 |   }) | 
| 21 |  | -}) | 
| 22 | 16 | 
 | 
| 23 |  | -after(() => { | 
| 24 |  | -  // there might be server-side code coverage information | 
| 25 |  | -  // we should grab it once after all tests finish | 
| 26 |  | -  const baseUrl = Cypress.config('baseUrl') || cy.state('window').origin | 
| 27 |  | -  const runningEndToEndTests = baseUrl !== Cypress.config('proxyUrl') | 
| 28 |  | -  if (runningEndToEndTests) { | 
| 29 |  | -    // we can only request server-side code coverage | 
| 30 |  | -    // if we are running end-to-end tests, | 
| 31 |  | -    // otherwise where do we send the request? | 
| 32 |  | -    const url = Cypress._.get( | 
| 33 |  | -      Cypress.env('codeCoverage'), | 
| 34 |  | -      'url', | 
| 35 |  | -      '/__coverage__' | 
| 36 |  | -    ) | 
| 37 |  | -    cy.request({ | 
| 38 |  | -      url, | 
| 39 |  | -      log: false, | 
| 40 |  | -      failOnStatusCode: false | 
|  | 17 | +  afterEach(() => { | 
|  | 18 | +    // save coverage after each test | 
|  | 19 | +    // because the entire "window" object is about | 
|  | 20 | +    // to be recycled by Cypress before next test | 
|  | 21 | +    cy.window().then(win => { | 
|  | 22 | +      // if application code has been instrumented, the app iframe "window" has an object | 
|  | 23 | +      const applicationSourceCoverage = win.__coverage__ | 
|  | 24 | + | 
|  | 25 | +      if (applicationSourceCoverage) { | 
|  | 26 | +        cy.task('combineCoverage', applicationSourceCoverage) | 
|  | 27 | +      } | 
| 41 | 28 |     }) | 
| 42 |  | -      .then(r => Cypress._.get(r, 'body.coverage', null), { log: false }) | 
| 43 |  | -      .then(coverage => { | 
| 44 |  | -        if (!coverage) { | 
| 45 |  | -          // we did not get code coverage - this is the | 
| 46 |  | -          // original failed request | 
| 47 |  | -          return | 
| 48 |  | -        } | 
| 49 |  | -        cy.task('combineCoverage', coverage) | 
|  | 29 | +  }) | 
|  | 30 | + | 
|  | 31 | +  after(() => { | 
|  | 32 | +    // there might be server-side code coverage information | 
|  | 33 | +    // we should grab it once after all tests finish | 
|  | 34 | +    const baseUrl = Cypress.config('baseUrl') || cy.state('window').origin | 
|  | 35 | +    const runningEndToEndTests = baseUrl !== Cypress.config('proxyUrl') | 
|  | 36 | +    if (runningEndToEndTests) { | 
|  | 37 | +      // we can only request server-side code coverage | 
|  | 38 | +      // if we are running end-to-end tests, | 
|  | 39 | +      // otherwise where do we send the request? | 
|  | 40 | +      const url = Cypress._.get( | 
|  | 41 | +        Cypress.env('codeCoverage'), | 
|  | 42 | +        'url', | 
|  | 43 | +        '/__coverage__' | 
|  | 44 | +      ) | 
|  | 45 | +      cy.request({ | 
|  | 46 | +        url, | 
|  | 47 | +        log: false, | 
|  | 48 | +        failOnStatusCode: false | 
| 50 | 49 |       }) | 
| 51 |  | -  } | 
|  | 50 | +        .then(r => Cypress._.get(r, 'body.coverage', null), { log: false }) | 
|  | 51 | +        .then(coverage => { | 
|  | 52 | +          if (!coverage) { | 
|  | 53 | +            // we did not get code coverage - this is the | 
|  | 54 | +            // original failed request | 
|  | 55 | +            return | 
|  | 56 | +          } | 
|  | 57 | +          cy.task('combineCoverage', coverage) | 
|  | 58 | +        }) | 
|  | 59 | +    } | 
| 52 | 60 | 
 | 
| 53 |  | -  // collect and merge frontend coverage | 
| 54 |  | -  const specFolder = Cypress.config('integrationFolder') | 
| 55 |  | -  const supportFolder = Cypress.config('supportFolder') | 
|  | 61 | +    // collect and merge frontend coverage | 
|  | 62 | +    const specFolder = Cypress.config('integrationFolder') | 
|  | 63 | +    const supportFolder = Cypress.config('supportFolder') | 
| 56 | 64 | 
 | 
| 57 |  | -  // if spec bundle has been instrumented (using Cypress preprocessor) | 
| 58 |  | -  // then we will have unit test coverage | 
| 59 |  | -  // NOTE: spec iframe is NOT reset between the tests, so we can grab | 
| 60 |  | -  // the coverage information only once after all tests have finished | 
| 61 |  | -  const unitTestCoverage = window.__coverage__ | 
| 62 |  | -  if (unitTestCoverage) { | 
| 63 |  | -    // remove coverage for the spec files themselves, | 
| 64 |  | -    // only keep "external" application source file coverage | 
| 65 |  | -    const coverage = Cypress._.omitBy( | 
| 66 |  | -      window.__coverage__, | 
| 67 |  | -      (fileCoverage, filename) => | 
| 68 |  | -        filename.startsWith(specFolder) || filename.startsWith(supportFolder) | 
| 69 |  | -    ) | 
| 70 |  | -    cy.task('combineCoverage', coverage) | 
| 71 |  | -  } | 
|  | 65 | +    // if spec bundle has been instrumented (using Cypress preprocessor) | 
|  | 66 | +    // then we will have unit test coverage | 
|  | 67 | +    // NOTE: spec iframe is NOT reset between the tests, so we can grab | 
|  | 68 | +    // the coverage information only once after all tests have finished | 
|  | 69 | +    const unitTestCoverage = window.__coverage__ | 
|  | 70 | +    if (unitTestCoverage) { | 
|  | 71 | +      // remove coverage for the spec files themselves, | 
|  | 72 | +      // only keep "external" application source file coverage | 
|  | 73 | +      const coverage = Cypress._.omitBy( | 
|  | 74 | +        window.__coverage__, | 
|  | 75 | +        (fileCoverage, filename) => | 
|  | 76 | +          filename.startsWith(specFolder) || filename.startsWith(supportFolder) | 
|  | 77 | +      ) | 
|  | 78 | +      cy.task('combineCoverage', coverage) | 
|  | 79 | +    } | 
| 72 | 80 | 
 | 
| 73 |  | -  // when all tests finish, lets generate the coverage report | 
| 74 |  | -  cy.task('coverageReport') | 
| 75 |  | -}) | 
|  | 81 | +    // when all tests finish, lets generate the coverage report | 
|  | 82 | +    cy.task('coverageReport') | 
|  | 83 | +  }) | 
|  | 84 | +} | 
0 commit comments