angular: ComponentFixture.detectChanges causes NG0100 in zoneless mode
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
No
Description
In zoneless mode, calling ComponentFixture.detectChanges() throws an NG0100 error.
Let’s take the following component as an example:
@Component({
selector: 'app-root',
template: '{{ value }}',
})
export class AppComponent {
value = 0;
}
I am well aware that in real life, since we use zoneless change detection, changing the value of the component should not trigger a change detection, because no signal is being modified.
Let’s run the following (bad) test for it:
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [AppComponent],
providers: [provideExperimentalZonelessChangeDetection()],
}).compileComponents();
});
it('should detect changes', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toBe('0');
fixture.componentInstance.value = 1;
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toBe('1');
});
});
When executed, I expect this test to pass, because we imperatively launch a change detection instead of letting Angular autodetect the changes.
But the test neither passes nor properly fails. Instead, it throws an NG0100 error.
The better test, letting Angular auto-detect the changes, fails correctly:
import { ComponentFixtureAutoDetect, TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { provideExperimentalZonelessChangeDetection } from '@angular/core';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [AppComponent],
providers: [
provideExperimentalZonelessChangeDetection(),
{ provide: ComponentFixtureAutoDetect, useValue: true },
],
}).compileComponents();
});
it('should detect changes', async () => {
const fixture = TestBed.createComponent(AppComponent);
await fixture.whenStable();
expect(fixture.nativeElement.textContent).toBe('0');
fixture.componentInstance.value = 1;
await fixture.whenStable();
expect(fixture.nativeElement.textContent).toBe('1');
});
});
Please provide a link to a minimal reproduction of the bug
Please provide the exception or error you saw
Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '0'. Current value: '1'. Expression location: AppComponent component. Find more at https://angular.dev/errors/NG0100
Please provide the environment you discovered this bug in (run ng version)
Angular CLI: 19.0.4
Node: 18.20.3
Package Manager: npm 10.2.3
OS: linux x64
Angular: 19.0.3
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1900.4
@angular-devkit/build-angular 19.0.4
@angular-devkit/core 19.0.4
@angular-devkit/schematics 19.0.4
@angular/cli 19.0.4
@schematics/angular 19.0.4
rxjs 7.8.1
typescript 5.6.3
zone.js 0.15.0
Anything else?
To run the tests in the stackblitz:
cd demonpm ing test
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 18 (17 by maintainers)
Commits related to this issue
- docs(core): Update zoneless test documentation to clarify OnPush requirement This updates the zoneless documentation to mention that components under test a required to be onpush compatible. This inc... — committed to atscott/angular by atscott 2 years ago
- docs(core): Update zoneless test documentation to clarify OnPush requirement (#59689) This updates the zoneless documentation to mention that components under test a required to be onpush compatible.... — committed to angular/angular by atscott 2 years ago
- docs(core): Update zoneless test documentation to clarify OnPush requirement (#59689) This updates the zoneless documentation to mention that components under test a required to be onpush compatible.... — committed to dgp1130/angular by atscott 2 years ago
I’d say that generally speaking,
ComponentFixture#componentInstanceshouldn’t be used.IMHO, more conceptually speaking, the most atomic thing is the component (class + template + styles + providers + view providers). Shallow testing should still be possible but not generalized, and it should not be performed the way it is today. We need a better shallow testing experience.
Then why keep
ComponentFixture#componentInstance? Well, I think that there are still edge cases where it is useful:So it would throw if you make the situation the same as you have in the test. Rather than a button click, you need to call ApplicationRef.tick after setting the value. Clicking the button marks the component for check and schedules a synchronization. Angular doesn’t know that you changed the value at another time.
Similarly, if your test were triggering a click on an button after updating the value, you would not see an error thrown.
You can also get Angular to throw in your application in this situation. provideExperimentalCheckNoChangesForDebug will check for changes that were not accompanied by a notification in an interval. Angular otherwise does not run the check unless something has notified it of a change. This is very useful for identifying parts of the application that aren’t OnPush/Zoneless compatible. We used this on Angular.dev and the Google Fonts app when converting them to Zoneless.
Nice catch @jnizet !
Actually, what
ComponentFixtureAutoDetectdoes is that it links the components to anApplicationRefto make the tests as symmetric to production as possible.Zoneless tests have this behavior enabled by default so in your second example, it should fail as expected using
ComponentFixture#whenStable()and without settingComponentFixtureAutoDetect.