Bun

Bun v0.6.4


Ashcon Partovi · May 26, 2023

We're hiring C/C++ and Zig engineers to build the future of JavaScript! Join our team →

Last week, we launched our new JavaScript bundler in Bun v0.6.0.

Today, we're releasing improvements to bun test, Node.js compatibility bugfixes, console.log() improvements, modifiable timezone, require.cache support, bundler bugfixes and more.

curl
npm
brew
docker
curl
curl -fsSL https://bun.sh/install | bash
npm
npm install -g bun
brew
brew tap oven-sh/bun
brew install bun
docker
docker pull oven/bun
docker run --rm --init --ulimit memlock=-1:-1 oven/bun

To upgrade Bun:

bun upgrade

Up to 80% faster bun test

We've made bun test up to 80% faster, particularly when there are lots of small test files.

Bun was over-scheduling the garbage collector to run on every file synchronously. It already schedules GC whenever the heap size changes, so this is unnecessary. This resulted in a 9% growth in memory usage, or so, which is fine given that comparing other test runners with the same code, Bun uses 2-4x less memory.

bun test reads .env.test and .env.test.local

  • NODE_ENV=test is now properly set.
  • Environment variables can be read from .env.test and .env.test.local files.

bun test timeouts

You can now set a timeout for your tests by passing --timeout or by passing a third optional number argument to test().

import { test } from "bun:test";

test("i took too long", async () => {
  await Bun.sleep(100);

  // the last argument is a timeout in milliseconds
}, 50);

expect().toHaveLength() and expect().toBeEmpty()

expect().toHaveLength() and expect().toBeEmpty() are now supported. These test matchers are available on Blob, Buffer, File, Headers, Map, Set, String, TypedArray, and Array-like objects.

import { expect, test } from "bun:test";

test("expect().toHaveLength()", () => {
  expect([1, 2, 3]).toHaveLength(3);
  expect([1, 2, 3]).not.toHaveLength(2);

  // Works on Blob, Buffer, File, Headers, Map, Set, String, TypedArray, and Array-like objects, or anything with a .length property
  expect(new Headers({ Origin: "https://bun.sh" })).toHaveLength(1);
});

test("expect().toBeEmpty()", () => {
  expect([]).toBeEmpty();
  expect([1]).not.toBeEmpty();

  // Works on Bun.file() too
  expect(Bun.file(import.meta.path)).not.toBeEmpty();
});

View on Twitter

Change the timezone

You can now change the timezone that Bun uses for Date and Intl by setting TZ environment variable or assigning process.env.TZ in your code. This is also used by Node.js.

process.env.TZ = "America/New_York";

When using bun test, instead of using your machine's timezone, the default timezone is set to Etc/UTC to help prevent flaky tests when running in your CI environment.

View on Twitter

Implemented require.cache

require.cache is a Node.js API that allows you to delete a module from the cache. This is useful for controlling hot reloading behavior, and is now supported in Bun. Unlike Node.js, delete require.cache[id] works with ESM too.

console.log(headers) shows headers

console.log(searchParams) shows params

Bundler bugfixes

Thanks to @dylan-conway for fixing:

  • Bun was auto-importing JSX when using the "classic" JSX runtime, which is incorrect.
  • Bun was not importing Fragment from jsxImportSource when using the "automatic" JSX runtime.
  • Bun was incorrectly choosing the "production" JSX runtime when using jsxImportSource and NODE_ENV=development (or unset) in certain case.

Transpiler bugfixes

  • A regression related to printing tagged template literals containing non-ascii characters when running in Bun's runtime has been fixed.

Node.js compatiblity bugfixes

  • node:https now uses the correct port when using node:https and port is not set. Thanks to @cirospaciari for the fix.
  • node:http now has the correct return type for getHeader(). Thanks to @Jarred-Sumner for the fix.
  • node:https now correctly uses { tls: true } when using createServer(). Thanks to @cirospaciari for the fix.

bun install bugfixes

  • A bug that could cause bun to fail to symlink binaries into node_modules/.bin has been fixed, thanks to @alexlamsl.

Rewrote JavaScript builtins in TypeScript

Bun's internal JavaScript builtins are now implemented in TypeScript and bundled + minified with bun build, which reduces Bun's binary size by about 900 KB and makes it easier for us to catch bugs. This is thanks to @paperdave.

It also improves Bun's development velocity on these JavaScript builtins because the previous builtins generator script used by WebKit takes around 9s to run and this new builtins generator script takes around 0.9s.

Changelog

#3008Fixed port for HTTPS when using node:https by @cirospaciari
#3007Fixed return type of getHeader() in node:http by @Jarred-Sumner
#3012Fixed { tls: true } when using createServer() by @cirospaciari
#3018Implemented process.env.TZ and changed the default timezone of bun test to Etc/UTC by @Jarred-Sumner
#3015Fixed a bug with test.todo() and async functions by @Jarred-Sumner
f71eb39Reduced aggresiveness of garbage collection during bun test by @Jarred-Sumner
#3032Fixed a bug where module.exports had a key with a space by @dylan-conway
#3040Implemented bun test --timeout N that can change the default per-test timeout by @Electroid
#3045Implemented require.cache by @Jarred-Sumner
#3041Fixed a bug with emojis in tagged template literals by @Jarred-Sumner
#3057Fixed various bugs with jsxImportSource, jsxFactory, and jsxFragmentFactory by @dylan-conway
#3051Fixed crash when using server.fetch(Request) by @cirospaciari
#3037Implemented support to load .env.test and .env.{test,production,development}.local by @Jarred-Sumner