status
dsq_thread_id
Setting up dev tooling sucks, especially in the world of JavaScript. This is why I’m sometimes excited about
bun
(https://bun.sh/), which promises to handle all the configuration so you can just code. Great.I’ll show you a specific example of where bun can help—setting up a unit-testing framework.
The quick version
Create a typescript file with the
*.test.ts
suffix anywhere in your project. Example:// src/quick.test.ts
import { test, expect } from "bun:test";
test("this is an example test", () => {
expect(1).toBe(2); // Should fail
});
Run the tests:
bun test
That is all.
The Good
The above is all you need to set up unit testing with bun. Compare this to your memory, possibly repressed, of setting up
jest
, or even vitest
which requires some configuration. For the most part bun lives up to its promise of working straight away out of the box.
Here’s what’s nice about this setup:
- Fast to set up, you don’t configure anything (you do need to install
bun
, admitedly)
- Fast to run
- Familiar API
This last point is perhaps the most important—You don’t need to learn anything new in order to write tests this way.
The Bad
Unfortunately, there are some caveats here. The first issue is that the above code will run, but not type check. Bun, for all it’s excellent DX, has a surprisingly poor experience when it comes to adding types.
The docs will tell you:
Install the types:
npm install -D bun-types
. Then configure your tsconfig.json
:{
"compilerOptions": {
+ "types": ["bun-types"]
}
}
However, this will likely cause errors with the other types in your project! Not a good experience.
The gist is, the
bun-types
explicitly exclude the usual built-in types for the dom, assumedly because Bun is a runtime and some of their types differ. However, for the testing use case we don’t care about the bun runtime, we just to be able to import from bun:test
and get nice autocomplete.As I write this there’s an open issue about this on Github. Bun recently hit v1 and as a venture-backed startup will undoubtedly be looking to get a foothold in larger companies (read: existing codebases) so perhaps this issue will be resolved soon.
Adding the types
As mentioned in that Github issue, there is a workaround: Manually re-import the built-in typescript types by referencing them in your own
types.d.ts
file./// <reference lib="dom" />
Make sure that file, wherever you put it, is included by typescript (i.e. the
"include": [...]
configuration in tsconfig.json
.Overall
I’m still happy with what Bun has done here. The initial setup experience is significantly better than what I remember from both
jest
and vitest
(not to mention mocha
...). It’s a step in the right direction, and the issues are solvable.I do worry about the stability of the company (as opposed to the product). Until they get acquired (maybe by Vercel) it seems very unclear from the outside how they will manage to provide a better edge-hosting offering than the incumbents, let alone build a business compelling enough to make investors happy.