jest/packages/jest-create-cache-key-function
Simen Bekkhus 79a4ff4985
fix: add ESM exports of all modules manually (#14661)
2023-10-30 11:01:22 +00:00
..
src fix: optimize getGlobalCacheKey (#14455) 2023-08-29 08:12:28 +02:00
.npmignore chore: make sure to exclude .eslintcache from published module (#13832) 2023-01-28 11:57:53 +01:00
README.md fix: lower length of key to improve usage in win32 (#13827) 2023-02-23 09:44:32 +01:00
package.json fix: add ESM exports of all modules manually (#14661) 2023-10-30 11:01:22 +00:00
tsconfig.json chore(type tests): bump `jest-runner-tsd` (#12299) 2022-02-04 19:43:49 +01:00

README.md

jest-create-cache-key-function

This module creates a function which is used for generating cache keys used by code transformers in Jest.

Install

$ npm install --save-dev @jest/create-cache-key-function

API

createCacheKey(files?: Array<string>, values?: Array<String>, length?: number): GetCacheKeyFunction

Returns a function that can be used to generate cache keys based on source code of provided files and provided values.

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. The default is 32, or 16 on Windows.

Note:

The source code for your test is already taken into account when generating the cache key. The files array should be used to provide files that are not directly related to your code such as external configuration files.

Usage

Here is some sample usage code while creating a new transformer for Jest

const createCacheKeyFunction =
  require('@jest/create-cache-key-function').default;

const filesToAccountFor = [
  __filename,
  require.resolve('some-package-name/package.json'),
];

const valuesToAccountFor = [process.env.SOME_LOCAL_ENV, 'Some_Other_Value'];

module.exports = {
  process(src, filename, config, options) {},
  getCacheKey: createCacheKeyFunction(filesToAccountFor, valuesToAccountFor),
};