Skip to content
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

Replace colons in transform cache filenames #8353

Merged
merged 8 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- `[jest-snapshot]` Handle arrays when merging snapshots ([#7089](https://github.com/facebook/jest/pull/7089))
- `[expect]` Extract names of async and generator functions ([#8362](https://github.com/facebook/jest/pull/8362))
- `[jest-runtime]` Fix virtual mocks not being unmockable after previously being mocked ([#8396](https://github.com/facebook/jest/pull/8396))
- `[jest-transform]` Replace special characters in transform cache filenames to support Windows ([#8353](https://github.com/facebook/jest/pull/8353))

### Chore & Maintenance

Expand Down
9 changes: 5 additions & 4 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export default class ScriptTransformer {
.update(fileData)
.update(configString)
.update(instrument ? 'instrument' : '')
.update(filename)
.update(CACHE_VERSION)
.digest('hex');
}
Expand All @@ -121,11 +122,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(/\W/g, '');
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
Original file line number Diff line number Diff line change
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