Skip to content

No automatic override for imports on standalone components #306

Closed
@jadengis

Description

@jadengis

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions