DEV Community

givehug
givehug

Posted on • Updated on

Speed up your Jest test suite with Bun

Adding Bun to your existing Jest project

Probably the most impressive performance gains you can get with Bun right now (some days after 1.0.0 release) is the speed of its test runner. You can make your Jest based test suite 10x faster or even more. This is some pretty mind boggling performance that we were not used to in JS community.

Image description

If you already have an existing mature project, you can start integrating Bun gradually. Change the suffix of the files you are going to test with Bun from eg. .test. to .spec. (or vice versa) and keep testing the others with Jest. You can start testing pure functions, utilities, etc, everything that does not depend on module mocking or some native platform api with Bun. The jest.mock support might be coming as well, track the progress here.

For Bun create an npm script "test:bun": "bun test .test.". For Jest - "test:jest": "jest --config {pathToConfigFile}", and add the following line to jest config testPathIgnorePatterns: ['.test.'], to exclude .test. files from execution. Simple. Now you can run both commands one after another.

Installing Bun on Node Alpine docker image for CI

If you use Gihub Actions, just get oven-sh/setup-bun, nothing else needed.

Bun also ships official docker image, however its pure Bun, not slim or alpine based, and not very optimised yet.

To install Bun on your existing Node container, check one of their dockerfiles (most of them WIP). On Alpine you will just need to pre-install unzip and glibc.


prepare environment

WORKDIR /tmp
RUN apk --no-cache add unzip

get bun

ADD https://github.com/oven-sh/bun/releases/latest/download/bun-linux-x64.zip bun-linux-x64.zip
RUN unzip bun-linux-x64.zip

get glibc

ARG GLIBC_RELEASE
RUN wget https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_RELEASE}/glibc-${GLIBC_RELEASE}.apk

IMAGE

FROM alpine:latest

install bun

COPY --from=get /tmp/bun-linux-x64/bun /usr/local/bin

prepare glibc

ARG GLIBC_RELEASE
COPY --from=get /tmp/sgerrand.rsa.pub /etc/apk/keys
COPY --from=get /tmp/glibc-${GLIBC_RELEASE}.apk /tmp

install glibc

RUN apk --no-cache add /tmp/glibc-${GLIBC_RELEASE}.apk && \

cleanup

rm /etc/apk/keys/sgerrand.rsa.pub && \
rm /tmp/glibc-${GLIBC_RELEASE}.apk && \
Enter fullscreen mode Exit fullscreen mode

smoke test

bun --version
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode




Side notes

Drop nodemon and ts-node. Replace with bun --watch index.ts / bun --hot index.ts / bun index.ts.

Repo with examples

... coming soon ...

Top comments (0)