Skip to content

feat(core): add utility for testing directives - #70453

Merged
mattrbeck merged 1 commit into
angular:mainfrom
crisbeto:create-directive
Sep 1, 2026
Merged

mattrbeck merged 1 commit into
angular:mainfrom
crisbeto:create-directive

Conversation

@crisbeto

Copy link
Copy Markdown
Member

Adds TestBed.createDirective to make testing directives easier.

Fixes #54164.

@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer target: minor This PR is targeted for the next minor release labels Aug 28, 2026
@angular-robot angular-robot Bot added detected: feature PR contains a feature commit area: core Issues related to the framework runtime labels Aug 28, 2026
@ngbot ngbot Bot added this to the Backlog milestone Aug 28, 2026
@crisbeto
crisbeto marked this pull request as ready for review August 28, 2026 12:40
@pullapprove
pullapprove Bot requested review from JeanMeche and atscott August 28, 2026 12:40
Comment thread packages/core/testing/src/test_bed.ts Outdated
@pullapprove
pullapprove Bot requested a review from JeanMeche August 28, 2026 12:43
Adds `TestBed.createDirective` to make testing directives easier.

Fixes angular#54164.
Comment thread packages/core/testing/src/fixture.ts
@pullapprove
pullapprove Bot requested a review from JeanMeche August 28, 2026 12:55
@pullapprove
pullapprove Bot requested a review from JeanMeche August 28, 2026 12:56

@JeanMeche JeanMeche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed-for: public-api

Comment on lines +720 to +721
const noNgZone = this.inject(ComponentFixtureNoNgZone, false);
const ngZone = noNgZone ? null : this.inject(NgZone, null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the way to do this so it matches fixture and app behavior is

const ngZone = this.inject(ComponentFixtureNoNgZone, false) ? new NoopNgZone() : inject(NgZone);

And then you can just do ngZone.run below without the separate init (done twice below)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was matching this to what we have for components.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, can we change it there too then? 馃槄 inject(NgZone) should really never fail

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory it could break if somebody was doing something like {provide: NgZone, useValue: null}, I can't imagine why but also wouldn't be surprised. I can do it in a follow-up so I don't need to re-TGP this change.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory it could break if somebody was doing something like {provide: NgZone, useValue: null}, I can't imagine why but also wouldn't be surprised. I can do it in a follow-up so I don't need to re-TGP this change.

I don't think it's possible. In the constructor of ComponentFixture, it does this._ngZone.runOutsideAngular

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I'm okay with you doing this as a followup

@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Sep 1, 2026
@mattrbeck
mattrbeck merged commit 05c4d5a into angular:main Sep 1, 2026
25 checks passed
@mattrbeck

Copy link
Copy Markdown
Member

This PR was merged into the repository. The changes were merged into the following branches:

crisbeto added a commit to crisbeto/material2 that referenced this pull request Sep 4, 2026
Since angular/angular#70453, the framework has a `DirectiveFixture` in addition to the `ComponentFixture`. These changes expand the APIs in the `TestbedHarnessEnvironment` to account for directive fixtures.
crisbeto added a commit to crisbeto/material2 that referenced this pull request Sep 4, 2026
Since angular/angular#70453, the framework has a `DirectiveFixture` in addition to the `ComponentFixture`. These changes expand the APIs in the `TestbedHarnessEnvironment` to account for directive fixtures.
crisbeto added a commit to crisbeto/material2 that referenced this pull request Sep 4, 2026
Since angular/angular#70453, the framework has a `DirectiveFixture` in addition to the `ComponentFixture`. These changes expand the APIs in the `TestbedHarnessEnvironment` to account for directive fixtures.
crisbeto added a commit to angular/components that referenced this pull request Sep 5, 2026
Since angular/angular#70453, the framework has a `DirectiveFixture` in addition to the `ComponentFixture`. These changes expand the APIs in the `TestbedHarnessEnvironment` to account for directive fixtures.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime detected: feature PR contains a feature commit target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide the ability to test directives that contain input signals without wrapper components

4 participants