-
Sorry to open this as a "bug" but I wanted to move us out of the stack overflow post and into here while trying to make it work. I just saw your reply there pointing at the spectator example, but I still can't make what you're showing work. Here's a complete example I finally put together to help debug this. If I run that test, it still throws the error from the import {Component, OnInit} from '@angular/core'
import {createComponentFactory, Spectator} from '@ngneat/spectator/jest'
import {MockBuilder} from 'ng-mocks'
@Component({
selector: 'app-header',
standalone: true,
template: ''
})
class HeaderComponent implements OnInit {
ngOnInit(): void {
throw new Error('Why did I get called?')
}
}
@Component({
selector: 'app-foo',
standalone: true,
imports: [HeaderComponent],
template: '<app-header></app-header>'
})
class ExampleComponent {
}
describe('ExampleComponent', () => {
let spectator: Spectator<ExampleComponent>
let component: ExampleComponent
const dependencies = MockBuilder(HeaderComponent).build()
const createComponent = createComponentFactory({
component: ExampleComponent,
...dependencies
})
beforeEach(() => {
spectator = createComponent()
component = spectator.component
})
it('should create', () => {
expect(component).toBeTruthy()
})
}) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
Oh wow, that's counter-intuitive at first glance, but I guess makes sense. What all is it generating at that point? Do I essentially now no longer need to specify any imports on spectator's |
Beta Was this translation helpful? Give feedback.
-
it works the next way so instead of having @Component({
selector: 'app-foo',
standalone: true,
imports: [HeaderComponent],
template: '<app-header></app-header>'
})
``
during testing, it will be
```ts
@Component({
selector: 'app-foo',
standalone: true,
imports: [MockComponent(HeaderComponent)], // <- MockComponent
template: '<app-header></app-header>'
}) Because you are testing describe('ExampleComponent', () => {
let spectator: Spectator<ExampleComponent>
let component: ExampleComponent
// HERE use ExampleComponent, not HeaderComponent
const dependencies = MockBuilder(ExampleComponent).build();
const createComponent = createComponentFactory({
component: ExampleComponent,
...dependencies
})
beforeEach(() => {
spectator = createComponent()
component = spectator.component
})
it('should create', () => {
expect(component).toBeTruthy()
})
}) Regarding imports in spectator, right, you don't need them anymore. You should call const dependencies = MockBuilder(MyComponent, [ItsModule, AnotherService, DependencyStandaloneForMock])
.mock(AnotherService, {auth$: of(true)}, // <- customizing a mock service
.build();
const createComponent = createComponentFactory({
component: MyComponent,
...dependencies,
}); For const dependencies = MockBuilder(
// Things to keep and export.
[
ComponentForTesting,
RouterModule,
RouterTestingModule.withRoutes([]),
],
// Things to mock.
TheModuleOfTheComponentForTesting,
)
const createComponent = createComponentFactory({
component: ComponentForTesting,
...dependencies,
}); |
Beta Was this translation helpful? Give feedback.
-
This is great, thank you so much for your amazing support. I think I understand what you're saying, but it's 3am for me. I'll try to play with this tomorrow and see if I actually understand :) |
Beta Was this translation helpful? Give feedback.
-
So I'm making good progress, but I've run into two confusing things. Why don't I always need the importsIn one of my standalone components, I import like so: const dependencies = MockBuilder(HeaderComponent).build()
const createComponent = createRoutingFactory({
component: HeaderComponent,
...dependencies
}) Why don't I have to import either of the Ngb modules? Standalone directive complains about a moduleI have a standalone directive, const dependencies = MockBuilder(
[DisplaySubSpaceContactsComponent, ...],
[TabLinkDirective, ...]
).build()
const createComponent = createComponentFactory({
component: DisplaySubSpaceContactsComponent,
detectChanges: false,
...dependencies
}) it tells me:
|
Beta Was this translation helpful? Give feedback.
-
Let's close this out as we've opened other issues/discussions. |
Beta Was this translation helpful? Give feedback.
it works the next way
MockBuilder(ThingsToKeepAsItIsForTesting, ThingsToMockForTesting)
, starting the version 14 there is an additional behavior forThingsToKeepAsItIsForTesting
, if it's a standalone declaration, thenMockBuilder
will mock all imports and redeclare the decoration:so instead of having
Because you are testing
ExampleComponent
, you …