angular / angular/angular-cli

Schematics: Unit testing a rule that uses mergeWith/apply fails due to missing context

オープン
#17,205 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る
area: @angular-devkit/schematics freq1: low severity3: broken type: bug/fix
主要言語
TypeScript
スター
27k
フォーク
11.8k
平均マージ
14時間 23分
マージ済み PR(30日)
162

説明

## Addendum

In creating the test case I've realised that I'm not passing a `partialContext` to `callRule`, so it is definitely missing context, however, it is not at all clear _what_ context is necessary for `mergeWith` to succeed and the error does not indicate that context is actually missing.

Happy for this to be marked as a question instead. Guidance on _how_ to provide the context and _what_ context to provide would be amazingly useful.

# 🐞 Bug report

### Description

Using `callRule` to unit test rules within a schematic fails when it comes to rules which return `mergeWith`

## 🔬 Minimal Reproduction

See: https://github.com/jdpearce/schematics-apply-problem

Given a rule which applies template files to the tree, this unit test will fail with `TypeError: Cannot read property 'path' of undefined`:

```ts
describe('applyTemplateFiles Rule', () => {
let tree: UnitTestTree;

beforeEach(async () => {
tree = new UnitTestTree(Tree.empty());
});

it('should apply the files to the tree', async () => {
const schema = {};

tree = (await callRule(applyTemplateFiles(schema), tree)) as UnitTestTree;

expect(tree.files).toEqual(jasmine.arrayContaining(['/libs/destination/test.txt']));
});
});
```

## 🔥 Exception or Error

```bash
Failures:
1) applyTemplateFiles Rule should apply the files to the tree
Message:
TypeError: Cannot read property 'path' of undefined
Stack:
at
at ./apply-problem/node_modules/@angular-devkit/schematics/tools/file-system-engine-host-base.js:216:96
at ./tmp/apply-problem/node_modules/@angular-devkit/schematics/src/rules/url.js:13:73
at Object.callSource (./tmp/apply-problem/node_modules/@angular-devkit/schematics/src/rules/call.js:55:20)
at ./tmp/apply-problem/node_modules/@angular-devkit/schematics/src/rules/base.js:45:60
at Object.callSource (./tmp/apply-problem/node_modules/@angular-devkit/schematics/src/rules/call.js:55:20)
at ./tmp/apply-problem/node_modules/@angular-devkit/schematics/src/rules/base.js:53:23
at MergeMapSubscriber.project (./tmp/apply-problem/node_modules/@angular-devkit/schematics/src/rules/call.js:74:24)
at MergeMapSubscriber._tryNext (./tmp/apply-problem/node_modules/rxjs/internal/operators/mergeMap.js:69:27)
at MergeMapSubscriber._next (./tmp/apply-problem/node_modules/rxjs/internal/operators/mergeMap.js:59:18)
at MergeMapSubscriber.Subscriber.next (./tmp/apply-problem/node_modules/rxjs/internal/Subscriber.js:66:18)

2 specs, 1 failure
```

## 🌍 Your Environment

"@angular-devkit/core": "^9.0.6",
"@angular-devkit/schematics": "^9.0.6",
"@types/jasmine": "^3.3.9",
"@types/node": "^8.0.31",
"jasmine": "^3.3.1",
"typescript": "~3.5.3"

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

リンクされた最小再現例と、callRule、mergeWith、apply を使用するユニットテストから始めます。報告された失敗を file-system-engine-host-base.js と schematics url.js のエントリポイントを通して追跡し、省略された partialContext に注意してください。実装では必要なコンテキストを確立し、テストを通過させるか、不足しているコンテキストを明確に報告できるようにします。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
angular, typescript
領域
devtools, tooling
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。