Skip to content

Commit

Permalink
Accept getCacheKey for dependencyExtractor and hasteImplModulePath
Browse files Browse the repository at this point in the history
  • Loading branch information
rubennorte committed Nov 9, 2018
1 parent 0da47a8 commit 5998235
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 16 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
- `[babel-preset-jest]` [**BREAKING**] Export a function instead of an object for Babel 7 compatibility ([#7203](https://github.com/facebook/jest/pull/7203))
- `[expect]` Check constructor equality in .toStrictEqual() ([#7005](https://github.com/facebook/jest/pull/7005))
- `[jest-util]` Add `jest.getTimerCount()` to get the count of scheduled fake timers ([#7285](https://github.com/facebook/jest/pull/7285))
- `[jest-config]` Add `dependencyExtractor` option to use a custom module to extract dependencies from files ([#7313](https://github.com/facebook/jest/pull/7313), [#7349](https://github.com/facebook/jest/pull/7349)).
- `[jest-config]` Add `dependencyExtractor` option to use a custom module to extract dependencies from files ([#7313](https://github.com/facebook/jest/pull/7313), [#7349](https://github.com/facebook/jest/pull/7349), [#7350](https://github.com/facebook/jest/pull/7350)).
- `[jest-haste-map]` [**BREAKING**] Expose relative paths when getting the file iterator ([#7321](https://github.com/facebook/jest/pull/7321))
- `[jest-haste-map]` Accept a `getCacheKey` method in `hasteImplModulePath` modules to reset the cache when the logic changes ([#7350](https://github.com/facebook/jest/pull/7350))
- `[jest-config]` Add `haste.computeSha1` option to compute the sha-1 of the files in the haste map ([#7345](https://github.com/facebook/jest/pull/7345))

### Fixes
Expand Down
2 changes: 2 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ This option allows the use of a custom dependency extractor. It must be a node m

The function should return an iterable (`Array`, `Set`, etc.) with the dependencies found in the code.

That module can also contain a `getCacheKey` function to generate a cache key to determine if the logic has changed and any cached artifacts relying on it should be discarded.

### `errorOnDeprecated` [boolean]

Default: `false`
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-haste-map/src/__tests__/dependencyExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ export function extract(code, defaultDependencyExtractor) {

return dependencies;
}

let cacheKey;

export function getCacheKey() {
return cacheKey;
}

export function setCacheKey(key) {
cacheKey = key;
}
38 changes: 23 additions & 15 deletions packages/jest-haste-map/src/__tests__/haste_impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@

const path = require('path');

module.exports = {
getHasteName(filename) {
if (
filename.includes('__mocks__') ||
filename.includes('NoHaste') ||
filename.includes(path.sep + 'module_dir' + path.sep) ||
filename.includes(path.sep + 'sourcemaps' + path.sep)
) {
return undefined;
}
export function getHasteName(filename) {
if (
filename.includes('__mocks__') ||
filename.includes('NoHaste') ||
filename.includes(path.sep + 'module_dir' + path.sep) ||
filename.includes(path.sep + 'sourcemaps' + path.sep)
) {
return undefined;
}

return filename
.substr(filename.lastIndexOf(path.sep) + 1)
.replace(/(\.(android|ios|native))?\.js$/, '');
},
};
return filename
.substr(filename.lastIndexOf(path.sep) + 1)
.replace(/(\.(android|ios|native))?\.js$/, '');
}

let cacheKey;

export function getCacheKey() {
return cacheKey;
}

export function setCacheKey(key) {
cacheKey = key;
}
25 changes: 25 additions & 0 deletions packages/jest-haste-map/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,31 @@ describe('HasteMap', () => {
expect(hasteMap1.getCacheFilePath()).not.toBe(hasteMap2.getCacheFilePath());
});

it('creates different cache file paths for different dependency extractor cache keys', () => {
jest.resetModuleRegistry();
const HasteMap = require('../');
const dependencyExtractor = require('./dependencyExtractor');
const config = Object.assign({}, defaultConfig, {
dependencyExtractor: require.resolve('./dependencyExtractor'),
});
dependencyExtractor.setCacheKey('foo');
const hasteMap1 = new HasteMap(config);
dependencyExtractor.setCacheKey('bar');
const hasteMap2 = new HasteMap(config);
expect(hasteMap1.getCacheFilePath()).not.toBe(hasteMap2.getCacheFilePath());
});

it('creates different cache file paths for different hasteImplModulePath cache keys', () => {
jest.resetModuleRegistry();
const HasteMap = require('../');
const hasteImpl = require('./haste_impl');
hasteImpl.setCacheKey('foo');
const hasteMap1 = new HasteMap(defaultConfig);
hasteImpl.setCacheKey('bar');
const hasteMap2 = new HasteMap(defaultConfig);
expect(hasteMap1.getCacheFilePath()).not.toBe(hasteMap2.getCacheFilePath());
});

it('creates different cache file paths for different projects', () => {
jest.resetModuleRegistry();
const HasteMap = require('../');
Expand Down
20 changes: 20 additions & 0 deletions packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,28 @@ class HasteMap extends EventEmitter {
'deprecated. Provide a RegExp instead. See https://github.com/facebook/jest/pull/4063.',
);
}

const rootDirHash = crypto
.createHash('md5')
.update(options.rootDir)
.digest('hex');
let hasteImplHash = '';
let dependencyExtractorHash = '';

if (options.hasteImplModulePath) {
const hasteImpl = require(options.hasteImplModulePath);
if (hasteImpl.getCacheKey) {
hasteImplHash = String(hasteImpl.getCacheKey());
}
}

if (options.dependencyExtractor) {
const dependencyExtractor = require(options.dependencyExtractor);
if (dependencyExtractor.getCacheKey) {
dependencyExtractorHash = String(dependencyExtractor.getCacheKey());
}
}

this._cachePath = HasteMap.getCacheFilePath(
this._options.cacheDirectory,
`haste-map-${this._options.name}-${rootDirHash}`,
Expand All @@ -278,6 +296,8 @@ class HasteMap extends EventEmitter {
this._options.computeSha1.toString(),
options.mocksPattern || '',
(options.ignorePattern || '').toString(),
hasteImplHash,
dependencyExtractorHash,
);
this._whitelist = getWhiteList(options.providesModuleNodeModules);
this._buildPromise = null;
Expand Down

0 comments on commit 5998235

Please sign in to comment.