Skip to content

Commit 82c7415

Browse files
authored
[feature] Add package to create cache key functions (#10587)
1 parent abf9f8d commit 82c7415

File tree

8 files changed

+148
-0
lines changed

8 files changed

+148
-0
lines changed

.watchmanconfig

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- `[jest-circus, jest-config, jest-runtime]` Add new `injectGlobals` config and CLI option to disable injecting global variables into the runtime ([#10484](https://github.com/facebook/jest/pull/10484))
66
- `[jest-each]` Fixes `.each` type to always be callable ([#10447](https://github.com/facebook/jest/pull/10447))
77
- `[jest-runner]` Add support for `moduleLoader`s with `default` exports ([#10541](https://github.com/facebook/jest/pull/10541))
8+
- `[@jest/create-cache-key-function]` Added a new package for creating cache keys ([#10587](https://github.com/facebook/jest/pull/10587))
89

910
### Fixes
1011

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
**/__mocks__/**
2+
**/__tests__/**
3+
src
4+
tsconfig.json
5+
tsconfig.tsbuildinfo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@jest/create-cache-key-function",
3+
"version": "26.4.2",
4+
"repository": {
5+
"type": "git",
6+
"url": "https://github.com/facebook/jest.git",
7+
"directory": "packages/jest-create-cache-key-function"
8+
},
9+
"devDependencies": {
10+
"@types/node": "*"
11+
},
12+
"engines": {
13+
"node": ">= 10.14.2"
14+
},
15+
"license": "MIT",
16+
"main": "build/index.js",
17+
"types": "build/index.d.ts",
18+
"publishConfig": {
19+
"access": "public"
20+
},
21+
"gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638"
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
let NODE_ENV: string;
9+
let BABEL_ENV: string;
10+
11+
beforeEach(() => {
12+
NODE_ENV = process.env.NODE_ENV;
13+
process.env.NODE_ENV = 'test';
14+
BABEL_ENV = process.env.BABEL_ENV;
15+
process.env.BABEL_ENV = 'test';
16+
});
17+
18+
afterEach(() => {
19+
process.env.NODE_ENV = NODE_ENV;
20+
process.env.BABEL_ENV = BABEL_ENV;
21+
});
22+
23+
test('creation of a cache key', () => {
24+
const createCacheKeyFunction = require('../index').default;
25+
const createCacheKey = createCacheKeyFunction([], ['value']);
26+
const hashA = createCacheKey('test', 'test.js', null, {
27+
config: {},
28+
instrument: false,
29+
});
30+
const hashB = createCacheKey('test code;', 'test.js', null, {
31+
config: {},
32+
instrument: false,
33+
});
34+
const hashC = createCacheKey('test', 'test.js', null, {
35+
config: {},
36+
instrument: true,
37+
});
38+
39+
expect(hashA.length).toEqual(32);
40+
expect(hashA).not.toEqual(hashB);
41+
expect(hashA).not.toEqual(hashC);
42+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
import {createHash} from 'crypto';
10+
import {relative} from 'path';
11+
/* eslint-disable-next-line no-restricted-imports */
12+
import {readFileSync} from 'fs';
13+
import type {Config} from '@jest/types';
14+
15+
type CacheKeyOptions = {
16+
config: Config.ProjectConfig;
17+
instrument: boolean;
18+
};
19+
20+
type GetCacheKeyFunction = (
21+
fileData: string,
22+
filePath: Config.Path,
23+
configStr: string,
24+
options: CacheKeyOptions,
25+
) => string;
26+
27+
function getGlobalCacheKey(files: Array<string>, values: Array<string>) {
28+
return [
29+
process.env.NODE_ENV,
30+
process.env.BABEL_ENV,
31+
...values,
32+
...files.map((file: string) => readFileSync(file)),
33+
]
34+
.reduce(
35+
(hash, chunk) => hash.update('\0', 'utf8').update(chunk || ''),
36+
createHash('md5'),
37+
)
38+
.digest('hex');
39+
}
40+
41+
function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction {
42+
return (src, file, _configString, options) => {
43+
const {config, instrument} = options;
44+
return createHash('md5')
45+
.update(globalCacheKey)
46+
.update('\0', 'utf8')
47+
.update(src)
48+
.update('\0', 'utf8')
49+
.update(config.rootDir ? relative(config.rootDir, file) : '')
50+
.update('\0', 'utf8')
51+
.update(instrument ? 'instrument' : '')
52+
.digest('hex');
53+
};
54+
}
55+
56+
export default (
57+
files: Array<string> = [],
58+
values: Array<string> = [],
59+
): GetCacheKeyFunction => getCacheKeyFunction(getGlobalCacheKey(files, values));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "build"
6+
},
7+
"references": [
8+
{"path": "../jest-types"}
9+
]
10+
}

yarn.lock

+8
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,14 @@ __metadata:
18081808
languageName: unknown
18091809
linkType: soft
18101810

1811+
"@jest/create-cache-key-function@workspace:packages/jest-create-cache-key-function":
1812+
version: 0.0.0-use.local
1813+
resolution: "@jest/create-cache-key-function@workspace:packages/jest-create-cache-key-function"
1814+
dependencies:
1815+
"@types/node": "*"
1816+
languageName: unknown
1817+
linkType: soft
1818+
18111819
"@jest/environment@^26.3.0, @jest/environment@workspace:packages/jest-environment":
18121820
version: 0.0.0-use.local
18131821
resolution: "@jest/environment@workspace:packages/jest-environment"

0 commit comments

Comments
 (0)