diff --git a/.circleci/config.yml b/.circleci/config.yml
index 15d2dd6d..7573900f 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -11,7 +11,7 @@ jobs:
docker:
- image: cimg/node:20.8.0
environment:
- # we don't need Cypress to check code styl
+ # we don't need Cypress to check code style
CYPRESS_INSTALL_BINARY: '0'
steps:
- attach_workspace:
@@ -158,6 +158,7 @@ workflows:
- ts-example
- unit-tests-js
- use-webpack
+ - redirect
- windows_test
- publish:
filters:
@@ -184,4 +185,5 @@ workflows:
- test-ts-example
- test-unit-tests-js
- test-use-webpack
+ - test-redirect
- windows_test
diff --git a/.nycrc.json b/.nycrc.json
index 6f8691fd..26e2323b 100644
--- a/.nycrc.json
+++ b/.nycrc.json
@@ -1,6 +1,6 @@
{
"exclude": [
- "support-utils.js",
+ "support.js",
"task-utils.js"
]
}
diff --git a/support.js b/support.js
index 25249f59..bf90a390 100644
--- a/support.js
+++ b/support.js
@@ -72,13 +72,15 @@ const registerHooks = () => {
return
}
- if (
- Cypress._.find(windowCoverageObjects, {
- coverage: applicationSourceCoverage
- })
- ) {
+ const existingCoverage = Cypress._.find(windowCoverageObjects, {
+ coverage: applicationSourceCoverage
+ })
+ if (existingCoverage) {
// this application code coverage object is already known
- // which can happen when combining `window:load` and `before` callbacks
+ // which can happen when combining `window:load` and `before` callbacks,
+ // it can also happen when the user navigates away and then returns to the page
+ // in which case we need to use new applicationSourceCoverage, because the old will not be updated anymore.
+ existingCoverage.coverage = applicationSourceCoverage
return
}
diff --git a/test-apps/redirect/.babelrc b/test-apps/redirect/.babelrc
new file mode 100644
index 00000000..7a016cf8
--- /dev/null
+++ b/test-apps/redirect/.babelrc
@@ -0,0 +1,3 @@
+{
+ "plugins": ["istanbul"]
+}
diff --git a/test-apps/redirect/.nycrc.json b/test-apps/redirect/.nycrc.json
new file mode 100644
index 00000000..93a10fdb
--- /dev/null
+++ b/test-apps/redirect/.nycrc.json
@@ -0,0 +1,5 @@
+{
+ "exclude": [
+ "app.js"
+ ]
+}
diff --git a/test-apps/redirect/README.md b/test-apps/redirect/README.md
new file mode 100644
index 00000000..9b6265b7
--- /dev/null
+++ b/test-apps/redirect/README.md
@@ -0,0 +1,3 @@
+# example: redirect
+
+Tests a frontend app that redirects, through un-instrumented code, back to itself.
diff --git a/test-apps/redirect/app.js b/test-apps/redirect/app.js
new file mode 100644
index 00000000..f80f3106
--- /dev/null
+++ b/test-apps/redirect/app.js
@@ -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()
+})
diff --git a/test-apps/redirect/cypress.config.js b/test-apps/redirect/cypress.config.js
new file mode 100644
index 00000000..d5f38630
--- /dev/null
+++ b/test-apps/redirect/cypress.config.js
@@ -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
+ }
+})
diff --git a/test-apps/redirect/cypress/e2e/spec.cy.js b/test-apps/redirect/cypress/e2e/spec.cy.js
new file mode 100644
index 00000000..5c36a2fe
--- /dev/null
+++ b/test-apps/redirect/cypress/e2e/spec.cy.js
@@ -0,0 +1,11 @@
+// enables intelligent code completion for Cypress commands
+// https://on.cypress.io/intelligent-code-completion
+///