Skip to content

Commit

Permalink
fix: cache transforms within a worker
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Aug 31, 2019
1 parent 4482e71 commit 382df36
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@
- `[jest-mock]` Fix for mockReturnValue overriding mockImplementationOnce ([#8398](https://github.com/facebook/jest/pull/8398))
- `[jest-snapshot]` Remove only the added newlines in multiline snapshots ([#8859](https://github.com/facebook/jest/pull/8859))
- `[jest-snapshot]` Distinguish empty string from external snapshot not written ([#8880](https://github.com/facebook/jest/pull/8880))
- `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890))

### Chore & Maintenance

Expand Down
22 changes: 22 additions & 0 deletions e2e/__tests__/transform.test.ts
Expand Up @@ -185,3 +185,25 @@ describe('transformer-config', () => {
expect(stdout).toMatchSnapshot();
});
});

describe('transformer caching', () => {
const dir = path.resolve(__dirname, '..', 'transform/cache');
const dirRegexp = new RegExp('^' + dir);

it('does not rerun transform within worker', () => {
// --no-cache because babel can cache stuff and result in false green
const {stdout} = runJest(dir, ['--no-cache', '-w=2']);

const loggedFiles = stdout.split('\n').map(line => {
expect(line).toMatch(dirRegexp);

return line.replace(dirRegexp, '<rootDir>');
});

// We run with 2 workers, so the file should be transformed twice
expect(loggedFiles).toEqual([
'<rootDir>/common-file.js',
'<rootDir>/common-file.js',
]);
});
});
12 changes: 12 additions & 0 deletions e2e/transform/cache/__tests__/aTests.js
@@ -0,0 +1,12 @@
/**
* 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.
*/

const phrase = require('../common-file');

test('A', () => {
expect(phrase).toBe('hello');
});
12 changes: 12 additions & 0 deletions e2e/transform/cache/__tests__/bTests.js
@@ -0,0 +1,12 @@
/**
* 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.
*/

const phrase = require('../common-file');

test('B', () => {
expect(phrase).toBe('hello');
});
12 changes: 12 additions & 0 deletions e2e/transform/cache/__tests__/cTests.js
@@ -0,0 +1,12 @@
/**
* 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.
*/

const phrase = require('../common-file');

test('C', () => {
expect(phrase).toBe('hello');
});
12 changes: 12 additions & 0 deletions e2e/transform/cache/__tests__/dTests.js
@@ -0,0 +1,12 @@
/**
* 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.
*/

const phrase = require('../common-file');

test('D', () => {
expect(phrase).toBe('hello');
});
8 changes: 8 additions & 0 deletions e2e/transform/cache/common-file.js
@@ -0,0 +1,8 @@
/**
* 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.
*/

module.exports = 'hello';
10 changes: 10 additions & 0 deletions e2e/transform/cache/package.json
@@ -0,0 +1,10 @@
{
"name": "cache",
"version": "1.0.0",
"jest": {
"testEnvironment": "node",
"transform": {
"^.+\\.js$": "<rootDir>/transformer.js"
}
}
}
16 changes: 16 additions & 0 deletions e2e/transform/cache/transformer.js
@@ -0,0 +1,16 @@
/**
* 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.
*/

module.exports = {
process(src, path) {
if (path.includes('common')) {
console.log(path);
}

return src;
},
};
12 changes: 5 additions & 7 deletions packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -43,10 +43,7 @@ const {version: VERSION} = require('../package.json');
// This data structure is used to avoid recalculating some data every time that
// we need to transform a file. Since ScriptTransformer is instantiated for each
// file we need to keep this object in the local scope of this module.
const projectCaches: WeakMap<
Config.ProjectConfig,
ProjectCache
> = new WeakMap();
const projectCaches = new Map<string, ProjectCache>();

// To reset the cache for specific changesets (rather than package version).
const CACHE_VERSION = '1';
Expand Down Expand Up @@ -74,17 +71,18 @@ export default class ScriptTransformer {
this._transformCache = new Map();
this._transformConfigCache = new Map();

let projectCache = projectCaches.get(config);
const configString = stableStringify(this._config);
let projectCache = projectCaches.get(configString);

if (!projectCache) {
projectCache = {
configString: stableStringify(this._config),
configString,
ignorePatternsRegExp: calcIgnorePatternRegExp(this._config),
transformRegExp: calcTransformRegExp(this._config),
transformedFiles: new Map(),
};

projectCaches.set(config, projectCache);
projectCaches.set(configString, projectCache);
}

this._cache = projectCache;
Expand Down

0 comments on commit 382df36

Please sign in to comment.