Skip to content

Commit

Permalink
Cache reading of test file to avoid duplicate calls (jestjs#3125)
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee authored and skovhus committed Apr 29, 2017
1 parent 1be9315 commit e5400ef
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/jest-cli/src/runTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ function runTest(path: Path, config: Config, resolver: Resolver) {
BufferedConsole.write([], type, message, 4),
),
);
const cacheFS = {[path]: testSource};
setGlobal(env.global, 'console', testConsole);
const runtime = new ModuleLoader(config, env, resolver);
const runtime = new ModuleLoader(config, env, resolver, cacheFS);
const start = Date.now();
return TestRunner(config, env, runtime, path)
.then((result: TestResult) => {
Expand Down
20 changes: 16 additions & 4 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,17 @@ class Runtime {
_unmockList: ?RegExp;
_virtualMocks: BooleanObject;

constructor(config: Config, environment: Environment, resolver: Resolver) {
constructor(
config: Config,
environment: Environment,
resolver: Resolver,
cacheFS?: {[path: string]: string}
) {
this._moduleRegistry = Object.create(null);
this._internalModuleRegistry = Object.create(null);
this._mockRegistry = Object.create(null);
this._sourceMapRegistry = Object.create(null);
this._cacheFS = cacheFS || Object.create(null);
this._config = config;
this._environment = environment;
this._resolver = resolver;
Expand Down Expand Up @@ -455,9 +461,15 @@ class Runtime {
localModule.paths = this._resolver.getModulePaths(dirname);
localModule.require = this._createRequireImplementation(filename, options);

const transformedFile = transform(filename, this._config, {
isInternalModule,
});
const transformedFile = transform(
filename,
this._config,
{
isInternalModule,
},
this._cacheFS[filename]
);


if (transformedFile.sourceMapPath) {
this._sourceMapRegistry[filename] = transformedFile.sourceMapPath;
Expand Down
12 changes: 10 additions & 2 deletions packages/jest-runtime/src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,10 @@ const transformAndBuildScript = (
config: Config,
options: ?Options,
instrument: boolean,
fileSource?: string
): BuiltTransformResult => {
const isInternalModule = !!(options && options.isInternalModule);
const content = stripShebang(fs.readFileSync(filename, 'utf8'));
const content = stripShebang(fileSource || fs.readFileSync(filename, 'utf8'));
let wrappedCode: string;
let sourceMapPath: ?string = null;
const willTransform = !isInternalModule &&
Expand Down Expand Up @@ -396,14 +397,21 @@ module.exports = (
filename: Path,
config: Config,
options: Options,
fileSource?: string
): BuiltTransformResult => {
const instrument = shouldInstrument(filename, config);
const scriptCacheKey = getScriptCacheKey(filename, config, instrument);
let result = cache.get(scriptCacheKey);
if (result) {
return result;
} else {
result = transformAndBuildScript(filename, config, options, instrument);
result = transformAndBuildScript(
filename,
config,
options,
instrument,
fileSource
);
cache.set(scriptCacheKey, result);
return result;
}
Expand Down

0 comments on commit e5400ef

Please sign in to comment.