SchematicTestRunner: ability to run scheduled tasks

Open
#11,739 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
typescript
Domain
testing, tooling

Research direction

Start with the SchematicTestRunner implementation and the task-scheduling path around context.addTask and runner.engine.executePostTasks(); inspect how RunSchematicTask uses the engine workflow. Done means tests can execute only scheduled tasks with a selected name, or RunSchematicTask works in SchematicTestRunner, with coverage for the supported behavior.

Written by the indexing model from the issue text.

Description

area: @angular-devkit/schematics feature

If someone uses the SchematicTestRunner and runs a schematic that schedules specific tasks using the context.addTask method, those tasks cannot be run in the tests.

I figured that there is a method (runner.engine.executePostTasks() that runs all scheduled tasks. This is not really helpful though, since there are situations where we only want to run specific tasks and not all scheduled tasks.

This is because some tasks like RunSchematicTask depend on a engine.workflow that is not set up in the SchematicTestRunner. It would be either nice if the RunSchematicTask could work properly in the tests, or there is a method that runs tasks with a specific name.

Current workaround:

/**
 * Due to the fact that the Angular devkit does not support running scheduled tasks from a
 * schematic that has been launched through the TestRunner, we need to manually find the task
 * executor for the given task name and run all scheduled instances.
 *
 * Note that this means that there can be multiple tasks with the same name. The observable emits
 * only when all tasks finished executing.
 */
export function runPostScheduledTasks(runner: SchematicTestRunner, taskName: string) {

  const host = runner.engine['_host'] as EngineHost<{}, {}>;
  const tasks = runner.engine['_taskSchedulers'] as TaskScheduler[];

  return observableFrom(tasks).pipe(
    concatMap(scheduler => scheduler.finalize()),
    filter(task => task.configuration.name === taskName),
    concatMap(task => {
      return host.createTaskExecutor(task.configuration.name)
        .pipe(concatMap(executor => executor(task.configuration.options, task.context)));
    }),
    // Only emit the last emitted value because there can be multiple tasks with the same name.
    // The observable should only emit a value if all tasks completed.
    last()
  );
}

cc. @jelbourn @hansl

Dominant language
TypeScript
Stars
27k
Forks
11.8k
Avg merge
16h 35m
Merged PRs (30d)
176

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from angular/angular-cli

All issues in angular/angular-cli

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.