Skip to content

Commit 1a1b336

Browse files
authored
[heft-jest] (BREAKING CHANGE) Use slash-normalized relative paths for asset URLs (#5175)
* [heft-jest] Use slash-normalized relative paths for asset URLs * Update doc on transform * Use `rootDir` instead of `cwd` --------- Co-authored-by: David Michon <[email protected]>
1 parent c152ea5 commit 1a1b336

File tree

6 files changed

+39
-4
lines changed

6 files changed

+39
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
declare const path: string;
5+
6+
export default path;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import image from '../chunks/image.png';
5+
6+
describe('Image Test', () => {
7+
it('correctly handles urls for images', () => {
8+
expect(image).toBe('lib-commonjs/chunks/image.png');
9+
});
10+
});

build-tests/heft-webpack5-everything-test/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
"outDir": "lib",
66
"rootDir": "src",
77

8+
"allowArbitraryExtensions": true,
89
"forceConsistentCasingInFileNames": true,
10+
"esModuleInterop": true,
11+
"allowSyntheticDefaultImports": true,
912
"jsx": "react",
1013
"declaration": true,
1114
"sourceMap": true,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/heft-jest-plugin",
5+
"comment": "(BREAKING CHANGE) Update `jest-string-mock-transform` to emit slash-normalized relative paths to files, rather than absolute paths, to ensure portability of snapshots.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/heft-jest-plugin"
10+
}

heft-plugins/heft-jest-plugin/includes/jest-shared.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"transformIgnorePatterns": ["\\.c?js$"],
6161

6262
// jest-identity-mock-transform returns a proxy for exported key/value pairs, where Webpack would return a module
63-
// jest-string-mock-transform returns the filename, where Webpack would return a URL
63+
// jest-string-mock-transform returns the filename, relative to the current working directory, where Webpack would return a URL
6464
// When using the heft-jest-plugin, these will be replaced with the resolved module location
6565
"transform": {
6666
"\\.(css|sass|scss)$": "../lib/exports/jest-identity-mock-transform.js",
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
3-
3+
import { relative } from 'node:path';
44
import type { SyncTransformer, TransformedSource, TransformOptions } from '@jest/transform';
55

6+
const isWindows: boolean = process.platform === 'win32';
7+
68
/**
79
* This Jest transform handles imports of data files (e.g. .png, .jpg) that would normally be
810
* processed by a Webpack's file-loader. Instead of actually loading the resource, we return the file's name.
@@ -11,9 +13,13 @@ import type { SyncTransformer, TransformedSource, TransformOptions } from '@jest
1113
*/
1214
export class StringMockTransformer implements SyncTransformer {
1315
public process(sourceText: string, sourcePath: string, options: TransformOptions): TransformedSource {
14-
// For a file called "myImage.png", this will generate a JS module that exports the literal string "myImage.png"
16+
// heft-jest-plugin enforces that config.rootDir will always be the project root folder.
17+
const relativePath: string = relative(options.config.rootDir, sourcePath);
18+
const normalizedRelativePath: string = isWindows ? relativePath.replace(/\\/g, '/') : relativePath;
19+
// For a file called "myImage.png", this will generate a JS module that exports the slash-normalized relative
20+
// path from the current working directory to "myImage.png"
1521
return {
16-
code: `module.exports = ${JSON.stringify(sourcePath)};`
22+
code: `module.exports = ${JSON.stringify(normalizedRelativePath)};`
1723
};
1824
}
1925
}

0 commit comments

Comments
 (0)