Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cache transforms within a worker #8890

Merged
merged 3 commits into from
Sep 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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>();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not the correct fix, but with this change the tests pass at least, which is a good start 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think this is alright.


// 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