-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: code coverage incomplete on redirect (#660)
Co-authored-by: Matt Schile <[email protected]> Co-authored-by: Cacie Prins <[email protected]> Co-authored-by: Matthew Schile <[email protected]>
- Loading branch information
1 parent
3fc4872
commit 443029b
Showing
13 changed files
with
105 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"exclude": [ | ||
"support-utils.js", | ||
"support.js", | ||
"task-utils.js" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["istanbul"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"exclude": [ | ||
"app.js" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# example: redirect | ||
|
||
Tests a frontend app that redirects, through un-instrumented code, back to itself. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// This redirect code needs to be un-instrumented (excluded in .nycrc.json) | ||
// - If the redirect code is instrumented, Cypress would then treat them as different coverage objects and merge the code coverage (not testing what we want). | ||
// - If the redirect code is un-instrumented, Cypress can't tell them apart and will update the existing coverage object to point to the correct one (testing what we want). | ||
|
||
import { returnToApp } from './utils' | ||
|
||
// Timeouts are necessary to allow cypress to pick up the "initial" coverage object and compare it to the existing coverage objects. | ||
new Promise((resolve) => { | ||
if (window.location.port === '1234' && !localStorage.getItem('visited')) { | ||
localStorage.setItem('visited', true) | ||
console.log('Not visited. Redirecting') | ||
setTimeout(() => { | ||
window.location.href = 'http://localhost:1235' | ||
}, 500) | ||
} else if (window.location.port === '1235') { | ||
console.log('Redirecting back.') | ||
setTimeout(() => { | ||
window.location.href = 'http://localhost:1234' | ||
}, 500) | ||
} else { | ||
console.log('Visited'); | ||
setTimeout(() => { | ||
resolve() | ||
}, 500) | ||
} | ||
}).then(() => { | ||
returnToApp() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { defineConfig } = require('cypress') | ||
|
||
module.exports = defineConfig({ | ||
fixturesFolder: false, | ||
e2e: { | ||
setupNodeEvents(on, config) { | ||
require('@cypress/code-coverage/task')(on, config) | ||
return config | ||
}, | ||
baseUrl: 'http://localhost:1234', | ||
env: { | ||
codeCoverage: { | ||
exclude: ['cypress/**/*.*'] | ||
} | ||
}, | ||
chromeWebSecurity: false | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// enables intelligent code completion for Cypress commands | ||
// https://on.cypress.io/intelligent-code-completion | ||
/// <reference types="Cypress" /> | ||
|
||
context('Page test', () => { | ||
it('redirects back to the app', function() { | ||
cy.clearLocalStorage() | ||
cy.visit("http://localhost:1234") | ||
cy.contains("Returned to app") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import '@cypress/code-coverage/support' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<body> | ||
<h2>Test page</h2> | ||
<script src="app.js"></script> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "example-redirect", | ||
"description": "Tests a frontend app that redirects, through un-instrumented code, back to itself.", | ||
"devDependencies": { | ||
"@babel/core": "^7.12.0" | ||
}, | ||
"scripts": { | ||
"cy:run": "cypress run", | ||
"start:app": "parcel serve -p 1234 index.html", | ||
"start:other-app": "parcel serve -p 1235 index.html", | ||
"pretest": "rimraf .nyc_output .cache coverage dist", | ||
"test": "start-test start:app http://localhost:1234 start:other-app http://localhost:1235 cy:run", | ||
"coverage:verify": "npx nyc report --check-coverage true --lines 100", | ||
"coverage:check-files": "check-coverage utils.js && only-covered utils.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const returnToApp = () => { | ||
document.body | ||
.appendChild(document.createTextNode('Returned to app')) | ||
} |