chore: create tests groups per project (#20463)
This commit is contained in:
parent
7cb1b2db39
commit
2b499bd5d6
|
|
@ -74,6 +74,7 @@ export class Runner {
|
|||
options,
|
||||
reporter,
|
||||
plugins: [],
|
||||
testGroups: [],
|
||||
};
|
||||
|
||||
reporter.onConfigure(config);
|
||||
|
|
|
|||
|
|
@ -46,8 +46,8 @@ export type TaskRunnerState = {
|
|||
reporter: Multiplexer;
|
||||
config: FullConfigInternal;
|
||||
plugins: TestRunnerPlugin[];
|
||||
testGroups: TestGroup[];
|
||||
rootSuite?: Suite;
|
||||
testGroups?: TestGroup[];
|
||||
dispatcher?: Dispatcher;
|
||||
};
|
||||
|
||||
|
|
@ -59,7 +59,7 @@ export function createTaskRunner(config: FullConfigInternal, reporter: Multiplex
|
|||
if (config.globalSetup || config.globalTeardown)
|
||||
taskRunner.addTask('global setup', createGlobalSetupTask());
|
||||
taskRunner.addTask('load tests', createLoadTask());
|
||||
|
||||
taskRunner.addTask('shard tests', createTestGroupsTask());
|
||||
taskRunner.addTask('prepare to run', createRemoveOutputDirsTask());
|
||||
taskRunner.addTask('plugin begin', async ({ rootSuite, plugins }) => {
|
||||
for (const plugin of plugins)
|
||||
|
|
@ -158,22 +158,22 @@ export function createRemoveOutputDirsTask(): Task<TaskRunnerState> {
|
|||
function createLoadTask(): Task<TaskRunnerState> {
|
||||
return async (context, errors) => {
|
||||
const { config, reporter, options } = context;
|
||||
const rootSuite = await loadAllTests(config, reporter, options, errors);
|
||||
const testGroups = options.listOnly ? [] : createTestGroups(rootSuite.suites, config.workers);
|
||||
|
||||
context.rootSuite = rootSuite;
|
||||
context.testGroups = testGroups;
|
||||
if (errors.length)
|
||||
return;
|
||||
|
||||
context.rootSuite = await loadAllTests(config, reporter, options, errors);
|
||||
// Fail when no tests.
|
||||
if (!rootSuite.allTests().length && !context.options.passWithNoTests)
|
||||
if (!context.rootSuite.allTests().length && !context.options.passWithNoTests)
|
||||
throw new Error(`No tests found`);
|
||||
|
||||
if (!context.options.listOnly) {
|
||||
if (context.config.shard)
|
||||
filterForShard(context.config.shard, rootSuite, testGroups);
|
||||
context.config._maxConcurrentTestGroups = testGroups.length;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function createTestGroupsTask(): Task<TaskRunnerState> {
|
||||
return async context => {
|
||||
const { config, rootSuite } = context;
|
||||
|
||||
for (const projectSuite of rootSuite!.suites)
|
||||
context.testGroups.push(...createTestGroups(projectSuite, config.workers));
|
||||
|
||||
if (context.config.shard)
|
||||
filterForShard(context.config.shard, rootSuite!, context.testGroups);
|
||||
context.config._maxConcurrentTestGroups = context.testGroups.length;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ export type TestGroup = {
|
|||
tests: TestCase[];
|
||||
};
|
||||
|
||||
export function createTestGroups(projectSuites: Suite[], workers: number): TestGroup[] {
|
||||
export function createTestGroups(projectSuite: Suite, workers: number): TestGroup[] {
|
||||
// This function groups tests that can be run together.
|
||||
// Tests cannot be run together when:
|
||||
// - They belong to different projects - requires different workers.
|
||||
|
|
@ -62,49 +62,47 @@ export function createTestGroups(projectSuites: Suite[], workers: number): TestG
|
|||
};
|
||||
};
|
||||
|
||||
for (const projectSuite of projectSuites) {
|
||||
for (const test of projectSuite.allTests()) {
|
||||
let withWorkerHash = groups.get(test._workerHash);
|
||||
if (!withWorkerHash) {
|
||||
withWorkerHash = new Map();
|
||||
groups.set(test._workerHash, withWorkerHash);
|
||||
}
|
||||
let withRequireFile = withWorkerHash.get(test._requireFile);
|
||||
if (!withRequireFile) {
|
||||
withRequireFile = {
|
||||
general: createGroup(test),
|
||||
parallel: new Map(),
|
||||
parallelWithHooks: createGroup(test),
|
||||
};
|
||||
withWorkerHash.set(test._requireFile, withRequireFile);
|
||||
}
|
||||
for (const test of projectSuite.allTests()) {
|
||||
let withWorkerHash = groups.get(test._workerHash);
|
||||
if (!withWorkerHash) {
|
||||
withWorkerHash = new Map();
|
||||
groups.set(test._workerHash, withWorkerHash);
|
||||
}
|
||||
let withRequireFile = withWorkerHash.get(test._requireFile);
|
||||
if (!withRequireFile) {
|
||||
withRequireFile = {
|
||||
general: createGroup(test),
|
||||
parallel: new Map(),
|
||||
parallelWithHooks: createGroup(test),
|
||||
};
|
||||
withWorkerHash.set(test._requireFile, withRequireFile);
|
||||
}
|
||||
|
||||
// Note that a parallel suite cannot be inside a serial suite. This is enforced in TestType.
|
||||
let insideParallel = false;
|
||||
let outerMostSerialSuite: Suite | undefined;
|
||||
let hasAllHooks = false;
|
||||
for (let parent: Suite | undefined = test.parent; parent; parent = parent.parent) {
|
||||
if (parent._parallelMode === 'serial')
|
||||
outerMostSerialSuite = parent;
|
||||
insideParallel = insideParallel || parent._parallelMode === 'parallel';
|
||||
hasAllHooks = hasAllHooks || parent._hooks.some(hook => hook.type === 'beforeAll' || hook.type === 'afterAll');
|
||||
}
|
||||
// Note that a parallel suite cannot be inside a serial suite. This is enforced in TestType.
|
||||
let insideParallel = false;
|
||||
let outerMostSerialSuite: Suite | undefined;
|
||||
let hasAllHooks = false;
|
||||
for (let parent: Suite | undefined = test.parent; parent; parent = parent.parent) {
|
||||
if (parent._parallelMode === 'serial')
|
||||
outerMostSerialSuite = parent;
|
||||
insideParallel = insideParallel || parent._parallelMode === 'parallel';
|
||||
hasAllHooks = hasAllHooks || parent._hooks.some(hook => hook.type === 'beforeAll' || hook.type === 'afterAll');
|
||||
}
|
||||
|
||||
if (insideParallel) {
|
||||
if (hasAllHooks && !outerMostSerialSuite) {
|
||||
withRequireFile.parallelWithHooks.tests.push(test);
|
||||
} else {
|
||||
const key = outerMostSerialSuite || test;
|
||||
let group = withRequireFile.parallel.get(key);
|
||||
if (!group) {
|
||||
group = createGroup(test);
|
||||
withRequireFile.parallel.set(key, group);
|
||||
}
|
||||
group.tests.push(test);
|
||||
}
|
||||
if (insideParallel) {
|
||||
if (hasAllHooks && !outerMostSerialSuite) {
|
||||
withRequireFile.parallelWithHooks.tests.push(test);
|
||||
} else {
|
||||
withRequireFile.general.tests.push(test);
|
||||
const key = outerMostSerialSuite || test;
|
||||
let group = withRequireFile.parallel.get(key);
|
||||
if (!group) {
|
||||
group = createGroup(test);
|
||||
withRequireFile.parallel.set(key, group);
|
||||
}
|
||||
group.tests.push(test);
|
||||
}
|
||||
} else {
|
||||
withRequireFile.general.tests.push(test);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue