Skip to content

Commit

Permalink
Replace colons in transform cache filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
etr2460 committed Apr 21, 2019
1 parent 31e06e8 commit 92b06d9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@
- `[jest-core]` Make `detectOpenHandles` imply `runInBand` ([#8283](https://github.com/facebook/jest/pull/8283))
- `[jest-haste-map]` Fix the `mapper` option which was incorrectly ignored ([#8299](https://github.com/facebook/jest/pull/8299))
- `[jest-jasmine2]` Fix describe return value warning being shown if the describe function throws ([#8335](https://github.com/facebook/jest/pull/8335))
- `[jest-transform]` Replace colons in transform cache filenames to support Windows

### Chore & Maintenance

Expand Down
8 changes: 4 additions & 4 deletions packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -121,11 +121,11 @@ export default class ScriptTransformer {
// Create sub folders based on the cacheKey to avoid creating one
// directory with many files.
const cacheDir = path.join(baseCacheDir, cacheKey[0] + cacheKey[1]);
const cacheFilenamePrefix = path
.basename(filename, path.extname(filename))
.replace(/:/g, '_COLON_');
const cachePath = slash(
path.join(
cacheDir,
path.basename(filename, path.extname(filename)) + '_' + cacheKey,
),
path.join(cacheDir, cacheFilenamePrefix + '_' + cacheKey),
);
createDirectory(cacheDir);

Expand Down
29 changes: 29 additions & 0 deletions packages/jest-transform/src/__tests__/script_transformer.test.js
Expand Up @@ -152,6 +152,7 @@ describe('ScriptTransformer', () => {

mockFs = object({
'/fruits/banana.js': ['module.exports = "banana";'].join('\n'),
'/fruits/banana:colon.js': ['module.exports = "bananaColon";'].join('\n'),
'/fruits/grapefruit.js': [
'module.exports = function () { return "grapefruit"; }',
].join('\n'),
Expand Down Expand Up @@ -532,6 +533,34 @@ describe('ScriptTransformer', () => {
expect(writeFileAtomic.sync).toBeCalled();
});

it('reads values from the cache when the file contains colons', () => {
const transformConfig = {
...config,
transform: [['^.+\\.js$', 'test_preprocessor']],
};
let scriptTransformer = new ScriptTransformer(transformConfig);
scriptTransformer.transform('/fruits/banana:colon.js', {});

const cachePath = getCachePath(mockFs, config);
expect(writeFileAtomic.sync).toBeCalled();
expect(writeFileAtomic.sync.mock.calls[0][0]).toBe(cachePath);

// Cache the state in `mockFsCopy`
const mockFsCopy = mockFs;
jest.resetModuleRegistry();
reset();

// Restore the cached fs
mockFs = mockFsCopy;
scriptTransformer = new ScriptTransformer(transformConfig);
scriptTransformer.transform('/fruits/banana:colon.js', {});

expect(fs.readFileSync).toHaveBeenCalledTimes(2);
expect(fs.readFileSync).toBeCalledWith('/fruits/banana:colon.js', 'utf8');
expect(fs.readFileSync).toBeCalledWith(cachePath, 'utf8');
expect(writeFileAtomic.sync).not.toBeCalled();
});

it('does not reuse the in-memory cache between different projects', () => {
const scriptTransformer = new ScriptTransformer({
...config,
Expand Down

0 comments on commit 92b06d9

Please sign in to comment.