Skip to content

First test running render/renderHook is slow #1423

Closed
@KrastanD

Description

@KrastanD

Describe the bug

The first test in a file that uses render/renderHook is slow. This is exacerbated when ran without jest cache.

Locally on my M1 Pro Macbook:
Without jest cache, the first test takes ~27 times as long as the second test. 1512ms vs 55ms
With jest cache, the first test takes ~3 times as long which seems reasonable. 142ms vs 56ms

On Github Actions:
Without jest cache, the first test takes ~70 times as long as the second test.

I know it's not related to the actual tests because if I change the order of them it is always the one that is first that takes significantly longer.

Expected behavior

I would expect that it takes the tests to run under a second whether they are cached or not, first or not.

Steps to Reproduce

React Query has an example repo using CRA and react testing library with some sample tests here
I recreated the repo and tests using expo and react native testing library here

When running the RTL repo tests without cache I get the following:
image
All tests take under 100 ms.

When running the RNTL repo tests without cache I get the following:
image
Both first tests take 1.6 seconds.

I also ran the RNTL repo tests on Github Actions here with the following results:
image
Interestingly its only the first renderHook that takes a long time of 4.1 seconds.

I created a branch in the RNTL repo called renderHook-isolated. There I created two new test files that don't use react-query or msw - just basic react-native and react native testing library.
Here are the results:
image
Even the very basic react hook test takes 3/4 of a second and it seems react query just makes the matter worse.

From my understanding, renderHook just calls the hook in a 'rendered Test Component so i would assume the issue is in the render` function. I won't pretend to know how it works but perhaps whatever setup jest and RNTL do before the tests is somehow leaking into the first test.

Curious to hear your thoughts and thanks in advance!

Note: This is very specific but I noticed that this issue is resolved if I first use render on any component wrapped in a TamaguiProvider in a separate test in the same file.
Perhaps rendering something as complicated as the TamaguiProvider causes a big enough delay to not leak into the other tests. 🤷

Versions

 npmPackages:
@testing-library/react-native: ^12.1.2 => 12.1.2 
react: 18.2.0 => 18.2.0 
react-native: 0.71.8 => 0.71.8 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions