testing-library / svelte-testing-library

:chipmunk: Simple and complete Svelte DOM testing utilities that encourage good testing practices
https://testing-library.com/docs/svelte-testing-library/intro
MIT License
608 stars 34 forks source link

Rerender stopped working on version 5.1.0 #369

Closed LuisaAPF closed 2 months ago

LuisaAPF commented 2 months ago

Tests that use rerender started failing after upgrading from 4.1.0 to 5.1.0. I understand the function behaviour changed recently, but this is not working for me:

it('should show MyComponent when show = true', async () => {
    const { rerender } = render(MyComponent, {
      props: {show: true}
    })

    expect(screen.queryByTestId('my_component')).toBeInTheDocument()

    await rerender({props: {show: false}})

    expect(screen.queryByTestId('my_component')).not.toBeInTheDocument()
})
mcous commented 2 months ago

Apologies, the docs PR detailing this change ~has not yet merged~ was only just merged today.

You now simply pass props to rerender directly. You should be seeing a warning log to this effect.

- await rerender({props: {show: false}})
+ await rerender({show: false})
LuisaAPF commented 2 months ago

Thanks for your reply @mcous. I think I figured out the reason the tests broke. It wasn't because of the syntax (by the way, I didn't see any warnings, not sure if Vitest is suppressing them). It was because I was not using a reactive variable to hide my component, but instead a regular variable that depends on the prop and which was being assigned on mount.

mcous commented 2 months ago

Gotcha; that makes sense! rerender in v4 destroyed and remounted the component, which would prevent you from seeing those sorts of reactivity bugs. That's why it was changed!