Skip to content

Use url path for snapshot src attribute #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

stephanie56
Copy link
Member

@stephanie56 stephanie56 commented Aug 7, 2025

Issue:

Problem

When running Cypress with Chromatic on Windows machines, the sourceMap object was storing Windows file system paths (containing backslashes) as values, which were then used to update the src attributes in the DOM snapshots.

While the Windows file system path may work for an image url (browser normalize it automatically), but urls used in CSS properties could be broken and caused resource not found since backslashes are used for css escape. For example, this code:

.btn {background: url('assets\images\icon.png')}

can be interpreted as this:

.btn {background-image: url('assetsmagescon.png')}

What this PR does

Separate the concerns of file system operations and URL generation:

  • Continue using the original fileSystemPath for writing files to archive
  • Convert backslashes to forward slashes before storing in sourceMap for URL replacement.

How to test

@stephanie56 stephanie56 force-pushed the stephanie/ap-6057-svgs-not-loading-in-chromatic-build-but-available-in-cypress branch from a12f0f7 to dd9356d Compare August 7, 2025 14:21
@stephanie56 stephanie56 force-pushed the stephanie/ap-6057-svgs-not-loading-in-chromatic-build-but-available-in-cypress branch from dd9356d to 7e700d8 Compare August 7, 2025 14:24
Copy link

codecov bot commented Aug 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.65%. Comparing base (87c85ab) to head (7e700d8).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #270      +/-   ##
==========================================
+ Coverage   95.64%   95.65%   +0.01%     
==========================================
  Files          14       14              
  Lines         390      391       +1     
  Branches       67       66       -1     
==========================================
+ Hits          373      374       +1     
  Misses         17       17              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

if (origSrcPath !== fileSystemPath) {
sourceMap.set(origSrcPath, fileSystemPath);
const mappedSrcUrl = fileSystemPath.replace(/\\/g, '/');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fileSystemPath will always be normalized with forward-slashes because it's created from a URL:

> new URL('https://chromatic.com/a/b/c\\d\\e\\f').pathname
'/a/b/c/d/e/f'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tevanoff that's right, fileSystemPath is created by the new URL(url), but later in the pipeline it's broken down into parts and rejoined again here this.truncateFileName(sanitizedSrc).

    let sanitizedSrc = this.url.pathname; // <- starting point
    sanitizedSrc = this.preserveExternalDomain();
    sanitizedSrc = this.ensureNonDirectory(sanitizedSrc);
    sanitizedSrc = this.encodeQueryString(sanitizedSrc);
    sanitizedSrc = this.truncateFileName(sanitizedSrc); // <- transformed to platform specific path
    sanitizedSrc = this.removeSpecialChars(sanitizedSrc);
    sanitizedSrc = this.renameReservedPathnames(sanitizedSrc);
    sanitizedSrc = this.addExtension(sanitizedSrc);

The path parts are joined again using path.join(), this method joins all given path segments together using the platform-specific separator as a delimiter. So if the user is running e2e on a windows machine, then path.join() return a windows specific path?

@stephanie56 stephanie56 closed this Aug 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants