Cypress automatic cache retention versus manual cache clearing for CSS-only changes
0 reputation · 03 Jun 2021, 16:37 UTC
0 reputation · 03 Jun 2021, 16:37 UTC
Determine whether to rely on Cypress's automatic cache retention or to manually clear the cache when only CSS or import files change, in order to avoid stale screenshots while preserving fast test feedback.
Constraints include the speed benefit of retaining cached command snapshots versus the risk of showing outdated results; clearing the cache removes saved debugging artifacts and increases test execution time. The unresolved behavior is whether Cypress should automatically clear the cache on every cypress run when a package.json dependency changes, a decision currently left to the user.
Should Cypress automatically clear the cache when a dependency in package.json changes? What are the trade‑offs between enabling automatic cache clearing on every run versus relying on manual cache clearing? How does each approach affect test feedback speed and debugging capability in CI pipelines?
34775 reputation · 03 Jun 2021, 23:46 UTC
By default, Cypress does not clear its internal cache when a package.json dependency changes. The cache that can affect screenshot freshness is the cypress/cache directory, which stores command snapshots, screenshots, and videos that Cypress re‑uses between runs to speed up debugging. If you rely on this cache, a CSS or import change can be invisible in a new run because Cypress will replay the old snapshot. Clearing the cache manually (or disabling it for a run) guarantees that every run starts from a clean state, but it also removes the debugging artefacts and adds a measurable overhead to the test execution time.
cypress/cache that contains command snapshots, screenshots, and videos.package.json changes.npx cypress cache clear or delete the folder manually.cypress.config.js you can set cache: false (or pass --env cache=false) to disable caching for a single run.package.json changes; this must be implemented in the CI pipeline.If you want to avoid stale screenshots for CSS‑only changes while still enjoying fast feedback, the safest approach is to clear the cache only when a change to package.json (or your build assets) is detected. In a CI pipeline you can hash package.json and, if the hash changes, delete cypress/cache before running tests. This keeps the cache for unchanged runs and preserves debugging artefacts while guaranteeing fresh screenshots when the underlying assets change.
Enabling automatic clearing on every run (e.g., setting cache: false globally) guarantees fresh screenshots, but it removes the benefit of cached snapshots and increases runtime. Manual clearing (by running npx cypress cache clear only after a CSS change) keeps the cache for speed but introduces a human‑error risk: if you forget to clear the cache, CI may pass with stale visual results.
package.json in the CI job.
PACKAGE_HASH=$(sha256sum package.json | cut -d' ' -f1)
# Store the hash in a file or artifact
if [ "$PACKAGE_HASH" != "$(cat .prev_package_hash 2>/dev/null)" ]; then
echo "package.json changed – clearing Cypress cache"
npx cypress cache clear
fi
# Save the new hash for the next run
echo "$PACKAGE_HASH" > .prev_package_hash
npx cypress run --spec "cypress/e2e/**/*.cy.js"
With this pattern you keep the cache for unchanged runs, preserve debugging screenshots, and only clear when a dependency change could invalidate them.
cypress/cache folder is actually deleted when you run the CI script.# 1. Ensure the server serves fresh CSS
# (e.g., add ?v=${Date.now()} to the stylesheet link in the test page)
# 2. Run a test and capture a screenshot
npx cypress run --spec "cypress/e2e/example.cy.js"
# 3. Modify a CSS file
# 4. Run the test again without clearing the cache – screenshot should be the old style
npx cypress run --spec "cypress/e2e/example.cy.js"
# 5. Delete the cache folder
rm -rf cypress/cache
# 6. Run again – screenshot should now reflect the updated CSS
npx cypress run --spec "cypress/e2e/example.cy.js"
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.