We noticed that unit tests take a lot of time to execute while not containing anything heavy in terms of computations.
Most of the time, those tests contain some mocks and simple checks that should be rather fast. Mocks are stored in memory and we are not calling any third-party services so the large amounts of time required for tests to execute are not justified. This also impacts our builds as they are slower than expected.
βοΈ Solution
There are multiple things we can do to improve unit tests speed. An example is to optimize imports since jest (our test runner) takes more time to process large imports than specific ones.
Example from packages/ui:
This is slower:
import { StakePlusIcon } from '../../src/components';
than this:
import { StakePlusIcon } from '../../src/components/Icons';
π Subtasks
[ ] Optimize imports. Opt in for specific imports than global ones.
[ ] Investigate if we can run only new tests or tests for which the underlying components/modules have changed with https://jestjs.io/docs/cli#--onlychanged. This is for the test:ci command.
π Context
We noticed that unit tests take a lot of time to execute while not containing anything heavy in terms of computations.
Most of the time, those tests contain some mocks and simple checks that should be rather fast. Mocks are stored in memory and we are not calling any third-party services so the large amounts of time required for tests to execute are not justified. This also impacts our builds as they are slower than expected.
βοΈ Solution
There are multiple things we can do to improve unit tests speed. An example is to optimize imports since jest (our test runner) takes more time to process large imports than specific ones.
Example from
packages/ui
:This is slower:
than this:
π Subtasks
test:ci
command.--runInBand
flag fortest:ci
. For reference - https://jestjs.io/docs/cli#--runinband.π― Definition of Done