question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to test the Component in isolation without the HTML template?

See original GitHub issue

It seems that TestBed.configureTestingModule({ provider: MyComponent }).createComponents() is being called for each shallow render. Is it possible to call it without TestBed.configureTestingModule({ provider: MyComponent }).createComponents()? So it will look like this: TestBed.configureTestingModule({ provider: MyComponent })

I quite like the mocking ability with this library so it would be great if I could not call .createComponents() in my tests. This is so I can test the component in isolation.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
azaeng04commented, Aug 13, 2021

Thanks so much @getsaf!

See the Renderer2 is included as a dependency. constructor MyComponent(private Renderer2) So when running the tests. The TestBed complains that it is not being provided. How do I work around this with shallow.render()?

Thanks for the tip! I am still just playing around with the lib to get an idea of how it works.

Side note: I would love to invite you to this Angular community I recently joined: https://www.angularnation.net/. It would be great if you could do a talk on this amazing lib you wrote. I find it REALLY helpful and I am sure other devs would too!

0reactions
getsafcommented, Aug 13, 2021

The console in your screenshot says that the Renderer2 class is abstract, those can’t be used as Angular providers. I think Renderer2 is an internal Angular thing that you should not need to provide it manually. I’m also pretty sure you don’t need to use provideMock in your shallow options.

IMO, rendering the component in a beforeEach is likely gonna cost you more code/headache than just calling shallow.render() it in each test because you lose access all the built-into shallow-render tools like mocking/query/fixture, etc when you render before your test executes.

Just suggestions here, but this kind of refactor could save you time and is more in-line with shallow-render’s intended use:

let shallow: Shallow<MyComponent>;
beforeEach(() => {
  // Only define your baseline setup but do not render here...
  shallow = new Shallow(MyComponent, MyModule);
});

it('displays the service total when the button is clicked', async () => {
  const { find, fixture } = await shallow
    .mock(MyService, { getResponse: () => ({ total: 10000 }) })
    .render();

  find('button').click();
  fixture.detectChanges();

  expect(find('label').nativeElement.innerText).toContain('Response was 10,000');
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I design/test an Angular component without running ...
Run the backend app (because of authentication checks etc. · Run the angular app ( ng serve handles this and will obviously auto...
Read more >
Three Ways to Test Angular Components | by Victor Savkin
In this article we will look at three ways to test Angular components: isolated tests, shallow tests, and integration tests.
Read more >
What is Component Testing? Techniques, Example Test Cases
Component testing done without isolation of other components in the software or application under test is referred as Component Testing Large.
Read more >
Basics of testing components - Angular
Component class testing should be kept very clean and simple. It should test only a single unit. At first glance, you should be...
Read more >
How to test React Components in isolation with Storybook
A guide and tutorial on how to test React components in isolation using Storybook. This includes guide uses a 'React counter' for code ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found