SchematicTestRunner: ability to run scheduled tasks
- 主要語言
- TypeScript
- 星號
- 27k
- 分支
- 11.8k
- 平均合併
- 14 小時 23 分鐘
- 30 天內合併 PR
- 162
描述
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:
```ts
/**
* 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
貢獻指南
研究方向
從 SchematicTestRunner 的實作以及圍繞 context.addTask 和 runner.engine.executePostTasks() 的任務排程路徑開始;檢查 RunSchematicTask 如何使用 engine 工作流程。完成的標準是測試可以只執行具有選定名稱的已排程任務,或 RunSchematicTask 在 SchematicTestRunner 中正常運作,並為受支援的行為提供涵蓋。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- typescript
- 領域
- testing, tooling
- Issue 類型
- 功能
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100