ngneat/spectator

{ shallow: true } doesn't mock standalone components/directives/pipes

dankerk opened this issue · 3 comments

Description

If you add the setting { shallow: true } to the createComponentFactory function, it normally mocks all components, directives, pipes that are in the template, which is awesome.

However, it doesn't do this for standalone components, directives, pipes and I think that it should.

Is this something that can be addressed?

Proposed solution

For this setting to work for both "old style" and standalone components/directives/pipes

createComponentFactory({
		component: SomeComponent,
		shallow: true,
})

Alternatives considered

import { MockComponents } from 'ng-mocks';

createComponentFactory({
		component: SomeComponent,
		shallow: true,
		declarations: [MockComponens(MyStandaloneComponent)],
})

Do you want to create a pull request?

No

Have the same issue.

Angular version: 15.0.1
Spectator version: 12.2.0

I have the same issue with Angular 16.2, spectator 14.0.0

Until this gets rectified, here is a (hopefully temporary?) solution:

createComponentFactory({
    component: StandaloneComponent,
    overrideComponents: [
        [
            StandaloneComponent,
            {
                remove: { imports: [NestedComponent] },
                add: { imports: [MockComponent(NestedComponent)] }
            }
        ]
    ]
});

This relies on ng-mocks, but that part is easily changed to whatever implementation you wish to use.