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

未关闭
#17,205 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
angular, typescript
领域
devtools, tooling

调研方向

从链接的最小复现和使用 callRule、mergeWith 与 apply 的单元测试开始。通过 file-system-engine-host-base.js 和 schematics url.js 入口点跟踪报告的失败,并注意被省略的 partialContext。完成后应建立所需的 context,并使测试通过,或明确报告缺失的 context。

由索引模型根据 Issue 内容生成。

描述

area: @angular-devkit/schematics freq1: low severity3: broken type: bug/fix

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:

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

Failures:
1) applyTemplateFiles Rule should apply the files to the tree
  Message:
    TypeError: Cannot read property 'path' of undefined
  Stack:
        at <Jasmine>
        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"
主要语言
TypeScript
星标
27k
派生
11.8k
平均合并
16 小时 35 分钟
30 天内合并 PR
176

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

angular/angular-cli 的其他 Issue

查看 angular/angular-cli 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。