Skip to content

Commit

Permalink
refactoring instrument
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Nov 13, 2024
1 parent bbbbf8c commit a2be40b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
2 changes: 2 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export default defineConfig({
coverage: {
// set to true to hide the messages in the Command Log
quiet: false,
// intercept and instrument scripts matching these URLs
instrument: '**/calculator/**/*.js',
},
},
// We've imported your old cypress plugins here.
Expand Down
52 changes: 29 additions & 23 deletions cypress/e2e/calculator.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,41 @@ const baseUrl = Cypress.config('baseUrl')
// @ts-ignore
const proxyServer = Cypress.config('proxyServer') + '/'

const instrumentScripts = Cypress.env('coverage')?.instrument

it(
'collects code coverage on the fly',
{ viewportWidth: 1200, viewportHeight: 1000, baseUrl: null },
() => {
cy.intercept(
{
method: 'GET',
resourceType: 'script',
},
(req) => {
// TODO: remove caching headers for now
// @ts-ignore
req.continue((res) => {
const relativeUrl = req.url
if (instrumentScripts) {
cy.intercept(
{
method: 'GET',
resourceType: 'script',
url: instrumentScripts,
},
(req) => {
// TODO: remove caching headers for now
// @ts-ignore
req.continue((res) => {
const relativeUrl = req.url
// @ts-ignore
.replace(baseUrl, '')
.replace(proxyServer, '')
// console.log('instrumenting', relativeUrl)

// @ts-ignore
.replace(baseUrl, '')
.replace(proxyServer, '')
console.log('instrumenting', relativeUrl)
const instrumented = instrumenter.instrumentSync(
res.body,
relativeUrl,
)
res.body = instrumented
return res
})
},
)
}

// @ts-ignore
const instrumented = instrumenter.instrumentSync(
res.body,
relativeUrl,
)
res.body = instrumented
return res
})
},
)
cy.visit('cypress/calculator/index.html')
// after instrumenting the coverage should be collected in the window object
// under window.__coverage__ key
Expand Down

0 comments on commit a2be40b

Please sign in to comment.