- Notifications
You must be signed in to change notification settings - Fork 93
Closed
Labels
Description
Problem
For standalone components, we define compiler scope like
@Component({ selector: 'app-foo', template: `...`, standalone: true, imports: [SomeComponent, SomeModule] }) export class FooComponent {}When using angular testing library, define component compilation scope using the declarations and imports property on the render function.
describe('test', async () => { await render(FooComponent, { declarations: [MockSomeComponent], imports: [...]}) })However, this does not overwrite the imports property of standalone components.
Workaround
It is possible to manually override the imports property on a standalone component using TestBed.
beforeEach(() => { TestBed.configureTestingModule({ imports: [FooComponent] }) TestBed.overrideComponent(FooComponent, {set: {imports: mockImports}}) }) describe('another test', async () => { await render(FooComponent, {excludeComponentDeclaration: true}) })Proposed Solution
Perhaps we can use TestBed.overrideComponent to swap the imports already on a standalone component for the imports array passed into render. Perhaps this can be guarded be a configuration flag like overrideComponentImports.
I might whip up a PR with this idea but just wanted to briefly discuss if this makes sense before putting in the work. Thanks!