svelte-jester icon indicating copy to clipboard operation
svelte-jester copied to clipboard

jest code coverage - uncovered lines

Open sebastian-meier opened this issue 3 years ago • 9 comments

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

sebastian-meier avatar Apr 30 '21 14:04 sebastian-meier

I have same issue..

yunseop-bbrick avatar Jun 11 '21 05:06 yunseop-bbrick

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

sebastianrothe avatar Sep 15 '21 18:09 sebastianrothe

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: https://github.com/mihar-22/svelte-jester/issues/25 As soon as this one is resolved i will try again.

sebastian-meier avatar Sep 16 '21 09:09 sebastian-meier

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

sebastian-meier avatar Sep 16 '21 12:09 sebastian-meier

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

enjoythelive1 avatar Sep 16 '21 12:09 enjoythelive1

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: https://github.com/mihar-22/svelte-jester/issues/25 As soon as this one is resolved i will try again.

#25 is resolved, already

sebastianrothe avatar Sep 16 '21 15:09 sebastianrothe

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.

sebastian-meier avatar Sep 16 '21 15:09 sebastian-meier

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?

tveronezi avatar Mar 16 '22 12:03 tveronezi

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

sebastianrothe avatar Mar 16 '22 15:03 sebastianrothe