DEV Community

givehug
givehug

Posted on • Edited on

4

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 && \

# smoke test
    bun --version
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 ...

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay