How to run tests with Bun
Bun appears to draw inspiration from this shift towards a more systematic and automated approach to testing. Jest, a popular testing framework, is a reference for Bun testing features. It offers features like snapshot testing, built-in mocking, and other capabilities to streamline the testing workflow. In addition, Bun provides a fast, built-in Jest-compatible test runner. With Bun, developers can enjoy the following features:
TypeScript and JSX support
Lifecycle hooks for testing components
Snapshot testing for tracking and validating UI component changes
UI and DOM testing capabilities
Watch mode with
--watchfor continuous testing during developmentScript preload with
--preloadto enhance test performance and efficiency
Running a single test with Bun
Tests in Bun are written in JavaScript or TypeScript and use a familiar Jest-like API. We can create test files with names that match specific patterns to be automatically discovered by the test runner. Test files typically have names like *.test{js|jsx|ts|tsx} or *.spec.{js|jsx|ts|tsx} but they can be *_test.{js|jsx|ts|tsx} or *_spec{js|jsx|ts|tsx}.
To run tests using Bun, we employ the following command:
The test runner explores the working directory to locate files matching the prescribed test file patterns and execute the tests within. Here is an example of the outcome when using the bun test command with the test file shown above:
Let’s break down the provided test:
Line 1: We import the
expectandtestfunctions from the"bun:test"module.Line 3: We write a test case with the description
"5 + 5". The arrow function (() => {) is the test function executed when this test case runs.Line 4: Within the test function, we use the
expectfunction to make an assertion. It checks whether the result of the expression5 + 5is equal (toBe(10)) to 10. In other words, it verifies that the sum of 5 and 5 equals 10.
In the above example, a simple test is executed successfully and passed. If a test encounters a failure, the outcome would be different. Let’s see how a test failure scenario unfolds:
As demonstrated, when a test fails, we can precisely pinpoint the location of the failure and understand the reason behind it. This feature provides developers a valuable tool for diagnosing and resolving issues promptly.
Running multiple tests with Bun
Bun’s test runner processes all our tests sequentially within a single environment. This implies that tests are executed one after the other. When a test run is completed, the test runner provides an exit code, which can be vital for automating the build and CI/CD processes to determine the overall test suite outcome.
An exit code of
In the following testing setup, we implement similar test cases in two different files to showcase how the bun test efficiently manages and executes tests across multiple files. Let’s observe how the exit code varies based on test outcomes.
Tests that will fail
In this case, a non-zero exit code indicates the failure of a test.
Tests that will pass
Now, let’s fix the first test to make it pass:
Here, we obtain an exit code of bun test command to customize and control the test execution process.
Conclusion
Running tests with Bun is exceptionally straightforward, and its compatibility with Jest makes it an outstanding choice as a testing framework for our projects. However, it’s important to note that Bun offers a rich array of features beyond what we’ve covered. These include capabilities such as mock tests, watch mode for continuous testing during development, and more. Delving into these advanced features can further enhance our testing workflow and productivity, making Bun a versatile and powerful tool for comprehensive testing in our projects.
Free Resources