Closed dereklin closed 4 years ago
Thanks for the post.
Shallow does allow multiple ways of testing nested components. There are a few issues going on in your post though:
const { find } = await shallow.render(`
<my-component [someProp]="somePropVal" (someAction)="handleSomeAction()">
</my-component>
`,
{ bind: { somePropVal: 'foo', someAction: () => {} } }
);
Here are some example tests with nested components that may help: https://stackblitz.com/github/getsaf/shallow-render-stackblitz?file=examples%2Fmultiple-components.spec.ts https://stackblitz.com/github/getsaf/shallow-render-stackblitz?file=examples%2Fcomponent-with-content-child.spec.ts
@getsaf Thanks for the reply and hints. I am using version 8.5.2 now. Now I am getting:
MockOfEmptyAnchor cannot be used as an entry component.
My usual angular testbed looks like:
TestBed.configureTestingModule({
imports: [MyTestingModule, MyModule],
declarations: [TestComponent] // I am creating a Test component as a host for testing
}).compileComponents();
My shallow-render set up looks like:
let shallow: Shallow<TestComponent>;
beforeEach(() => {
shallow = new Shallow(TestComponent, MyModule);
});
Is the error because I am not importing the mock module MyTestingModule? How to import the whole module instead of mocking individual services like
shallow = new Shallow(ColorLabelComponent, ColorModule.forRoot()).mock(RedService, { color: () => 'MOCKED COLOR' });
?
You cannot use shallow render and TestBed at the same time. Shallow handles all test module configuration for you. You also should not use test modules or host components. Shallow was designed to eliminate the need for all those things.
I would recommend looking at the docs and the example tests. Your answers are in there.
Also there's a StackBlitz with live examples: https://stackblitz.com/github/getsaf/shallow-render-stackblitz
Here's an example test with multiple components: https://github.com/getsaf/shallow-render/blob/master/lib/examples/multiple-components.spec.ts
I am trying to understand how this library works.
I am trying to test some nested components like this:
I get:
Could you point out the mistakes I am making?