-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Add package to create cache key functions
- Loading branch information
Showing
8 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
**/__mocks__/** | ||
**/__tests__/** | ||
src | ||
tsconfig.json | ||
tsconfig.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "@jest/create-cache-key-function", | ||
"version": "26.4.2", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/facebook/jest.git", | ||
"directory": "packages/jest-create-cache-key-function" | ||
}, | ||
"dependencies": { | ||
"graceful-fs": "^4.2.4" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "*" | ||
}, | ||
"engines": { | ||
"node": ">= 10.14.2" | ||
}, | ||
"license": "MIT", | ||
"main": "build/index.js", | ||
"types": "build/index.d.ts", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638" | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/jest-create-cache-key-function/src/__tests__/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
let NODE_ENV: string; | ||
let BABEL_ENV: string; | ||
|
||
beforeEach(() => { | ||
NODE_ENV = process.env.NODE_ENV; | ||
process.env.NODE_ENV = 'test'; | ||
BABEL_ENV = process.env.BABEL_ENV; | ||
process.env.BABEL_ENV = 'test'; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env.NODE_ENV = NODE_ENV; | ||
process.env.BABEL_ENV = BABEL_ENV; | ||
}); | ||
|
||
test('creation of a cache key', () => { | ||
const createCacheKeyFunction = require('../index').default; | ||
const createCacheKey = createCacheKeyFunction([], ['value']); | ||
const hashA = createCacheKey('test', 'test.js', null, { | ||
config: {}, | ||
instrument: false, | ||
}); | ||
const hashB = createCacheKey('test code;', 'test.js', null, { | ||
config: {}, | ||
instrument: false, | ||
}); | ||
const hashC = createCacheKey('test', 'test.js', null, { | ||
config: {}, | ||
instrument: true, | ||
}); | ||
|
||
expect(hashA.length).toEqual(32); | ||
expect(hashA).not.toEqual(hashB); | ||
expect(hashA).not.toEqual(hashC); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import {createHash} from 'crypto'; | ||
import {relative} from 'path'; | ||
import {readFileSync} from 'graceful-fs'; | ||
import type {Config, ProjectConfig} from '@jest/types'; | ||
|
||
type CacheKeyOptions = { | ||
config: ProjectConfig; | ||
instrument: boolean; | ||
}; | ||
|
||
type GetCacheKeyFunction = ( | ||
fileData: string, | ||
filePath: Config.Path, | ||
configStr: string, | ||
options: CacheKeyOptions, | ||
) => string; | ||
|
||
function getGlobalCacheKey(files: Array<string>, values: Array<string>) { | ||
const dependencies = require('../package').dependencies; | ||
return [ | ||
process.env.NODE_ENV, | ||
process.env.BABEL_ENV, | ||
dependencies && dependencies['babel-preset-fbjs'], | ||
...values, | ||
...files.map((file: string) => readFileSync(file)), | ||
] | ||
.reduce( | ||
(hash, chunk) => hash.update('\0', 'utf-8').update(chunk || ''), | ||
createHash('md5'), | ||
) | ||
.digest('hex'); | ||
} | ||
|
||
function getCacheKeyFunction(globalCacheKey: string) { | ||
return ( | ||
src: string, | ||
file: Config.Path, | ||
configString: string, | ||
options: CacheKeyOptions, | ||
) => { | ||
const {config, instrument} = options; | ||
const rootDir = config && config.rootDir; | ||
|
||
return createHash('md5') | ||
.update(globalCacheKey) | ||
.update('\0', 'utf8') | ||
.update(src) | ||
.update('\0', 'utf8') | ||
.update(rootDir ? relative(config.rootDir, file) : '') | ||
.update('\0', 'utf8') | ||
.update(instrument ? 'instrument' : '') | ||
.digest('hex'); | ||
}; | ||
} | ||
|
||
export default ( | ||
files: Array<string> = [], | ||
values: Array<string> = [], | ||
): GetCacheKeyFunction => getCacheKeyFunction(getGlobalCacheKey(files, values)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "build" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters