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

Open
#17,205 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
angular, typescript
Domain
devtools, tooling

Research direction

Start with the linked minimal reproduction and the unit test using callRule, mergeWith, and apply. Trace the reported failure through file-system-engine-host-base.js and the schematics url.js entry point, paying attention to the omitted partialContext. Done should establish the required context and either make the test pass or clearly report the missing context.

Written by the indexing model from the issue text.

Description

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"
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.