Skip to content

Commit

Permalink
Make transform cache paths depend on config name and Jest version. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer authored Jul 13, 2016
1 parent 64ac15d commit c4050d0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Path: /fruits/banana.js
Config: {
\"cache\": true,
\"cacheDirectory\": \"/cache/\",
\"name\": \"test\",
\"preprocessorIgnorePatterns\": [
\"/node_modules/\"
],
Expand Down
13 changes: 13 additions & 0 deletions packages/jest-runtime/src/__tests__/transform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ describe('transform', () => {
config = {
cache: true,
cacheDirectory: '/cache/',
name: 'test',
preprocessorIgnorePatterns: ['/node_modules/'],
};

Expand Down Expand Up @@ -160,5 +161,17 @@ describe('transform', () => {
expect(fs.readFileSync).toBeCalledWith('/fruits/banana.js', 'utf8');
expect(fs.readFileSync).toBeCalledWith(cachePath, 'utf8');
expect(fs.writeFileSync).not.toBeCalled();

// Don't read from the cache when `config.cache` is false.
jest.resetModuleRegistry();
reset();
mockFs = mockFsCopy;
transformConfig.cache = false;
transform('/fruits/banana.js', transformConfig);

expect(fs.readFileSync.mock.calls.length).toBe(1);
expect(fs.readFileSync).toBeCalledWith('/fruits/banana.js', 'utf8');
expect(fs.readFileSync).not.toBeCalledWith(cachePath, 'utf8');
expect(fs.writeFileSync).toBeCalled();
});
});
9 changes: 8 additions & 1 deletion packages/jest-runtime/src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ const Resolver = require('jest-resolve');
const createDirectory = require('jest-util').createDirectory;
const crypto = require('crypto');
const fs = require('graceful-fs');
const getCacheFilePath = require('jest-haste-map').getCacheFilePath;
const path = require('path');
const stableStringify = require('json-stable-stringify');
const vm = require('vm');

const VERSION = require('../package.json').version;

export type Processor = {
process: (sourceText: string, sourcePath: Path) => string,
};
Expand Down Expand Up @@ -152,7 +155,11 @@ module.exports = (
);
}

const baseCacheDir = path.join(config.cacheDirectory, 'preprocess-cache');
const baseCacheDir = getCacheFilePath(
config.cacheDirectory,
'jest-transform-cache-' + config.name,
VERSION,
);
const cacheKey = getCacheKey(preprocessor, content, filename, config);
// Create sub folders based on the cacheKey to avoid creating one
// directory with many files.
Expand Down

0 comments on commit c4050d0

Please sign in to comment.