Skip to content

Commit e3189c4

Browse files
committed
Don’t post-process HasteMap and remove one caching layer.
Runtime now directly operates with the data structure from the HasteMap. Workers now read directly from `jest-haste-map`’s cache.
1 parent 10f8caf commit e3189c4

6 files changed

Lines changed: 78 additions & 86 deletions

File tree

packages/jest-haste-map/src/index.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,19 @@ class HasteMap {
7373
build() {
7474
if (!this._buildPromise) {
7575
this._buildPromise = this._buildFileMap()
76-
.then(data => this._buildHasteMap(data))
77-
.then(data => this._persist(data));
76+
.then(data => this._buildHasteMap(data));
7877
}
7978
return this._buildPromise;
8079
}
8180

81+
persist(data) {
82+
return writeFile(this._cachePath, JSON.stringify(data)).then(() => data);
83+
}
84+
85+
read() {
86+
return this._parse(fs.readFileSync(this._cachePath, 'utf-8'));
87+
}
88+
8289
matchFiles(pattern) {
8390
if (!(pattern instanceof RegExp)) {
8491
pattern = new RegExp(pattern);
@@ -95,11 +102,10 @@ class HasteMap {
95102
}
96103

97104
_buildFileMap() {
98-
const dataPromise = this._options.resetCache
99-
? Promise.resolve(this._createEmptyMap())
100-
: readFile(this._cachePath, 'utf-8').then(data => this._parse(data));
105+
const read = this._options.resetCache ? this._createEmptyMap : this.read;
101106

102-
return dataPromise
107+
return Promise.resolve()
108+
.then(() => read.call(this))
103109
.catch(() => this._createEmptyMap())
104110
.then(data => this._crawl(data));
105111
}
@@ -159,17 +165,12 @@ class HasteMap {
159165

160166
_parse(data) {
161167
data = JSON.parse(data);
162-
Object.setPrototypeOf(data.clocks, null);
163-
Object.setPrototypeOf(data.files, null);
164-
Object.setPrototypeOf(data.map, null);
168+
for (const key in data) {
169+
Object.setPrototypeOf(data[key], null);
170+
}
165171
return data;
166172
}
167173

168-
_persist(data) {
169-
return writeFile(this._cachePath, JSON.stringify(data))
170-
.then(() => data);
171-
}
172-
173174
_crawl(data) {
174175
const crawl =
175176
(canUseWatchman && this._options.useWatchman) ? watchmanCrawl : nodeCrawl;

src/Runtime/Runtime.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
'use strict';
1010

11+
const HasteMap = require('jest-haste-map');
12+
1113
const constants = require('../constants');
1214
const fs = require('graceful-fs');
1315
const moduleMocker = require('jest-mock');
@@ -43,9 +45,9 @@ class Runtime {
4345

4446
this._shouldAutoMock = config.automock;
4547
this._extensions = config.moduleFileExtensions.map(ext => '.' + ext);
48+
this._defaultPlatform = config.haste.defaultPlatform;
4649

47-
this._modules = moduleMap.modules;
48-
this._packages = moduleMap.packages;
50+
this._modules = moduleMap.map;
4951
this._mocks = moduleMap.mocks;
5052

5153
if (config.collectCoverage) {
@@ -437,12 +439,25 @@ class Runtime {
437439
);
438440
}
439441

440-
_getModule(name) {
441-
return this._modules[name];
442+
_getModule(name, type) {
443+
if (!type) {
444+
type = 'module';
445+
}
446+
447+
const map = this._modules[name];
448+
if (map) {
449+
const module =
450+
map[this._defaultPlatform] || map[HasteMap.GENERIC_PLATFORM];
451+
if (module && module.type == type) {
452+
return module.path;
453+
}
454+
}
455+
456+
return null;
442457
}
443458

444459
_getPackage(name) {
445-
return this._packages[name];
460+
return this._getModule(name, 'package');
446461
}
447462

448463
_getMockModule(name) {

src/TestRunner.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const Test = require('./Test');
1212

1313
const fs = require('graceful-fs');
1414
const getCacheFilePath = require('jest-haste-map').getCacheFilePath;
15-
const getCacheKey = require('./lib/getCacheKey');
1615
const mkdirp = require('mkdirp');
1716
const path = require('path');
1817
const promisify = require('./lib/promisify');
@@ -391,18 +390,9 @@ class TestRunner {
391390
);
392391
}
393392

394-
_persistModuleMap(moduleMap) {
395-
const cacheFile = getCacheFilePath(
396-
this._config.cacheDirectory,
397-
getCacheKey('jest-module-map', this._config)
398-
);
399-
return promisify(fs.writeFile)(cacheFile, JSON.stringify(moduleMap));
400-
}
401-
402393
_createParallelTestRun(testPaths, onTestResult, onRunFailure) {
403394
const config = this._config;
404395
return this._resolver.getHasteMap()
405-
.then(moduleMap => this._persistModuleMap(moduleMap))
406396
.then(() => {
407397
const farm = workerFarm({
408398
autoStart: true,

src/TestWorker.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ process.on('uncaughtException', err => {
1515

1616
const Test = require('./Test');
1717

18-
const fs = require('graceful-fs');
19-
const getCacheFilePath = require('jest-haste-map').getCacheFilePath;
20-
const getCacheKey = require('./lib/getCacheKey');
18+
const createHasteMap = require('./lib/createHasteMap');
2119

2220
const formatError = error => {
2321
if (typeof error === 'string') {
@@ -40,11 +38,7 @@ let moduleMap;
4038
module.exports = (data, callback) => {
4139
try {
4240
if (!moduleMap) {
43-
const cacheFile = getCacheFilePath(
44-
data.config.cacheDirectory,
45-
getCacheKey('jest-module-map', data.config)
46-
);
47-
moduleMap = JSON.parse(fs.readFileSync(cacheFile));
41+
moduleMap = createHasteMap(data.config).read();
4842
}
4943

5044
new Test(data.path, data.config, moduleMap)

src/lib/createHasteMap.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*/
8+
9+
'use strict';
10+
11+
const HasteMap = require('jest-haste-map');
12+
13+
module.exports = function createHasteMap(config, options) {
14+
const extensions = Array.from(new Set(
15+
config.moduleFileExtensions.concat(config.testFileExtensions)
16+
));
17+
const ignorePattern = new RegExp(
18+
[config.cacheDirectory].concat(config.modulePathIgnorePatterns).join('|')
19+
);
20+
21+
return new HasteMap({
22+
cacheDirectory: config.cacheDirectory,
23+
extensions,
24+
ignorePattern,
25+
providesModuleNodeModules: config.haste.providesModuleNodeModules,
26+
platforms: config.haste.platforms || ['ios', 'android'],
27+
resetCache: options && options.resetCache,
28+
roots: config.testPathDirs,
29+
useWatchman: config.watchman,
30+
});
31+
};

src/resolvers/HasteResolver.js

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
'use strict';
1010

11-
const HasteMap = require('jest-haste-map');
12-
11+
const createHasteMap = require('../lib/createHasteMap');
1312
const path = require('path');
1413

1514
/* eslint-disable max-len */
@@ -19,36 +18,23 @@ const REQUIRE_EXTENSIONS_PATTERN = /(\b(?:require\s*?\.\s*?(?:requireActual|requ
1918
class HasteResolver {
2019

2120
constructor(config, options) {
22-
const extensions = Array.from(new Set(
23-
config.moduleFileExtensions.concat(config.testFileExtensions)
24-
));
25-
const ignorePattern = new RegExp(
26-
[config.cacheDirectory].concat(config.modulePathIgnorePatterns).join('|')
27-
);
28-
21+
this._map = createHasteMap(config, options);
2922
this._defaultPlatform = config.haste.defaultPlatform;
3023
this._mocksPattern = new RegExp(config.mocksPattern);
31-
3224
this._mappedModuleNames = Object.create(null);
3325
if (config.moduleNameMapper.length) {
3426
config.moduleNameMapper.forEach(
3527
map => this._mappedModuleNames[map[1]] = new RegExp(map[0])
3628
);
3729
}
3830

39-
this._map = new HasteMap({
40-
cacheDirectory: options.cacheDirectory,
41-
extensions,
42-
ignorePattern,
43-
providesModuleNodeModules: config.haste.providesModuleNodeModules,
44-
platforms: config.haste.platforms || ['ios', 'android'],
45-
resetCache: options.resetCache,
46-
roots: config.testPathDirs,
47-
useWatchman: config.watchman,
48-
});
49-
50-
// warm-up
51-
this._map.build();
31+
// warm-up and cache mocks
32+
this._hasteMapPromise = this._map.build().then(data =>
33+
this._getAllMocks().then(mocks => {
34+
data.mocks = mocks;
35+
return this._map.persist(data);
36+
})
37+
);
5238

5339
/*extractRequires: code => {
5440
const data = HasteMap.extractRequires(code);
@@ -84,32 +70,7 @@ class HasteResolver {
8470
}
8571

8672
getHasteMap() {
87-
if (this._mapPromise) {
88-
return this._mapPromise;
89-
}
90-
91-
return this._mapPromise = Promise.all([
92-
this._map.build(),
93-
this._getAllMocks(),
94-
]).then(data => this._getModuleMap(data[0].map, data[1]));
95-
}
96-
97-
_getModuleMap(map, mocks) {
98-
const modules = Object.create(null);
99-
const packages = Object.create(null);
100-
for (const name in map) {
101-
const module =
102-
map[name][this._defaultPlatform] ||
103-
map[name][HasteMap.GENERIC_PLATFORM];
104-
if (module) {
105-
if (module.type == 'package') {
106-
packages[name] = module.path;
107-
} else {
108-
modules[name] = module.path;
109-
}
110-
}
111-
}
112-
return {modules, mocks, packages};
73+
return this._hasteMapPromise;
11374
}
11475

11576
_getAllMocks() {

0 commit comments

Comments
 (0)