Commit graph

338 commits

Author SHA1 Message Date
Matthieu Riegler
4ad3a1fe37 refactor(core): Don't throw when there are not async metadata
In the context of AOT tests, component with defer blocks no longer throw on instanciation if the component is not overridden (with `overrideComponent`)

Prior to this change, all components with a `@defer` block would throw if `compileComponents` was not invoked.

In none-JIT apps, this change makes `compileComponents()` uneccesary.
2026-04-27 17:04:09 -07:00
Matthieu Riegler
f9b74e90e3 refactor(core): simplifying the ComponentFactory usage
The `ComponentFactory` has been removed from the public API in #68055.
This commit continues the cleanup and also removes `ModuleWithComponentFactories` from the public API.
2026-04-13 22:24:18 +03:00
Matthieu Riegler
d2054c7c1a refactor: prepare for required changeDetection prop on G3.
We'll make this a G3 only change to prevent backsliding during the transition period.
2026-03-20 15:52:38 -07:00
Matthieu Riegler
69fb1614ef refactor(core): remove checkNoChanges from the public API.
It's an internal API.

BREAKING CHANGE: `ChangeDetectorRef.checkNoChanges` was removed. In tests use `fixture.detectChanges()` instead.
2026-03-17 13:57:43 -06:00
Andrew Scott
dc3131c639 feat(core): TestBed.getFixture -> TestBed.getLastFixture and update implementation
TestBed.getFixture is now TestBed.getLastFixture and returns the last fixture
created rather than throwing if there are more than 1.
2026-03-12 14:57:16 -06:00
Andrew Scott
246a984a5d feat(core): add TestBed.getFixture
This adds a new method to the TestBed to retrieve the most recently created component fixture. This is useful for cases where the fixture is created in a beforeEach and needs to be accessed in a test.

If multiple fixtures have been created, this method will throw an error to prevent depending on fixture creation order.
2026-02-20 09:17:54 -08:00
Matthieu Riegler
71cde39ff0 docs: rewrite testing docs
Those rewrites focus on using Vitest instead of jasmine, drop usages of `fakeAsync`, present modern testing strategy that rely on `whenStable` more than explicit calls to `detectChanges`.

