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 -fsSL https://bun.sh/install | bashnpm install -g bunbrew tap oven-sh/bunbrew install bundocker pull oven/bundocker run --rm --init --ulimit memlock=-1:-1 oven/bunTo upgrade Bun:
bun upgradeUp to 80% faster bun test
We've made bun test up to 80% faster, particularly when there are lots of small test files.
bun:test just got 80% faster (in a benchmark rendering React components) pic.twitter.com/BJTW0VqakQ
— Jarred Sumner (@jarredsumner) May 23, 2023
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=testis now properly set.- Environment variables can be read from
.env.testand.env.test.localfiles.
In the next version of Bun
— Jarred Sumner (@jarredsumner) May 23, 2023
"bun test" sets NODE_ENV=test by default and loads .env.test & .env.test.local pic.twitter.com/VuxMUmppxR
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();
});
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.
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.
In the next version of Bun
— Jarred Sumner (@jarredsumner) May 24, 2023
delete require.cache[id] works on ESM pic.twitter.com/xVKYwkVhLy
console.log(headers) shows headers
in the next version of Bun
— Jarred Sumner (@jarredsumner) May 26, 2023
console.log(headers) shows the headers pic.twitter.com/RKiBNlffFs
console.log(searchParams) shows params
in the next version of Bun
— Jarred Sumner (@jarredsumner) May 26, 2023
console.log(headers) shows the headers pic.twitter.com/RKiBNlffFs
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
FragmentfromjsxImportSourcewhen using the "automatic" JSX runtime. - Bun was incorrectly choosing the "production" JSX runtime when using
jsxImportSourceandNODE_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:httpsnow uses the correct port when usingnode:httpsandportis not set. Thanks to @cirospaciari for the fix.node:httpnow has the correct return type forgetHeader(). Thanks to @Jarred-Sumner for the fix.node:httpsnow correctly uses{ tls: true }when usingcreateServer(). Thanks to @cirospaciari for the fix.
bun install bugfixes
- A bug that could cause bun to fail to symlink binaries into
node_modules/.binhas 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 @paperclover.
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
#3008 | Fixed port for HTTPS when using node:https by @cirospaciari |
#3007 | Fixed return type of getHeader() in node:http by @Jarred-Sumner |
#3012 | Fixed { tls: true } when using createServer() by @cirospaciari |
#3018 | Implemented process.env.TZ and changed the default timezone of bun test to Etc/UTC by @Jarred-Sumner |
#3015 | Fixed a bug with test.todo() and async functions by @Jarred-Sumner |
f71eb39 | Reduced aggresiveness of garbage collection during bun test by @Jarred-Sumner |
#3032 | Fixed a bug where module.exports had a key with a space by @dylan-conway |
#3040 | Implemented bun test --timeout N that can change the default per-test timeout by @Electroid |
#3045 | Implemented require.cache by @Jarred-Sumner |
#3041 | Fixed a bug with emojis in tagged template literals by @Jarred-Sumner |
#3057 | Fixed various bugs with jsxImportSource, jsxFactory, and jsxFragmentFactory by @dylan-conway |
#3051 | Fixed crash when using server.fetch(Request) by @cirospaciari |
#3037 | Implemented support to load .env.test and .env.{test,production,development}.local by @Jarred-Sumner |