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

Unify worker module map transmission w/ small perf benefit. #8237

Merged
merged 6 commits into from Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 12 additions & 17 deletions packages/jest-runner/src/index.ts
Expand Up @@ -103,16 +103,13 @@ class TestRunner {
onResult: OnTestSuccess,
onFailure: OnTestFailure,
) {
let resolvers: Map<string, SerializableResolver> | undefined = undefined;
if (watcher.isWatchMode()) {
resolvers = new Map();
for (const test of tests) {
if (!resolvers.has(test.context.config.name)) {
resolvers.set(test.context.config.name, {
config: test.context.config,
serializableModuleMap: test.context.moduleMap.toJSON(),
});
}
const resolvers: Map<string, SerializableResolver> = new Map();
for (const test of tests) {
if (!resolvers.has(test.context.config.name)) {
resolvers.set(test.context.config.name, {
config: test.context.config,
serializableModuleMap: test.context.moduleMap.toJSON(),
});
}
}

Expand All @@ -121,13 +118,11 @@ class TestRunner {
forkOptions: {stdio: 'pipe'},
maxRetries: 3,
numWorkers: this._globalConfig.maxWorkers,
setupArgs: resolvers
? [
{
serializableResolvers: Array.from(resolvers.values()),
},
]
: undefined,
setupArgs: [
{
serializableResolvers: Array.from(resolvers.values()),
},
],
}) as WorkerInterface;

if (worker.getStdout()) worker.getStdout().pipe(process.stdout);
Expand Down
37 changes: 14 additions & 23 deletions packages/jest-runner/src/testWorker.ts
Expand Up @@ -53,34 +53,25 @@ const formatError = (error: string | ErrorWithCode): SerializableError => {
};

const resolvers = new Map<string, Resolver>();
const getResolver = (config: Config.ProjectConfig, moduleMap?: ModuleMap) => {
const name = config.name;
if (moduleMap || !resolvers.has(name)) {
resolvers.set(
name,
Runtime.createResolver(
config,
moduleMap || Runtime.createHasteMap(config).readModuleMap(),
),
);
const getResolver = (config: Config.ProjectConfig) => {
const resolver = resolvers.get(config.name);
if (!resolver) {
throw new Error('Cannot find resolver for: ' + config.name);
}
return resolvers.get(name)!;
return resolver;
};

export function setup(setupData?: {
export function setup(setupData: {
serializableResolvers: Array<SerializableResolver>;
}) {
// Setup data is only used in watch mode to pass the latest version of all
// module maps that will be used during the test runs. Otherwise, module maps
// are loaded from disk as needed.
if (setupData) {
for (const {
config,
serializableModuleMap,
} of setupData.serializableResolvers) {
const moduleMap = HasteMap.ModuleMap.fromJSON(serializableModuleMap);
getResolver(config, moduleMap);
}
// Setup data is used in watch mode to pass all module maps that will be used
// during the test runs.
for (const {
config,
serializableModuleMap,
} of setupData.serializableResolvers) {
const moduleMap = HasteMap.ModuleMap.fromJSON(serializableModuleMap);
resolvers.set(config.name, Runtime.createResolver(config, moduleMap));
}
}

Expand Down