fixes #42748, #48510, #64962, #65987, #66029,  #66150
2026-01-05 19:38:23 -05:00
Andrew Scott
06be8034bb fix(core): Microtask scheduling should be used after any application synchronization
Previously, Angular would switch from the macrotask to a microtask
scheduler _only_ when the scheduler was the trigger for the
synchronization. This microtask scheduling is to ensure patterns such as
`Promise.resolve().then(() => updateAppStateAgain())` _during_
synchronization are caught and synchronized again within the same event
loop (guaranteeing that they aren't split across multiple browser paints).

The microtask scheduler should be used after any tick, not just from
those than run within the scheduler to always account for the promises
within synchronization. This is encountered most frequently during
bootstrap, which triggers the tick directly.

In this change we exempt `TestBed.tick` and
`ComponentFixture.detectChanges` from this behavior. Doing so would affect
the timing of stability and tests are quite sensitive to this (e.g.
`fixture.whenStable`). It is somewhat unfortunate that we have "special" test-only
behavior. However, it is important to acknowledge that this only affects
the test-only APIs as well. Any code in the application under test that
triggers `ApplicationRef.tick` directly would still use the microtask
scheduling behavior.

fixes #65444
2025-12-16 13:34:48 -08:00
Kristiyan Kostadinov
886cf6c452 fix(core): unable to inject viewProviders when host directive with providers is present
When registering providers, the DI system assumes that `viewProviders` are registered before plain `providers`. This was reinforced by components always being first in the array of directive matches, only one component being allowed per node and the fact that only components can have `viewProviders`.

This breaks down if there are host directives with `providers` on the component, because they'll execute earlier, throwing off the order of operations.

These changes fix the issue by separating out the resolvers for `viewProviders` and plain `providers` and explicitly running the component's `viewProviders` resolver before any others. This also has the benefit of not attempting to resolve `viewProviders` for directives which are guaranteed not to have them.

Fixes #65724.
2025-12-03 15:09:48 +01:00
Matthieu Riegler
b34e48bdff docs: add callout on zone base testing helpers
With zoneless being the default, we need to make these requirements more explicit.

fixes #65539
2025-11-24 13:27:46 -05:00
Andrew Scott
a253739ac8
refactor(core): Remove zone toggles for test and standalone
The migration is complete internally. All that remains is bootstrapModule
2025-10-28 15:57:58 +01:00
Andrew Scott
d5e00f9591 refactor(core): Make the zoneless-by-default change a flag flip (#63382)
This makes the zoneless-by-default change a flag flip that can be
individually enabled in tests, createApplication, and bootstrapModule
for gradual rollout. In addition, the "require on CD provider" check is
also made individually flippable for gradual rollout.

PR Close #63382
2025-09-16 18:56:54 +00:00
Andrew Scott
45fed3d201 fix(core): Remove Zone-based change provider from internals by default (#63382)
This change removes the internally provided `ZoneJS`-based change
detection scheduler. This makes Angular Zoneless by default and allows
tree-shaking of the Zone change detection providers.

BREAKING CHANGE: Angular no longer provides a change detection scheduler
for ZoneJS-based change detection by default. Add
`provideZoneChangeDetection` to the providers of your
`bootstrapApplication` function or your `AppModule` (if using
`bootstrapModule`). This provider addition will be covered by an
automated migration.

PR Close #63382
2025-09-16 18:56:53 +00:00
Andrew Scott
d399d7d02b fix(core): Explicit Zone CD in TestBed providers should not override TestBed error handler (#63404)
The internal error handler in TestBed rethrows errors to prevent them
from being silently ignored in tests. Prior to this commit, tests which
used `provideZoneChangeDetection` in the providers would override the
internal error handler of TestBed and prevent these errors from being
rethrown.

BREAKING CHANGE: (test only) - Using `provideZoneChangeDetection` in the
TestBed providers would previously prevent `TestBed` from rethrowing
errors as it should. Errors in the test will now be rethrown, regardless
of the usage of `provideZoneChangeDetection`. Tests should be adjusted to
prevent or account for these errors. As in previous major versions,
this behavior can be disabled with `rethrowApplicationErrors: false` in
`configureTestingModule` as a last resort.

PR Close #63404
2025-09-02 09:26:44 -07:00
cexbrayat
bf9db2897e docs: animationsEnabled defaults to false (#63262)
PR Close #63262
2025-08-20 08:25:26 +00:00
Jessica Janiuk
898244a03a refactor(core): add configurable behavior for animations to testbed (#62764)
This adds a test module configuration to define whether animations should be enabled or disabled in test. By default, they are disabled.

PR Close #62764
2025-07-29 09:49:35 +00:00
Kristiyan Kostadinov
cec91c0035 feat(core): add option to infer the tag names of components in tests (#62283)
Currently when testing a component using `TestBed.createComponent`, we always create the component as a `div` which isn't aligned with the runtime. The runtime tries to parse out the tag name from the first selector in `@Component` and only falls back to `div` if there isn't one. This behavior difference can cause components to not behave like they would in production which reduces the usefulness of the tests.

These changes add the `inferTagName` option to `TestBed.createComponent` and `TestBed.configureTestingModule` that allows apps to opt into inferring the tag name from the selector in the same way as the runtime. Currently the new option is set to `false`, but we intend to change it to `true` in a future version.

PR Close #62283
2025-07-23 07:30:36 -04:00
Kristiyan Kostadinov
2e0c98bd3f feat(core): support bindings in TestBed (#62040)
Adds support for passing in `Binding` objects into `TestBed.createComponent`. This makes it easier to test components by avoiding the need to create a wrapper component. Furthermore, it keeps the behavior consistent between tests and the actual app. For example, given a custom checkbox that looks like this:

```typescript
@Component({
  selector: 'my-checkbox',
  template: '...',
  host: {'[class.checked]': 'isChecked()'}
})
export class MyCheckbox {
  isChecked = input(false);
}
```

A test for the `isChecked` input would look something like this:

```typescript
it('should toggle the checked class', () => {
  @Component({
    imports: [MyCheckbox],
    template: '<my-checkbox [isChecked]="isChecked"/>',
  })
  class Wrapper {
    isChecked = false;
  }

  const fixture = TestBed.createComponent(Wrapper);
  const checkbox = fixture.nativeElement.querySelector('my-checkbox');
  fixture.detectChanges();
  expect(checkbox.classList).not.toContain('checked');

  fixture.componentInstance.isChecked = true;
  fixture.detectChanges();
  expect(checkbox.classList).toContain('checked');
});
```

Whereas with the new API, the test would look like this:

```typescript
it('should toggle the checked class', () => {
  const isChecked = signal(false);
  const fixture = TestBed.createComponent(MyCheckbox, {
    bindings: [inputBinding('isChecked', isChecked)]
  });
  const checkbox = fixture.nativeElement.querySelector('my-checkbox');
  fixture.detectChanges();
  expect(checkbox.classList).not.toContain('checked');

  isChecked.set(true);
  fixture.detectChanges();
  expect(checkbox.classList).toContain('checked');
});
```

PR Close #62040
2025-06-17 11:49:27 +02:00
Andrew Scott
4356e85456 fix(core): fakeAsync should not depend on module import order (#61375)
`fakeAsync` does not work if the zone-testing polyfill is included after
@angular/core/testing is loaded. This allows fakeAsync to work
even if the zone-testing is included later.

PR Close #61375
2025-05-23 09:45:41 -07:00
Andrew Scott
971981e1df fix(core): TestBed.tick should ensure test components are synchronized (#61382)
This ensures that `TestBed.tick` updates any components created with
`TestBed.createComponent`, regardless of whether autoDetectChanges is
on.

PR Close #61382
2025-05-21 15:02:52 +00:00
Andrew Scott
f182886ce5 refactor(core): Disallow autoDetectChanges(false) in zoneless (#61430)
This removes the ability to use `autoDetectChanges(false)` when
`provideZonelessChangeDetection` is used.

PR Close #61430
2025-05-20 08:49:06 +00:00
Pawel Kozlowski
2b3c89dba2 refactor(core): re-introduce TestBed.flushEffects()" (#61462)
Our intention was to remove TestBed.flushEffects() in Angular v20
as it was a developer preview API that doesn't need to follow our
deprecation policy. It turned out, though, that the angular.dev
documentation wasn't displaying the @developerPreview annotation
properly so some of the developers might have been unaware of the
non-stable nature of this API.

This commits re-introduces the TestBed.flushEffects() API. This
API call will delegate to TestBed.tick().

PR Close #61462
2025-05-19 13:26:52 +00:00
Andrew Scott
bdbf616a4e refactor(core): Deprecate fixture.autoDetectChanges(true/false) (#61429)
After nearly a decade of existence, there are 0 uses of
`autoDetectChanges(false)` internally. All uses of
`autoDetectChanges(true)` can be migrated directly to
`autoDetectChanges()` instead.

PR Close #61429
2025-05-19 08:31:37 +00:00
Paul Gschwendtner
d067dff394 build: migrate more targets of @angular/core to ts_project (#61370)
Migrates more targets of `@angular/core` to `ts_project`. Remaining are:

 - tests
 - schematics

PR Close #61370
2025-05-16 11:02:07 +00:00
Andrew Scott
8e2ca25e26 fix(core): Testing should not throw when Zone does not patch test FW APIs (#61376)
This prevents `core/testing` from throwing an error if ZoneJS is present
but does not patch the test FW APIs such that `fakeAsync` works
automatically. For example, there is currently no patching of the vitest
APIs, so if you try to use Vitest with Zone on the page, it will throw.

PR Close #61376
2025-05-16 07:53:40 +00:00
Matthieu Riegler
dd25f3d20f refactor(core): remove USE_RUNTIME_DEPS_TRACKER_FOR_JIT flag. (#61265)
The code has been migrated in G3, this flag is no longer necessary.

PR Close #61265
2025-05-13 15:22:18 -07:00
Miles Malerba
c0e9fc103f docs: rename @nodoc to @docs-private (#61194)
This aligns with how angular/components marks their hidden APIs.
`@nodoc` has been broken since the switch to adev, this change should
properly hide the APIs again.

PR Close #61194
2025-05-09 10:23:00 -07:00
Andrew Scott
e7f5aa2b52 refactor(core): Remove use of private export PendingTasksInternal where possible (#61049)
This commit removes the use of the privately exported
PendingTasksInternal everywhere except for Router. A follow-up change
will be done to remove that one as well and delete the private export.

PR Close #61049
2025-05-05 08:56:20 -07:00
Matthieu Riegler
f580318411 docs(docs-infra): Add version of introduction for APIs (#60814)
For new APIs we'll mention since when a particular API is in its current status (experimental, devPreview, stable)

fixes #49668

PR Close #60814
2025-05-02 07:51:33 -07:00
Alan Agius
a60373ee1a refactor(core): remove private __core_private_testing_placeholder__ API (#61055)
This API is no longer used in G3

PR Close #61055
2025-04-30 12:37:39 -07:00
Alan Agius
c037c58995 refactor(core): expose getCleanupHook via private exports (#61017)
Expose `getCleanupHook` as a private export to address integration issues with Vitest. In Vitest, `globalThis.beforeEach` and `globalThis.afterEach` are not available by default. Additionally, these hooks are patched during module evaluation, complicating seamless integration with Vitest.

See: https://github.com/angular/angular-cli/pull/30188

PR Close #61017
2025-04-29 10:30:17 -07:00
Pawel Kozlowski
d5fd7349fb feat(core): introduce TestBed.tick() (#60993)
This commit introduces the `TestBed.tick()` method that,
similarly to the `ApplicationRef.tick()`, synchronizes state
with the DOM. It can be used in unit tests to mimic framework's
logic executed in production applications. The `TestBed.tick()`
should be used instead of the removed `TestBed.flushEffects()`.

BREAKING CHANGE: the `TestBed.flushEffects()` was removed - use
the `TestBed.tick()` instead.

PR Close #60993
2025-04-25 12:53:23 +02:00
Pawel Kozlowski
0859a99e89 refactor(core): replace TestBed.flushEffects with tick (#60959)
Instead of stabilizing the TestBed.flushEffects() API we intend to
replace it with the tick() method (equivalent of ApplicationRef.tick().
The reasoning here is that we prefer tests running the entire
synchronization process (as in production apps) instead of invoking
parts of the synchronization process in a way that would naver happen
in a running application.

PR Close #60959
2025-04-23 09:54:30 +02:00
Matthieu Riegler
1c1ad12ad3 refactor(core): Instantiate the ErrorHandler lazily. (#58984)
This change prevents that user code in a custom `ErrorHandler` to run as part of `ApplicationRef`'s creation.

fixes #58970

PR Close #58984
2025-04-02 19:20:50 +00:00
arturovt
1c7b356625 fix(core): release hasPendingTasks observers (#59723)
In this commit, we unsubscribe the `hasPendingTasks` subject to remove all active observers and enable granular garbage collection, as users may forget to unsubscribe manually when subscribing to `isStable`.

PR Close #59723
2025-04-02 18:26:06 +00:00
Andrew Kushnir
e5cc624b87 refactor(core): convert scripts within packages/core/testing to relative imports (#60232)
This commit updates scripts within `packages/core/testing` to relative imports as a prep work to the upcoming infra updates.

PR Close #60232
2025-03-27 18:29:35 +00:00
Alan Agius
5753aa70eb refactor(platform-browser-dynamic): relocate DOMTestComponentRenderer to @angular/platform-browser (#60453)
This commit moves `DOMTestComponentRenderer` to `@angular/platform-browser/testing`, allowing the Angular CLI to eliminate its dependency on `@angular/platform-browser-dynamic`, which would no longer be required for new projects.

PR Close #60453
2025-03-19 19:08:08 +01:00
Kristiyan Kostadinov
5e209cb560 feat(core): remove TestBed.get (#60414)
`TestBed.get` isn't type safe and has been deprecated for several years now. These changes remove it from the public API and a follow-up change will add an automated migration to `TestBed.inject`.

BREAKING CHANGE:
* `TestBed.get` has been removed. Use `TestBed.inject` instead.

PR Close #60414
2025-03-19 15:52:26 +01:00
Kristiyan Kostadinov
611baaf069 feat(core): remove InjectFlags from public API (#60318)
Removes the deprecated `InjectFlags` symbol from the `@angular/core` public API, as well as all the places that accept it. The previous commit includes an automated migration to switch over to the new way of passing in flags.

BREAKING CHANGE:
* `InjectFlags` has been removed.
* `inject` no longer accepts `InjectFlags`.
* `Injector.get` no longer accepts `InjectFlags`.
* `EnvironmentInjector.get` no longer accepts `InjectFlags`.
* `TestBed.get` no longer accepts `InjectFlags`.
* `TestBed.inject` no longer accepts `InjectFlags`.

PR Close #60318
2025-03-11 11:33:09 -07:00
Andrew Scott
02cb7e061a refactor(core): remove microtask effect (#60234)
This has been removed internally and was never exposed externally (it was a breaking change)

PR Close #60234
2025-03-06 12:48:55 -08:00
Andrew Scott
962b59b14e fix(core): Ensure ComponentFixture does not duplicate error reporting from FakeAsync (#60104)
When using `fakeAsync`, if errors happen inside the Angular zone, they
are also handled by the fakeAsync machinery. Rethrowing errors from the
`NgZone.onError` subscription is not appropriate in this case because it
results in the error being thrown twice.

db2f2d99c8/packages/zone.js/lib/zone-spec/fake-async-test.ts (L783-L784)
db2f2d99c8/packages/zone.js/lib/zone-spec/fake-async-test.ts (L473-L478)

PR Close #60104
2025-02-26 11:56:18 -05:00
Andrew Scott
8657a0e4cc refactor(core): Add fake navigation to primitives for code sharing (#59857)
This moves the `FakeNavigation` implementation to the primitives folder
so its implementation can be shared with Wiz. This class was initially
copied directly from the Wiz implementation, with some small modifications.
There will still need to be some work done to align the implementations
and fix anything internally that needs adjusting.

PR Close #59857
2025-02-19 20:09:10 +00:00
Alan Agius
24e317cb15 refactor: replace ɵPendingTasks with ɵPendingTasksInternal (#59138)
This commits remove usage of the old export.

PR Close #59138
2024-12-10 13:45:07 -08:00
hawkgs
0513fbc9fc docs: set syntax highlighting of code examples MD code blocks (#59026)
Set the syntax highlighting based on the code examples' language.

PR Close #59026
2024-12-04 17:30:28 +01:00
Kristiyan Kostadinov
b35ecee85e refactor(core): clean up standalone flag (#58478)
The `NG_STANDALONE_DEFAULT_VALUE` is no longer being patched internally and can be removed.

PR Close #58478
2024-11-06 07:01:25 +01:00
Matthieu Riegler
517da9532c refactor(core): Setup constant for standalone default value (#58175)
This commit is part of the migration to standalone by default and sets up 2 files with a default value for standalone. They are still `false` in this case to land the change into G3 first. The switch to `true` will be executed in a follow-up PR.

PR Close #58175
2024-10-14 16:01:41 +00:00
cexbrayat
e00775a0c9 docs: mention autoDetectChanges parameter default value (#58092)
It was unclear whether the parameter was necessary, as its default value was not mentioned in the jdsoc.

PR Close #58092
2024-10-09 13:50:31 +00:00
Joey Perrott
9dbe6fc18b refactor: update license text to point to angular.dev (#57901)
Update license text to point to angular.dev instead of angular.io

PR Close #57901
2024-09-24 15:33:00 +02:00
Alex Rickabaugh
4e890cc5ac refactor(core): add support for new effect scheduling. (#56501)
The original effect design for Angular had one "bucket" of effects, which
are scheduled on the microtask queue. This approach got us pretty far, but
as developers have built more complex reactive systems, we've hit the
limitations of this design.

This commit changes the nature of effects significantly. In particular,
effects created in components have a completely new scheduling system, which
executes them as a part of the change detection cycle. This results in
behavior similar to that of nested effects in other reactive frameworks. The
scheduling behavior here uses the "mark for traversal" flag
(`HasChildViewsToRefresh`). This has really nice behavior:

 * if the component is dirty already, effects run following preorder hooks
   (ngOnInit, etc).
 * if the component isn't dirty, it doesn't get change detected only because
   of the dirty effect.

This is not a breaking change, since `effect()` is in developer preview (and
it remains so).

As a part of this redesigned `effect()` behavior, the `allowSignalWrites`
flag was removed. Effects no longer prohibit writing to signals at all. This
decision was taken in response to feedback / observations of usage patterns,
which showed the benefit of the restriction did not justify the DX cost.

The new effect timing is not yet enabled - a future PR will flip the flag.

PR Close #56501
2024-09-18 14:52:25 -07:00
Andrew Scott
0300dd2e18 fix(core): Fix fixture.detectChanges with autoDetect disabled and zoneless (#57416)
When disabling autodetect (not recommeneded) with zoneless,
`fixture.detectChanges` would previously not refresh the fixture's component.

PR Close #57416
2024-08-27 13:12:44 -07:00