|
| 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 | +import type {TransformOptions as BabelTransformOptions} from '@babel/core'; |
| 9 | +import type {TransformOptions as JestTransformOptions} from '@jest/transform'; |
| 10 | +import babelJest from '../index'; |
| 11 | + |
| 12 | +const processVersion = process.version; |
| 13 | +const nodeEnv = process.env.NODE_ENV; |
| 14 | +const babelEnv = process.env.BABEL_ENV; |
| 15 | + |
| 16 | +afterEach(() => { |
| 17 | + jest.resetModules(); |
| 18 | + |
| 19 | + if (process.version === 'new-node-version') { |
| 20 | + process.version = processVersion; |
| 21 | + } |
| 22 | + |
| 23 | + if (process.env.NODE_ENV === 'NEW_NODE_ENV') { |
| 24 | + process.env.NODE_ENV = nodeEnv; |
| 25 | + } |
| 26 | + |
| 27 | + if (process.env.BABEL_ENV === 'NEW_BABEL_ENV') { |
| 28 | + process.env.BABEL_ENV = babelEnv; |
| 29 | + } |
| 30 | +}); |
| 31 | + |
| 32 | +describe('getCacheKey', () => { |
| 33 | + const sourceText = 'mock source'; |
| 34 | + const sourcePath = 'mock-source-path.js'; |
| 35 | + |
| 36 | + const transformOptions = { |
| 37 | + config: {rootDir: 'mock-root-dir'}, |
| 38 | + configString: 'mock-config-string', |
| 39 | + instrument: true, |
| 40 | + } as JestTransformOptions; |
| 41 | + |
| 42 | + const oldCacheKey = babelJest.getCacheKey( |
| 43 | + sourceText, |
| 44 | + sourcePath, |
| 45 | + transformOptions, |
| 46 | + ); |
| 47 | + |
| 48 | + test('returns cache key hash', () => { |
| 49 | + expect(oldCacheKey.length).toEqual(32); |
| 50 | + }); |
| 51 | + |
| 52 | + test('if `THIS_FILE` value is changing', () => { |
| 53 | + jest.doMock('graceful-fs', () => ({ |
| 54 | + readFileSync: () => 'new this file', |
| 55 | + })); |
| 56 | + |
| 57 | + const {default: babelJest}: typeof import('../index') = require('../index'); |
| 58 | + |
| 59 | + const newCacheKey = babelJest.getCacheKey( |
| 60 | + sourceText, |
| 61 | + sourcePath, |
| 62 | + transformOptions, |
| 63 | + ); |
| 64 | + |
| 65 | + expect(oldCacheKey).not.toEqual(newCacheKey); |
| 66 | + }); |
| 67 | + |
| 68 | + test('if `babelOptions.options` value is changing', () => { |
| 69 | + jest.doMock('../loadBabelConfig', () => { |
| 70 | + const babel: typeof import('@babel/core') = require('@babel/core'); |
| 71 | + |
| 72 | + return { |
| 73 | + loadPartialConfig: (options: BabelTransformOptions) => ({ |
| 74 | + ...babel.loadPartialConfig(options), |
| 75 | + options: 'new-options', |
| 76 | + }), |
| 77 | + }; |
| 78 | + }); |
| 79 | + |
| 80 | + const {default: babelJest}: typeof import('../index') = require('../index'); |
| 81 | + |
| 82 | + const newCacheKey = babelJest.getCacheKey( |
| 83 | + sourceText, |
| 84 | + sourcePath, |
| 85 | + transformOptions, |
| 86 | + ); |
| 87 | + |
| 88 | + expect(oldCacheKey).not.toEqual(newCacheKey); |
| 89 | + }); |
| 90 | + |
| 91 | + test('if `sourceText` value is changing', () => { |
| 92 | + const newCacheKey = babelJest.getCacheKey( |
| 93 | + 'new source text', |
| 94 | + sourcePath, |
| 95 | + transformOptions, |
| 96 | + ); |
| 97 | + |
| 98 | + expect(oldCacheKey).not.toEqual(newCacheKey); |
| 99 | + }); |
| 100 | + |
| 101 | + test('if `sourcePath` value is changing', () => { |
| 102 | + const newCacheKey = babelJest.getCacheKey( |
| 103 | + sourceText, |
| 104 | + 'new-source-path.js', |
| 105 | + transformOptions, |
| 106 | + ); |
| 107 | + |
| 108 | + expect(oldCacheKey).not.toEqual(newCacheKey); |
| 109 | + }); |
| 110 | + |
| 111 | + test('if `configString` value is changing', () => { |
| 112 | + const newCacheKey = babelJest.getCacheKey(sourceText, sourcePath, { |
| 113 | + ...transformOptions, |
| 114 | + configString: 'new-config-string', |
| 115 | + }); |
| 116 | + |
| 117 | + expect(oldCacheKey).not.toEqual(newCacheKey); |
| 118 | + }); |
| 119 | + |
| 120 | + test('if `babelOptions.config` value is changing', () => { |
| 121 | + jest.doMock('../loadBabelConfig', () => { |
| 122 | + const babel: typeof import('@babel/core') = require('@babel/core'); |
| 123 | + |
| 124 | + return { |
| 125 | + loadPartialConfig: (options: BabelTransformOptions) => ({ |
| 126 | + ...babel.loadPartialConfig(options), |
| 127 | + config: 'new-config', |
| 128 | + }), |
| 129 | + }; |
| 130 | + }); |
| 131 | + |
| 132 | + const {default: babelJest}: typeof import('../index') = require('../index'); |
| 133 | + |
| 134 | + const newCacheKey = babelJest.getCacheKey( |
| 135 | + sourceText, |
| 136 | + sourcePath, |
| 137 | + transformOptions, |
| 138 | + ); |
| 139 | + |
| 140 | + expect(oldCacheKey).not.toEqual(newCacheKey); |
| 141 | + }); |
| 142 | + |
| 143 | + test('if `babelOptions.babelrc` value is changing', () => { |
| 144 | + jest.doMock('../loadBabelConfig', () => { |
| 145 | + const babel: typeof import('@babel/core') = require('@babel/core'); |
| 146 | + |
| 147 | + return { |
| 148 | + loadPartialConfig: (options: BabelTransformOptions) => ({ |
| 149 | + ...babel.loadPartialConfig(options), |
| 150 | + babelrc: 'new-babelrc', |
| 151 | + }), |
| 152 | + }; |
| 153 | + }); |
| 154 | + |
| 155 | + const {default: babelJest}: typeof import('../index') = require('../index'); |
| 156 | + |
| 157 | + const newCacheKey = babelJest.getCacheKey( |
| 158 | + sourceText, |
| 159 | + sourcePath, |
| 160 | + transformOptions, |
| 161 | + ); |
| 162 | + |
| 163 | + expect(oldCacheKey).not.toEqual(newCacheKey); |
| 164 | + }); |
| 165 | + |
| 166 | + test('if `instrument` value is changing', () => { |
| 167 | + const newCacheKey = babelJest.getCacheKey(sourceText, sourcePath, { |
| 168 | + ...transformOptions, |
| 169 | + instrument: false, |
| 170 | + }); |
| 171 | + |
| 172 | + expect(oldCacheKey).not.toEqual(newCacheKey); |
| 173 | + }); |
| 174 | + |
| 175 | + test('if `process.env.NODE_ENV` value is changing', () => { |
| 176 | + process.env.NODE_ENV = 'NEW_NODE_ENV'; |
| 177 | + |
| 178 | + const newCacheKey = babelJest.getCacheKey( |
| 179 | + sourceText, |
| 180 | + sourcePath, |
| 181 | + transformOptions, |
| 182 | + ); |
| 183 | + |
| 184 | + expect(oldCacheKey).not.toEqual(newCacheKey); |
| 185 | + }); |
| 186 | + |
| 187 | + test('if `process.env.BABEL_ENV` value is changing', () => { |
| 188 | + process.env.BABEL_ENV = 'NEW_BABEL_ENV'; |
| 189 | + |
| 190 | + const newCacheKey = babelJest.getCacheKey( |
| 191 | + sourceText, |
| 192 | + sourcePath, |
| 193 | + transformOptions, |
| 194 | + ); |
| 195 | + |
| 196 | + expect(oldCacheKey).not.toEqual(newCacheKey); |
| 197 | + }); |
| 198 | + |
| 199 | + test('if node version is changing', () => { |
| 200 | + delete process.version; |
| 201 | + process.version = 'new-node-version'; |
| 202 | + |
| 203 | + const newCacheKey = babelJest.getCacheKey( |
| 204 | + sourceText, |
| 205 | + sourcePath, |
| 206 | + transformOptions, |
| 207 | + ); |
| 208 | + |
| 209 | + expect(oldCacheKey).not.toEqual(newCacheKey); |
| 210 | + }); |
| 211 | +}); |
0 commit comments