From 341919dbbedbfafadefeb404eae6930ae22b9178 Mon Sep 17 00:00:00 2001 From: mrazauskas <72159681+mrazauskas@users.noreply.github.com> Date: Sat, 4 Dec 2021 10:48:43 +0200 Subject: [PATCH 1/4] fix: add `process.version` to cache key --- packages/babel-jest/src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/babel-jest/src/index.ts b/packages/babel-jest/src/index.ts index da3f2d17d72f..9db0db4c54d7 100644 --- a/packages/babel-jest/src/index.ts +++ b/packages/babel-jest/src/index.ts @@ -98,6 +98,8 @@ function getCacheKeyFromConfig( .update(process.env.NODE_ENV || '') .update('\0', 'utf8') .update(process.env.BABEL_ENV || '') + .update('\0', 'utf8') + .update(process.version) .digest('hex'); } From 5f45e4a22c49c98e13ffdeacfeaf1f7b445927ad Mon Sep 17 00:00:00 2001 From: mrazauskas <72159681+mrazauskas@users.noreply.github.com> Date: Sun, 5 Dec 2021 10:34:44 +0200 Subject: [PATCH 2/4] add unit test --- .../src/__tests__/getCacheKey.test.ts | 211 ++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 packages/babel-jest/src/__tests__/getCacheKey.test.ts diff --git a/packages/babel-jest/src/__tests__/getCacheKey.test.ts b/packages/babel-jest/src/__tests__/getCacheKey.test.ts new file mode 100644 index 000000000000..63e38cff3cb3 --- /dev/null +++ b/packages/babel-jest/src/__tests__/getCacheKey.test.ts @@ -0,0 +1,211 @@ +/** + * 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 type {TransformOptions as BabelTransformOptions} from '@babel/core'; +import type {TransformOptions as JestTransformOptions} from '@jest/transform'; +import babelJest from '../index'; + +const processVersion = process.version; +const nodeEnv = process.env.NODE_ENV; +const babelEnv = process.env.BABEL_ENV; + +afterEach(() => { + jest.resetModules(); + + if (process.version === 'new-node-version') { + process.version = processVersion; + } + + if (process.env.NODE_ENV === 'NEW_NODE_ENV') { + process.env.NODE_ENV = nodeEnv; + } + + if (process.env.BABEL_ENV === 'NEW_BABEL_ENV') { + process.env.BABEL_ENV = babelEnv; + } +}); + +describe('getCacheKey', () => { + const sourceText = 'mock source'; + const sourcePath = 'mock-source-path.js'; + + const transformOptions = { + config: {rootDir: 'mock-root-dir'}, + configString: 'mock-config-string', + instrument: true, + } as JestTransformOptions; + + const oldCacheKey = babelJest.getCacheKey( + sourceText, + sourcePath, + transformOptions, + ); + + test('returns cache key hash', () => { + expect(oldCacheKey.length).toEqual(32); + }); + + test('if `THIS_FILE` value is changing', () => { + jest.doMock('graceful-fs', () => ({ + readFileSync: () => 'new this file', + })); + + const {default: babelJest}: typeof import('../index') = require('../index'); + + const newCacheKey = babelJest.getCacheKey( + sourceText, + sourcePath, + transformOptions, + ); + + expect(oldCacheKey).not.toEqual(newCacheKey); + }); + + test('if `babelOptions.options` value is changing', () => { + jest.doMock('../loadBabelConfig', () => { + const babel: typeof import('@babel/core') = require('@babel/core'); + + return { + loadPartialConfig: (options: BabelTransformOptions) => ({ + ...babel.loadPartialConfig(options), + options: 'new-options', + }), + }; + }); + + const {default: babelJest}: typeof import('../index') = require('../index'); + + const newCacheKey = babelJest.getCacheKey( + sourceText, + sourcePath, + transformOptions, + ); + + expect(oldCacheKey).not.toEqual(newCacheKey); + }); + + test('if `sourceText` value is changing', () => { + const newCacheKey = babelJest.getCacheKey( + 'new source text', + sourcePath, + transformOptions, + ); + + expect(oldCacheKey).not.toEqual(newCacheKey); + }); + + test('if `sourcePath` value is changing', () => { + const newCacheKey = babelJest.getCacheKey( + sourceText, + 'new-source-path.js', + transformOptions, + ); + + expect(oldCacheKey).not.toEqual(newCacheKey); + }); + + test('if `configString` value is changing', () => { + const newCacheKey = babelJest.getCacheKey(sourceText, sourcePath, { + ...transformOptions, + configString: 'new-config-string', + }); + + expect(oldCacheKey).not.toEqual(newCacheKey); + }); + + test('if `babelOptions.config` value is changing', () => { + jest.doMock('../loadBabelConfig', () => { + const babel: typeof import('@babel/core') = require('@babel/core'); + + return { + loadPartialConfig: (options: BabelTransformOptions) => ({ + ...babel.loadPartialConfig(options), + config: 'new-config', + }), + }; + }); + + const {default: babelJest}: typeof import('../index') = require('../index'); + + const newCacheKey = babelJest.getCacheKey( + sourceText, + sourcePath, + transformOptions, + ); + + expect(oldCacheKey).not.toEqual(newCacheKey); + }); + + test('if `babelOptions.babelrc` value is changing', () => { + jest.doMock('../loadBabelConfig', () => { + const babel: typeof import('@babel/core') = require('@babel/core'); + + return { + loadPartialConfig: (options: BabelTransformOptions) => ({ + ...babel.loadPartialConfig(options), + babelrc: 'new-babelrc', + }), + }; + }); + + const {default: babelJest}: typeof import('../index') = require('../index'); + + const newCacheKey = babelJest.getCacheKey( + sourceText, + sourcePath, + transformOptions, + ); + + expect(oldCacheKey).not.toEqual(newCacheKey); + }); + + test('if `instrument` value is changing', () => { + const newCacheKey = babelJest.getCacheKey(sourceText, sourcePath, { + ...transformOptions, + instrument: false, + }); + + expect(oldCacheKey).not.toEqual(newCacheKey); + }); + + test('if `process.env.NODE_ENV` value is changing', () => { + process.env.NODE_ENV = 'NEW_NODE_ENV'; + + const newCacheKey = babelJest.getCacheKey( + sourceText, + sourcePath, + transformOptions, + ); + + expect(oldCacheKey).not.toEqual(newCacheKey); + }); + + test('if `process.env.BABEL_ENV` value is changing', () => { + process.env.BABEL_ENV = 'NEW_BABEL_ENV'; + + const newCacheKey = babelJest.getCacheKey( + sourceText, + sourcePath, + transformOptions, + ); + + expect(oldCacheKey).not.toEqual(newCacheKey); + }); + + test('if node version is changing', () => { + delete process.version; + process.version = 'new-node-version'; + + const newCacheKey = babelJest.getCacheKey( + sourceText, + sourcePath, + transformOptions, + ); + + expect(oldCacheKey).not.toEqual(newCacheKey); + }); +}); From aae274a2e9b788631bb97398b14bedd669cbab90 Mon Sep 17 00:00:00 2001 From: mrazauskas <72159681+mrazauskas@users.noreply.github.com> Date: Sun, 5 Dec 2021 10:42:11 +0200 Subject: [PATCH 3/4] add change log entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9b536940d4b..dfdf3e7202aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ### Fixes +- `[babel-jest]` Add `process.version` chunk to the cache key ([#12122](https://github.com/facebook/jest/pull/12122)) - `[jest-worker]` Filter `execArgv` correctly ([#12097](https://github.com/facebook/jest/pull/12097)) ## 27.4.0 From d3b85e35595e2020b05f6934380c2e106ca47e8a Mon Sep 17 00:00:00 2001 From: mrazauskas <72159681+mrazauskas@users.noreply.github.com> Date: Sun, 5 Dec 2021 10:44:46 +0200 Subject: [PATCH 4/4] fix change log --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dfdf3e7202aa..a4a738d87dae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[babel-jest]` Add `process.version` chunk to the cache key ([#12122](https://github.com/facebook/jest/pull/12122)) + ### Chore & Maintenance ### Performance @@ -12,7 +14,6 @@ ### Fixes -- `[babel-jest]` Add `process.version` chunk to the cache key ([#12122](https://github.com/facebook/jest/pull/12122)) - `[jest-worker]` Filter `execArgv` correctly ([#12097](https://github.com/facebook/jest/pull/12097)) ## 27.4.0