angular/packages/core/testing/src
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
..
application_error_handler.ts refactor(core): Instantiate the ErrorHandler lazily. (#58984) 2025-04-02 19:20:50 +00:00
async.ts docs: set syntax highlighting of code examples MD code blocks (#59026) 2024-12-04 17:30:28 +01:00
component_fixture.ts fix(core): TestBed.tick should ensure test components are synchronized (#61382) 2025-05-21 15:02:52 +00:00
defer.ts docs: rename @nodoc to @docs-private (#61194) 2025-05-09 10:23:00 -07:00
fake_async.ts fix(core): fakeAsync should not depend on module import order (#61375) 2025-05-23 09:45:41 -07:00
logger.ts refactor(core): convert scripts within packages/core/testing to relative imports (#60232) 2025-03-27 18:29:35 +00:00
metadata_override.ts refactor: update license text to point to angular.dev (#57901) 2024-09-24 15:33:00 +02:00
metadata_overrider.ts refactor(core): convert scripts within packages/core/testing to relative imports (#60232) 2025-03-27 18:29:35 +00:00
resolvers.ts refactor(core): convert scripts within packages/core/testing to relative imports (#60232) 2025-03-27 18:29:35 +00:00
styling.ts refactor: update license text to point to angular.dev (#57901) 2024-09-24 15:33:00 +02:00
test_bed.ts feat(core): support bindings in TestBed (#62040) 2025-06-17 11:49:27 +02:00
test_bed_common.ts refactor(core): convert scripts within packages/core/testing to relative imports (#60232) 2025-03-27 18:29:35 +00:00
test_bed_compiler.ts refactor(core): remove USE_RUNTIME_DEPS_TRACKER_FOR_JIT flag. (#61265) 2025-05-13 15:22:18 -07:00
test_hooks.ts refactor(core): remove private __core_private_testing_placeholder__ API (#61055) 2025-04-30 12:37:39 -07:00
testing.ts feat(core): support bindings in TestBed (#62040) 2025-06-17 11:49:27 +02:00
testing_internal.ts refactor: update license text to point to angular.dev (#57901) 2024-09-24 15:33:00 +02:00
testing_private_export.ts refactor(core): expose getCleanupHook via private exports (#61017) 2025-04-29 10:30:17 -07:00