Skip to content

Commit 92b06d9

Browse files
committed
Replace colons in transform cache filenames
1 parent 31e06e8 commit 92b06d9

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- `[jest-core]` Make `detectOpenHandles` imply `runInBand` ([#8283](https://github.com/facebook/jest/pull/8283))
1818
- `[jest-haste-map]` Fix the `mapper` option which was incorrectly ignored ([#8299](https://github.com/facebook/jest/pull/8299))
1919
- `[jest-jasmine2]` Fix describe return value warning being shown if the describe function throws ([#8335](https://github.com/facebook/jest/pull/8335))
20+
- `[jest-transform]` Replace colons in transform cache filenames to support Windows
2021

2122
### Chore & Maintenance
2223

packages/jest-transform/src/ScriptTransformer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ export default class ScriptTransformer {
121121
// Create sub folders based on the cacheKey to avoid creating one
122122
// directory with many files.
123123
const cacheDir = path.join(baseCacheDir, cacheKey[0] + cacheKey[1]);
124+
const cacheFilenamePrefix = path
125+
.basename(filename, path.extname(filename))
126+
.replace(/:/g, '_COLON_');
124127
const cachePath = slash(
125-
path.join(
126-
cacheDir,
127-
path.basename(filename, path.extname(filename)) + '_' + cacheKey,
128-
),
128+
path.join(cacheDir, cacheFilenamePrefix + '_' + cacheKey),
129129
);
130130
createDirectory(cacheDir);
131131

packages/jest-transform/src/__tests__/script_transformer.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ describe('ScriptTransformer', () => {
152152

153153
mockFs = object({
154154
'/fruits/banana.js': ['module.exports = "banana";'].join('\n'),
155+
'/fruits/banana:colon.js': ['module.exports = "bananaColon";'].join('\n'),
155156
'/fruits/grapefruit.js': [
156157
'module.exports = function () { return "grapefruit"; }',
157158
].join('\n'),
@@ -532,6 +533,34 @@ describe('ScriptTransformer', () => {
532533
expect(writeFileAtomic.sync).toBeCalled();
533534
});
534535

536+
it('reads values from the cache when the file contains colons', () => {
537+
const transformConfig = {
538+
...config,
539+
transform: [['^.+\\.js$', 'test_preprocessor']],
540+
};
541+
let scriptTransformer = new ScriptTransformer(transformConfig);
542+
scriptTransformer.transform('/fruits/banana:colon.js', {});
543+
544+
const cachePath = getCachePath(mockFs, config);
545+
expect(writeFileAtomic.sync).toBeCalled();
546+
expect(writeFileAtomic.sync.mock.calls[0][0]).toBe(cachePath);
547+
548+
// Cache the state in `mockFsCopy`
549+
const mockFsCopy = mockFs;
550+
jest.resetModuleRegistry();
551+
reset();
552+
553+
// Restore the cached fs
554+
mockFs = mockFsCopy;
555+
scriptTransformer = new ScriptTransformer(transformConfig);
556+
scriptTransformer.transform('/fruits/banana:colon.js', {});
557+
558+
expect(fs.readFileSync).toHaveBeenCalledTimes(2);
559+
expect(fs.readFileSync).toBeCalledWith('/fruits/banana:colon.js', 'utf8');
560+
expect(fs.readFileSync).toBeCalledWith(cachePath, 'utf8');
561+
expect(writeFileAtomic.sync).not.toBeCalled();
562+
});
563+
535564
it('does not reuse the in-memory cache between different projects', () => {
536565
const scriptTransformer = new ScriptTransformer({
537566
...config,

0 commit comments

Comments
 (0)