Skip to content

Commit

Permalink
fix: cache transforms within a worker (#8890)
Browse files Browse the repository at this point in the history
* fix: cache transforms within a worker

* windows test fix
  • Loading branch information
SimenB authored and thymikee committed Sep 8, 2019
1 parent a7f2d0d commit 04efbeb
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -29,6 +29,7 @@
- `[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-snapshot]` [**BREAKING**] Distinguish empty string from internal snapshot not written ([#8898](https://github.com/facebook/jest/pull/8898))
- `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890))

### Chore & Maintenance

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

describe('transformer caching', () => {
const dir = path.resolve(__dirname, '../transform/cache');
const transformedFile = path.resolve(dir, './common-file.js');

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');

// Verify any lines logged are _just_ the file we care about
loggedFiles.forEach(line => {
expect(line).toBe(transformedFile);
});

// We run with 2 workers, so the file should be transformed twice
expect(loggedFiles).toHaveLength(2);
});
});
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 04efbeb

Please sign in to comment.