Skip to content

Commit

Permalink
Ignore rootDir to determine the cache key of transformed files in bab…
Browse files Browse the repository at this point in the history
…el-jest
  • Loading branch information
rubennorte committed Feb 21, 2019
1 parent 44bfa6e commit 3a5f5ea
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@
- `[expect]`: Improve report when matcher fails, part 8 ([#7876](https://github.com/facebook/jest/pull/7876))
- `[pretty-format]` Support `React.memo` ([#7891](https://github.com/facebook/jest/pull/7891))
- `[jest-config]` Print error information on preset normalization error ([#7935](https://github.com/facebook/jest/pull/7935))
- `[babel-jest]` Ignore `rootDir` to determine the cache key of transformed files ([#7947](https://github.com/facebook/jest/pull/7947))

### Fixes

Expand Down
1 change: 1 addition & 0 deletions packages/babel-jest/package.json
Expand Up @@ -17,6 +17,7 @@
"babel-plugin-istanbul": "^5.1.0",
"babel-preset-jest": "^24.1.0",
"chalk": "^2.4.2",
"jest-regex-util": "^24.0.0",
"slash": "^2.0.0"
},
"devDependencies": {
Expand Down
36 changes: 36 additions & 0 deletions packages/babel-jest/src/__tests__/index.ts
Expand Up @@ -39,3 +39,39 @@ test(`Returns source string with inline maps when no transformOptions is passed`
expect(result.map.sources).toEqual(['dummy_path.js']);
expect(JSON.stringify(result.map.sourcesContent)).toMatch('customMultiply');
});

test('getCacheKey does not depend on the rootDir', () => {
const getCacheKeyArgs = rootDir => {
const config = {
cwd: rootDir,
rootDir,
};

return [
'// Some code',
`${rootDir}/foo/bar.js`,
JSON.stringify(config),
{config, instrument: true, rootDir: config.rootDir},
];
};

jest.mock('@babel/core');

require('@babel/core').loadPartialConfig = options => ({
babelrc: `${options.cwd}/foo/.babelrc`,
config: `${options.cwd}/babel.config.js`,
options: {
cwd: options.cwd,
someOtherOption: `${options.cwd}/foo`,
},
});

const cacheKeyForRoot1 = babelJest.getCacheKey(
...getCacheKeyArgs('/root1/dir'),
);
const cacheKeyForRoot2 = babelJest.getCacheKey(
...getCacheKeyArgs('/root2/dir'),
);

expect(cacheKeyForRoot1).toEqual(cacheKeyForRoot2);
});
34 changes: 26 additions & 8 deletions packages/babel-jest/src/index.ts
Expand Up @@ -17,6 +17,7 @@ import {
transformSync as babelTransform,
} from '@babel/core';
import chalk from 'chalk';
import {escapeStrForRegex} from 'jest-regex-util';
import slash from 'slash';

const THIS_FILE = fs.readFileSync(__filename);
Expand Down Expand Up @@ -65,33 +66,50 @@ const createTransformer = (
return babelConfig;
}

const rootDirRegExpCache: {[rootDir: string]: RegExp} = Object.create(null);

function getRootDirRegExp(rootDir: Config.Path): RegExp {
if (!rootDirRegExpCache[rootDir]) {
rootDirRegExpCache[rootDir] = new RegExp(
`(['"])${escapeStrForRegex(rootDir)}(\\1|${escapeStrForRegex(
path.sep,
)})`,
'g',
);
}

return rootDirRegExpCache[rootDir];
}

const transformer: BabelJestTransformer = {
canInstrument: true,
createTransformer,
getCacheKey(
fileData,
filename,
configString,
_configString,
{config, instrument, rootDir},
) {
const babelOptions = loadBabelConfig(config.cwd, filename);
const configPath = [
babelOptions.config || '',
babelOptions.babelrc || '',
];
const configPath = [babelOptions.config, babelOptions.babelrc].map(
configPath => configPath && path.relative(rootDir, configPath),
);

return crypto
.createHash('md5')
.update(THIS_FILE)
.update('\0', 'utf8')
.update(JSON.stringify(babelOptions.options))
.update(
JSON.stringify(babelOptions.options).replace(
getRootDirRegExp(rootDir),
'$1<rootDir>$2',
),
)
.update('\0', 'utf8')
.update(fileData)
.update('\0', 'utf8')
.update(path.relative(rootDir, filename))
.update('\0', 'utf8')
.update(configString)
.update('\0', 'utf8')
.update(configPath.join(''))
.update('\0', 'utf8')
.update(instrument ? 'instrument' : '')
Expand Down

0 comments on commit 3a5f5ea

Please sign in to comment.