Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest code coverage - uncovered lines #43

Open
sebastian-meier opened this issue Apr 30, 2021 · 9 comments
Open

jest code coverage - uncovered lines #43

sebastian-meier opened this issue Apr 30, 2021 · 9 comments
Labels
bug Something isn't working question Further information is requested

Comments

@sebastian-meier
Copy link

When i use jest --coverage, all tests pass, but some branches do not reach 100% and produce uncovered lines.

Example from a boilerplate of mine.
image

To reproduce a very simple component:

button.svelte

<script lang="ts">
  export let label = '';
</script>

<button>{label}</button>

button.test.ts

import { render } from '@testing-library/svelte'
import Button from '../views/components/button.svelte'

test('button - props', () => {
  const label = 'Click';
  const setLabel = 'New Label';
  const { getByText, component } = render(Button, { label });

  expect(() => getByText(label)).not.toThrow();

  component.label = setLabel;
  expect(() => getByText(setLabel)).not.toThrow();

  component.label = undefined;
  expect(() => getByText('undefined')).toThrow();

  component.label = null;
  expect(() => getByText('null')).toThrow();

  delete component.label;
  expect(() => getByText('null')).toThrow();
})

test('button - no prop', () => {
  global.console.warn = jest.fn()
  const { container } = render(Button);
  expect(container.innerHTML).toBe('<div></div>')
expect(console.warn).toBeCalled()
})

test('button - false prop', () => {
  global.console.warn = jest.fn()
  const {container} = render(Button, { falseProp: false });
  expect(container.innerHTML).toBe('<div></div>')
  expect(console.warn).toBeCalled()
})

It's possible to increase the component's coverage, by introducing the following:
button.svelte

<script lang="ts">
  export let label = '';
  $: cLabel = label || '';
</script>

{#if cLabel}
<button>{cLabel}</button>
{/if}

But even then one else path not taken remains. Which to me makes no sense at that point:
image

Using svelte-jester v1.5.0
Full setup can be found in this repo

@yunseop-bbrick
Copy link

I have same issue..

@sebastianrothe
Copy link
Collaborator

Can you please retry with the latest version and jest 27+?

@sebastianrothe sebastianrothe added bug Something isn't working question Further information is requested labels Sep 15, 2021
@sebastian-meier
Copy link
Author

Upgrading to 27 led to a lot of problems. I think i could resolve most of them, but the last problem depends on this issue being solved: #25 As soon as this one is resolved i will try again.

@sebastian-meier
Copy link
Author

So i have downloaded a copy of the current dev version and got it working. But sadly the problem remains.

@enjoythelive1
Copy link

The same has happen to me, specifically with the {#if} blocks that do not have {:else}.

@sebastianrothe
Copy link
Collaborator

Upgrading to 27 led to a lot of problems. I think i could resolve most of them, but the last problem depends on this issue being solved: #25 As soon as this one is resolved i will try again.

#25 is resolved, already

@sebastian-meier
Copy link
Author

Sorry, yes its resolved, BUT, the last few comments on #25 highlight that the next release 2.1.3 will completely fix it. I have used the current dev state for testing not 2.1.2. Anyhow, 27 sadly did not fix the problem.

@tveronezi
Copy link

Where do you guys think the problem is? I can't find a place in the code it could be. Maybe it's outside this project? Maybe svelte/compiler?

@sebastianrothe
Copy link
Collaborator

I think so too. It is either jest or svelte.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants