From 30d1230d7eb774c8a7c937031d6fbf877d0ef26c Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Thu, 26 Jan 2023 11:31:15 -0500 Subject: [PATCH 01/29] fix: lower length of key to improve usage in win32 - limit key to to 16 characters - provide it as a parameter with the default so it can allow changes from the user - document method --- packages/jest-create-cache-key-function/src/index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index 4c056f30e0d8..db451494539f 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -40,7 +40,15 @@ type NewGetCacheKeyFunction = ( type GetCacheKeyFunction = OldGetCacheKeyFunction | NewGetCacheKeyFunction; -function getGlobalCacheKey(files: Array, values: Array) { +/** + * Computes the global cache key given a collection of files and values. This limits + * the output with a provided length. + * @param files list of files to read + * @param values list of values to add to the computation + * @param length length of the resulting key defaults to 16 + * @returns {string} the global cache key + */ +function getGlobalCacheKey(files: Array, values: Array, length = 16) { return [ process.env.NODE_ENV, process.env.BABEL_ENV, @@ -52,7 +60,7 @@ function getGlobalCacheKey(files: Array, values: Array) { createHash('sha1'), ) .digest('hex') - .substring(0, 32); + .substring(0, length); } function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { From 4555dfa3496327e5f7a4fdeb0ef2104bc02778de Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Fri, 27 Jan 2023 12:38:47 -0500 Subject: [PATCH 02/29] lint fix --- packages/jest-create-cache-key-function/src/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index db451494539f..d71afdcb88af 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -48,7 +48,11 @@ type GetCacheKeyFunction = OldGetCacheKeyFunction | NewGetCacheKeyFunction; * @param length length of the resulting key defaults to 16 * @returns {string} the global cache key */ -function getGlobalCacheKey(files: Array, values: Array, length = 16) { +function getGlobalCacheKey( + files: Array, + values: Array, + length = 16, +) { return [ process.env.NODE_ENV, process.env.BABEL_ENV, From d79fe07ba75f8ac58186ce7239e52c9e45205b00 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Fri, 27 Jan 2023 12:42:32 -0500 Subject: [PATCH 03/29] Removed default on getGlobalCacheKey and placed in the exported function --- .../jest-create-cache-key-function/src/index.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index d71afdcb88af..ed29381d5e93 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -45,13 +45,13 @@ type GetCacheKeyFunction = OldGetCacheKeyFunction | NewGetCacheKeyFunction; * the output with a provided length. * @param files list of files to read * @param values list of values to add to the computation - * @param length length of the resulting key defaults to 16 + * @param length length of the resulting key * @returns {string} the global cache key */ function getGlobalCacheKey( files: Array, values: Array, - length = 16, + length, ) { return [ process.env.NODE_ENV, @@ -87,9 +87,18 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { }; } +/** + * Computes the cache key given a collection of files and values. This limits + * the output with a provided length. + * @param files list of files to read + * @param values list of values to add to the computation + * @param length length of the resulting key defaults to 16 + * @returns {string} the global cache key + */ export default function createCacheKey( files: Array = [], values: Array = [], + length = 16, ): GetCacheKeyFunction { - return getCacheKeyFunction(getGlobalCacheKey(files, values)); + return getCacheKeyFunction(getGlobalCacheKey(files, values, length)); } From 2aca02edf62e08ced74ba25f5e04fa6151cda09d Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Fri, 27 Jan 2023 12:48:27 -0500 Subject: [PATCH 04/29] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 472efe9b8e45..c698b2d7dc97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ### Chore & Maintenance +- getCacheKey reduces size of global cache key portion to 16 characters. ([#13826](https://github.com/facebook/jest/pull/13827)) + ### Performance ## 29.4.1 From 643fd291501b5a83a0cdb815a89668fa602e189a Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Fri, 27 Jan 2023 12:49:03 -0500 Subject: [PATCH 05/29] Update CHANGELOG.md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c698b2d7dc97..47c8de4f51b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,10 +4,10 @@ ### Fixes -### Chore & Maintenance - - getCacheKey reduces size of global cache key portion to 16 characters. ([#13826](https://github.com/facebook/jest/pull/13827)) +### Chore & Maintenance + ### Performance ## 29.4.1 From 64d3a61b1d9b615b0a4118b920ed0611cd7c06f2 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Fri, 27 Jan 2023 12:51:28 -0500 Subject: [PATCH 06/29] Update index.ts --- packages/jest-create-cache-key-function/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index ed29381d5e93..a17691dabd06 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -51,7 +51,7 @@ type GetCacheKeyFunction = OldGetCacheKeyFunction | NewGetCacheKeyFunction; function getGlobalCacheKey( files: Array, values: Array, - length, + length: number, ) { return [ process.env.NODE_ENV, From 6341eceaa1bbdb2f239aafa85bdac5d6d67ec764 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Fri, 27 Jan 2023 15:28:52 -0500 Subject: [PATCH 07/29] Update CHANGELOG.md Co-authored-by: Tom Mrazauskas --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47c8de4f51b4..488fae04c480 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixes -- getCacheKey reduces size of global cache key portion to 16 characters. ([#13826](https://github.com/facebook/jest/pull/13827)) +- `[@jest/create-cache-key-function]` Allow passing `length` argument to `createCacheKey()` function and set its default value to `16` on Windows ([#13827](https://github.com/facebook/jest/pull/13827)) ### Chore & Maintenance From 522d7b87ec838c8402df01aa0fcd50bb1b0ce760 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Fri, 27 Jan 2023 15:32:13 -0500 Subject: [PATCH 08/29] Update index.ts --- packages/jest-create-cache-key-function/src/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index a17691dabd06..6d0c90c0c156 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -92,13 +92,14 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { * the output with a provided length. * @param files list of files to read * @param values list of values to add to the computation - * @param length length of the resulting key defaults to 16 + * @param length length of the resulting key defaults to 16 on win32 and 32 elsewhere * @returns {string} the global cache key */ export default function createCacheKey( files: Array = [], values: Array = [], - length = 16, + length?: number, ): GetCacheKeyFunction { - return getCacheKeyFunction(getGlobalCacheKey(files, values, length)); + let theLength = length ?? (process.platform === 'win32' ? 16 : 32); + return getCacheKeyFunction(getGlobalCacheKey(files, values, theLength)); } From fa8bee7e20ee46cfbc1cdc34f6d710e4354bc31d Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Fri, 27 Jan 2023 19:30:31 -0500 Subject: [PATCH 09/29] Update index.ts --- packages/jest-create-cache-key-function/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index 6d0c90c0c156..1e3e538f5a78 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -100,6 +100,6 @@ export default function createCacheKey( values: Array = [], length?: number, ): GetCacheKeyFunction { - let theLength = length ?? (process.platform === 'win32' ? 16 : 32); + const theLength = length ?? (process.platform === 'win32' ? 16 : 32); return getCacheKeyFunction(getGlobalCacheKey(files, values, theLength)); } From a8a7911342d5dd5062ca66229ecd75451ee03998 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Mon, 30 Jan 2023 13:11:56 -0500 Subject: [PATCH 10/29] Update packages/jest-create-cache-key-function/src/index.ts Co-authored-by: Tom Mrazauskas --- packages/jest-create-cache-key-function/src/index.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index 1e3e538f5a78..1ec3add48a42 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -40,14 +40,6 @@ type NewGetCacheKeyFunction = ( type GetCacheKeyFunction = OldGetCacheKeyFunction | NewGetCacheKeyFunction; -/** - * Computes the global cache key given a collection of files and values. This limits - * the output with a provided length. - * @param files list of files to read - * @param values list of values to add to the computation - * @param length length of the resulting key - * @returns {string} the global cache key - */ function getGlobalCacheKey( files: Array, values: Array, From 6fb185b9edc039a233c4667e9cd09235d872d383 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Mon, 30 Jan 2023 13:14:08 -0500 Subject: [PATCH 11/29] Update index.ts --- packages/jest-create-cache-key-function/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index 1ec3add48a42..698f53e17ad4 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -85,7 +85,7 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { * @param files list of files to read * @param values list of values to add to the computation * @param length length of the resulting key defaults to 16 on win32 and 32 elsewhere - * @returns {string} the global cache key + * @returns a function that is used to create the cache key. */ export default function createCacheKey( files: Array = [], From 9f3b767bfd3c468cabaeb8424204b5341c39d685 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Mon, 30 Jan 2023 13:15:13 -0500 Subject: [PATCH 12/29] Update index.ts --- packages/jest-create-cache-key-function/src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index 698f53e17ad4..8e33c7b8efec 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -80,8 +80,8 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { } /** - * Computes the cache key given a collection of files and values. This limits - * the output with a provided length. + * Provides a function that computes the cache key given a collection of files and values. + * This limits the output with a provided length. * @param files list of files to read * @param values list of values to add to the computation * @param length length of the resulting key defaults to 16 on win32 and 32 elsewhere From 7b607635602b740babc73f5ad6f613e2d93ae1ba Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Mon, 30 Jan 2023 13:16:20 -0500 Subject: [PATCH 13/29] Update README.md --- packages/jest-create-cache-key-function/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/jest-create-cache-key-function/README.md b/packages/jest-create-cache-key-function/README.md index cd014e038c6b..ba3d0ca6dc51 100644 --- a/packages/jest-create-cache-key-function/README.md +++ b/packages/jest-create-cache-key-function/README.md @@ -10,14 +10,15 @@ $ npm install --save-dev @jest/create-cache-key-function ## API -### `createCacheKey(files?: Array, values?: Array): GetCacheKeyFunction` +### `createCacheKey(files?: Array, values?: Array, length?: number): GetCacheKeyFunction` -Get a function that can generate cache keys using source code, provided files and provided values. +Provides a function that computes the cache key given a collection of files and values. This limits the output with a provided length. #### Parameters - `files`: [Optional] Array of absolute paths to files whose code should be accounted for when generating cache key - `values`: [Optional] Array of string values that should be accounted for when generating cache key +- `length`: [Optional] length of the resulting key defaults to 16 on win32 and 32 elsewhere **Note:** From 1e2a2db1b5cc5a1fb2b34526b1e0853b7bef3453 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Mon, 30 Jan 2023 13:18:01 -0500 Subject: [PATCH 14/29] Update index.ts --- packages/jest-create-cache-key-function/src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index 8e33c7b8efec..e7189636653b 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -82,8 +82,8 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { /** * Provides a function that computes the cache key given a collection of files and values. * This limits the output with a provided length. - * @param files list of files to read - * @param values list of values to add to the computation + * @param files Array of absolute paths to files whose code should be accounted for when generating cache key + * @param values Array of string values that should be accounted for when generating cache key * @param length length of the resulting key defaults to 16 on win32 and 32 elsewhere * @returns a function that is used to create the cache key. */ From 77551f8f9738b04869c6a02a3728863c58b5fef2 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Mon, 30 Jan 2023 13:22:20 -0500 Subject: [PATCH 15/29] Update packages/jest-create-cache-key-function/src/index.ts Co-authored-by: Tom Mrazauskas --- packages/jest-create-cache-key-function/src/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index e7189636653b..ef253be1be4d 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -92,6 +92,5 @@ export default function createCacheKey( values: Array = [], length?: number, ): GetCacheKeyFunction { - const theLength = length ?? (process.platform === 'win32' ? 16 : 32); - return getCacheKeyFunction(getGlobalCacheKey(files, values, theLength)); + return getCacheKeyFunction(getGlobalCacheKey(files, values, length)); } From 77e7ab36d3620b6b20c70d9374c40dcdee646aeb Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Mon, 30 Jan 2023 13:22:28 -0500 Subject: [PATCH 16/29] Update packages/jest-create-cache-key-function/src/index.ts Co-authored-by: Tom Mrazauskas --- packages/jest-create-cache-key-function/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index ef253be1be4d..e8c2696983a7 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -90,7 +90,7 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { export default function createCacheKey( files: Array = [], values: Array = [], - length?: number, + length = process.platform === 'win32' ? 16 : 32, ): GetCacheKeyFunction { return getCacheKeyFunction(getGlobalCacheKey(files, values, length)); } From 99ef72dd049d1db0def0c1dc4678ef49656f54ce Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Mon, 30 Jan 2023 13:23:29 -0500 Subject: [PATCH 17/29] Update index.ts --- packages/jest-create-cache-key-function/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index e8c2696983a7..9ddc0573f2bf 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -80,7 +80,7 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { } /** - * Provides a function that computes the cache key given a collection of files and values. + * Provides a function that computes the cache key given a collection of files and values. * This limits the output with a provided length. * @param files Array of absolute paths to files whose code should be accounted for when generating cache key * @param values Array of string values that should be accounted for when generating cache key From edfea0704f84c8d518abdc7db8db8a1ac2949386 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Mon, 30 Jan 2023 14:22:23 -0500 Subject: [PATCH 18/29] Update packages/jest-create-cache-key-function/README.md Co-authored-by: Tom Mrazauskas --- packages/jest-create-cache-key-function/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-create-cache-key-function/README.md b/packages/jest-create-cache-key-function/README.md index ba3d0ca6dc51..1da00e7aed64 100644 --- a/packages/jest-create-cache-key-function/README.md +++ b/packages/jest-create-cache-key-function/README.md @@ -12,7 +12,7 @@ $ npm install --save-dev @jest/create-cache-key-function ### `createCacheKey(files?: Array, values?: Array, length?: number): GetCacheKeyFunction` -Provides a function that computes the cache key given a collection of files and values. This limits the output with a provided length. +Returns a function that can be used to generate cache keys based on source code of provided files and provided values. #### Parameters From 530f9493d4224bf51ec16b8095b4c6b2696e9f6d Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Mon, 30 Jan 2023 14:22:40 -0500 Subject: [PATCH 19/29] Update packages/jest-create-cache-key-function/README.md Co-authored-by: Tom Mrazauskas --- packages/jest-create-cache-key-function/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-create-cache-key-function/README.md b/packages/jest-create-cache-key-function/README.md index 1da00e7aed64..531f65264946 100644 --- a/packages/jest-create-cache-key-function/README.md +++ b/packages/jest-create-cache-key-function/README.md @@ -18,7 +18,7 @@ Returns a function that can be used to generate cache keys based on source code - `files`: [Optional] Array of absolute paths to files whose code should be accounted for when generating cache key - `values`: [Optional] Array of string values that should be accounted for when generating cache key -- `length`: [Optional] length of the resulting key defaults to 16 on win32 and 32 elsewhere +- `length`: [Optional] Length of the resulting key. The default is `32`, or `16` on Windows. **Note:** From 0a36e2b1b89fe6f17fd387db72ffa8e3ddc5bff4 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Mon, 30 Jan 2023 14:22:56 -0500 Subject: [PATCH 20/29] Update packages/jest-create-cache-key-function/src/index.ts Co-authored-by: Tom Mrazauskas --- .../jest-create-cache-key-function/src/index.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index 9ddc0573f2bf..2e5b31b5f2c0 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -80,13 +80,13 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { } /** - * Provides a function that computes the cache key given a collection of files and values. - * This limits the output with a provided length. - * @param files Array of absolute paths to files whose code should be accounted for when generating cache key - * @param values Array of string values that should be accounted for when generating cache key - * @param length length of the resulting key defaults to 16 on win32 and 32 elsewhere - * @returns a function that is used to create the cache key. - */ + * Returns a function that can be used to generate cache keys based on source code of provided files and provided values. + * + * @param files - Array of absolute paths to files whose code should be accounted for when generating cache key + * @param values - Array of string values that should be accounted for when generating cache key + * @param length - Length of the resulting key. The default is `32`, or `16` on Windows. + * @returns A function that can be used to generate cache keys. + */ export default function createCacheKey( files: Array = [], values: Array = [], From f849b2c1bedb89f3914e78fde6650c8b1beab6d0 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Thu, 2 Feb 2023 08:47:03 -0500 Subject: [PATCH 21/29] Update index.test.ts --- .../src/__tests__/index.test.ts | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/packages/jest-create-cache-key-function/src/__tests__/index.test.ts b/packages/jest-create-cache-key-function/src/__tests__/index.test.ts index 15edc0fb3de9..154ec5445a37 100644 --- a/packages/jest-create-cache-key-function/src/__tests__/index.test.ts +++ b/packages/jest-create-cache-key-function/src/__tests__/index.test.ts @@ -9,17 +9,20 @@ import {interopRequireDefault} from 'jest-util'; let NODE_ENV: string; let BABEL_ENV: string; +let PLATFORM: string; beforeEach(() => { NODE_ENV = process.env.NODE_ENV; process.env.NODE_ENV = 'test'; BABEL_ENV = process.env.BABEL_ENV; process.env.BABEL_ENV = 'test'; + PLATFORM = process.platform; }); afterEach(() => { process.env.NODE_ENV = NODE_ENV; process.env.BABEL_ENV = BABEL_ENV; + process.platform = PLATFORM; }); test('creation of a cache key', () => { @@ -44,3 +47,27 @@ test('creation of a cache key', () => { expect(hashA).not.toEqual(hashB); expect(hashA).not.toEqual(hashC); }); + +test('creation of a cache key on win32', () => { + process.platform = 'win32'; + const createCacheKeyFunction = interopRequireDefault( + 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).toHaveLength(16); + expect(hashA).not.toEqual(hashB); + expect(hashA).not.toEqual(hashC); +}); From 6b8361f4f8f255d49533a2bc9027fec486dbfe13 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Thu, 2 Feb 2023 08:47:26 -0500 Subject: [PATCH 22/29] Update index.ts --- .../jest-create-cache-key-function/src/index.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index 2e5b31b5f2c0..5cd70bbaeb1f 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -80,13 +80,13 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { } /** - * Returns a function that can be used to generate cache keys based on source code of provided files and provided values. - * - * @param files - Array of absolute paths to files whose code should be accounted for when generating cache key - * @param values - Array of string values that should be accounted for when generating cache key - * @param length - Length of the resulting key. The default is `32`, or `16` on Windows. - * @returns A function that can be used to generate cache keys. - */ + * Returns a function that can be used to generate cache keys based on source code of provided files and provided values. + * + * @param files - Array of absolute paths to files whose code should be accounted for when generating cache key + * @param values - Array of string values that should be accounted for when generating cache key + * @param length - Length of the resulting key. The default is `32`, or `16` on Windows. + * @returns A function that can be used to generate cache keys. + */ export default function createCacheKey( files: Array = [], values: Array = [], From b1fcfb9565ef52400cf2308b8e07d27458f37468 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Wed, 15 Feb 2023 09:57:40 -0500 Subject: [PATCH 23/29] Update index.ts --- packages/jest-create-cache-key-function/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index 5cd70bbaeb1f..2a3afd5a67d6 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -81,7 +81,7 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { /** * Returns a function that can be used to generate cache keys based on source code of provided files and provided values. - * + * * @param files - Array of absolute paths to files whose code should be accounted for when generating cache key * @param values - Array of string values that should be accounted for when generating cache key * @param length - Length of the resulting key. The default is `32`, or `16` on Windows. From 714a93d2ac430084333715b037d1b30bf838f9f6 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Wed, 15 Feb 2023 09:58:43 -0500 Subject: [PATCH 24/29] Update index.test.ts --- .../src/__tests__/index.test.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/packages/jest-create-cache-key-function/src/__tests__/index.test.ts b/packages/jest-create-cache-key-function/src/__tests__/index.test.ts index 154ec5445a37..6545ec66ebc8 100644 --- a/packages/jest-create-cache-key-function/src/__tests__/index.test.ts +++ b/packages/jest-create-cache-key-function/src/__tests__/index.test.ts @@ -58,16 +58,6 @@ test('creation of a cache key on win32', () => { 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).toHaveLength(16); - expect(hashA).not.toEqual(hashB); - expect(hashA).not.toEqual(hashC); }); From 77cc75de4d79c30e379c9385e41475a7c42d51e6 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Wed, 15 Feb 2023 12:38:07 -0500 Subject: [PATCH 25/29] Update index.test.ts --- .../src/__tests__/index.test.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/jest-create-cache-key-function/src/__tests__/index.test.ts b/packages/jest-create-cache-key-function/src/__tests__/index.test.ts index 220a4144abfa..3060f5548de2 100644 --- a/packages/jest-create-cache-key-function/src/__tests__/index.test.ts +++ b/packages/jest-create-cache-key-function/src/__tests__/index.test.ts @@ -22,7 +22,9 @@ beforeEach(() => { afterEach(() => { process.env.NODE_ENV = NODE_ENV; process.env.BABEL_ENV = BABEL_ENV; - process.platform = PLATFORM; + Object.defineProperty(process, 'platform', { + value: PLATFORM, + }); }); test('creation of a cache key', () => { @@ -49,7 +51,9 @@ test('creation of a cache key', () => { }); test('creation of a cache key on win32', () => { - process.platform = 'win32'; + Object.defineProperty(process, 'platform', { + value: 'win32', + }); const createCacheKeyFunction = interopRequireDefault( require('../index'), ).default; From 2f075e2dca58588c347799d77cbe4efefa0c06a5 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Feb 2023 09:04:29 +0100 Subject: [PATCH 26/29] move changelog entry --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa655418b9c0..94b33ff331ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ ### Fixes +- `[@jest/create-cache-key-function]` Allow passing `length` argument to `createCacheKey()` function and set its default value to `16` on Windows ([#13827](https://github.com/facebook/jest/pull/13827)) + ### Chore & Maintenance ### Performance @@ -27,7 +29,6 @@ - `[jest-mock]` Clear mock state when `jest.restoreAllMocks()` is called ([#13867](https://github.com/facebook/jest/pull/13867)) - `[jest-mock]` Prevent `mockImplementationOnce` and `mockReturnValueOnce` bleeding into `withImplementation` ([#13888](https://github.com/facebook/jest/pull/13888)) - `[jest-mock]` Do not restore mocks when `jest.resetAllMocks()` is called ([#13866](https://github.com/facebook/jest/pull/13866)) -- `[@jest/create-cache-key-function]` Allow passing `length` argument to `createCacheKey()` function and set its default value to `16` on Windows ([#13827](https://github.com/facebook/jest/pull/13827)) ## 29.4.2 From e75fa73844c9da58342f489b68f5347d9d05ccc7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Feb 2023 09:15:23 +0100 Subject: [PATCH 27/29] pass length to both substrings --- packages/jest-create-cache-key-function/src/index.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index 2e5c4d4d52fb..215a3f4cc2ac 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -59,7 +59,10 @@ function getGlobalCacheKey( .substring(0, length); } -function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { +function getCacheKeyFunction( + globalCacheKey: string, + length: number, +): GetCacheKeyFunction { return (sourceText, sourcePath, configString, options) => { // Jest 27 passes a single options bag which contains `configString` rather than as a separate argument. // We can hide that API difference, though, so this module is usable for both jest@<27 and jest@>=27 @@ -75,7 +78,7 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { .update('\0', 'utf8') .update(instrument ? 'instrument' : '') .digest('hex') - .substring(0, 32); + .substring(0, length); }; } @@ -92,5 +95,5 @@ export default function createCacheKey( values: Array = [], length = process.platform === 'win32' ? 16 : 32, ): GetCacheKeyFunction { - return getCacheKeyFunction(getGlobalCacheKey(files, values, length)); + return getCacheKeyFunction(getGlobalCacheKey(files, values, length), length); } From 9536286ebb5c9ce2ee408719bb23c9fda387528a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Feb 2023 09:16:02 +0100 Subject: [PATCH 28/29] move changelog entry again --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94b33ff331ed..645ff7353e4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,11 @@ - `[jest-changed-files]` Support Sapling ([#13941](https://github.com/facebook/jest/pull/13941)) - `[jest-cli, jest-config, @jest/core, jest-haste-map, @jest/reporters, jest-runner, jest-runtime, @jest/types]` Add `workerThreads` configuration option to allow using [worker threads](https://nodejs.org/dist/latest/docs/api/worker_threads.html) for parallelization ([#13939](https://github.com/facebook/jest/pull/13939)) +- `[@jest/create-cache-key-function]` Allow passing `length` argument to `createCacheKey()` function and set its default value to `16` on Windows ([#13827](https://github.com/facebook/jest/pull/13827)) - `[jest-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937)) ### Fixes -- `[@jest/create-cache-key-function]` Allow passing `length` argument to `createCacheKey()` function and set its default value to `16` on Windows ([#13827](https://github.com/facebook/jest/pull/13827)) - ### Chore & Maintenance ### Performance From e0d2f3926a11f9977153d6f5ad548072ff07b5a7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Feb 2023 09:30:51 +0100 Subject: [PATCH 29/29] default to linux platform in tests --- .../src/__tests__/index.test.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/jest-create-cache-key-function/src/__tests__/index.test.ts b/packages/jest-create-cache-key-function/src/__tests__/index.test.ts index 3060f5548de2..f15be30ea631 100644 --- a/packages/jest-create-cache-key-function/src/__tests__/index.test.ts +++ b/packages/jest-create-cache-key-function/src/__tests__/index.test.ts @@ -9,22 +9,20 @@ import {interopRequireDefault} from 'jest-util'; let NODE_ENV: string; let BABEL_ENV: string; -let PLATFORM: string; beforeEach(() => { NODE_ENV = process.env.NODE_ENV; process.env.NODE_ENV = 'test'; BABEL_ENV = process.env.BABEL_ENV; process.env.BABEL_ENV = 'test'; - PLATFORM = process.platform; + Object.defineProperty(process, 'platform', { + value: 'linux', + }); }); afterEach(() => { process.env.NODE_ENV = NODE_ENV; process.env.BABEL_ENV = BABEL_ENV; - Object.defineProperty(process, 'platform', { - value: PLATFORM, - }); }); test('creation of a cache key', () => { @@ -51,7 +49,7 @@ test('creation of a cache key', () => { }); test('creation of a cache key on win32', () => { - Object.defineProperty(process, 'platform', { + Object.defineProperty(process, 'platform', { value: 'win32', }); const createCacheKeyFunction = interopRequireDefault(