svelteness / svelte-jester

A Jest transformer for Svelte - compile your components before importing them into tests.
MIT License
128 stars 18 forks source link

Add mechanism to call beforeEach() and afterEach() manually inside an it block #154

Open claremacrae opened 1 year ago

claremacrae commented 1 year ago

Thank you for creating this library. It has made it possible to test a svelte component in Jest.

There are some places where we want to make multiple calls in the same it function, instead of in 2 separate it blocks.

(This is because we wish to collate the outputs of a number of calls with different inputs in to a single test output)

For example, this works:

    it('test1', async () => {
        await doSvelteStuff();
    });
    it('test2', async () => {
        await doSvelteStuff();
    });

However, if we need this to be in the same block.

    it('test1And2', async () => {
        await doSvelteStuff();
        await doSvelteStuff();
    });

Then it gives us this kind of error:

Found multiple elements with the text: Apply

What we would like to be able to is clean up the svelte state manually in the middle of an it, something like either:

    it('test1And2', async () => {
        await doSvelteStuff();
        svelte_jester.cleanUp(); // simulate effect of afterEach()
        svelte_jester.startUp(); // simulate effect of beforeEach()
        await doSvelteStuff();
    });

Or...

    it('test1And2', async () => {
        await doSvelteStuff();
        svelte_jester.reset();
        await doSvelteStuff();
    });

Or maybe there is already a way to do this?

If it helps, we (myself and @isidore) would be happy to pair with someone on this.

sebastianrothe commented 11 months ago

This should have nothing to do with this library. Is doSvelteStuff(); an side-effect?

Maybe you can try adding await tick(); in between. This lets Svelte re-calculate all expressions.