Skip to content

[heft-jest] (BREAKING CHANGE) Use slash-normalized relative paths for asset URLs#5175

Merged
dmichon-msft merged 3 commits intomicrosoft:mainfrom
dmichon-msft:relativize-jest-asset-paths
Apr 1, 2025
Merged

[heft-jest] (BREAKING CHANGE) Use slash-normalized relative paths for asset URLs#5175
dmichon-msft merged 3 commits intomicrosoft:mainfrom
dmichon-msft:relativize-jest-asset-paths

Conversation

@dmichon-msft
Copy link
Copy Markdown
Contributor

@dmichon-msft dmichon-msft commented Mar 31, 2025

Summary

Updates the jest-string-mock-transformer that is the default transformation for static assets to emit slash-normalized paths relative to config.rootDir, rather than absolute paths. This ensures that the result of the transform can be safely used in snapshot tests, and will continue to work if the path is passed directly into file system APIs, because the fs API resolves relative paths to the current working directory.

Details

The motivation for this change is that the current implementation of the transformer is unsafe to use in snapshot tests, since it returns platform-specific absolute paths.

Example:

src/image.test.ts

import myImage from './my-image.png';

// Path will use slashes and be relative to the current working directory
// It will be the relative path to the *output* file in the CommonJS output folder.
expect(myImage).toBe('lib-commonjs/my-image.png');

How it was tested

Added a build test case.

Impacted documentation

Docs on the default transformer.

D4N14L
D4N14L previously approved these changes Mar 31, 2025
Comment thread build-tests/heft-webpack5-everything-test/src/chunks/image.d.png.ts
Comment thread heft-plugins/heft-jest-plugin/src/transformers/StringMockTransformer.ts Outdated
@dmichon-msft dmichon-msft enabled auto-merge (squash) April 1, 2025 00:31
@dmichon-msft dmichon-msft merged commit 1a1b336 into microsoft:main Apr 1, 2025
5 checks passed
@github-project-automation github-project-automation Bot moved this from Needs triage to Closed in Bug Triage Apr 1, 2025
@dmichon-msft dmichon-msft deleted the relativize-jest-asset-paths branch April 1, 2025 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Closed

Development

Successfully merging this pull request may close these issues.

3 participants