bun icon indicating copy to clipboard operation
bun copied to clipboard

Support the `node:test` built-in API

Open jbergstroem opened this issue 2 years ago • 4 comments

What is the problem this feature would solve?

As part of evaluating/migrating to bun from node, it is cumbersome to use both node:test and bun:test at the same time.

What is the feature you are proposing to solve the problem?

bun should support the node:test built-in API.

What alternatives have you considered?

Maintain a separate suite or use branching logic.

jbergstroem avatar Sep 12 '23 12:09 jbergstroem

Does it make sense to add a PR about it not being supported in the documentation?

jbergstroem avatar Sep 12 '23 12:09 jbergstroem

My usage is to use one test code to test both bun and node compatibility. Why bun can recogonize the 'test' module?

❯ t

> [email protected] test
> bun test

bun test v1.0.25 (a8ff7be6)

test/index.test.ts:
error: Cannot find package "test" from "/Users/yutengjing/code/fetch-github-trending/test/index.test.ts"

 0 pass
 1 fail
Ran 1 tests across 1 files. [5.00ms]
image

tjx666 avatar Jan 23 '24 06:01 tjx666

Hey @jbergstroem do we have any solution/alias to this?

We are currently trying to run our test suite (written with node:test) using bun to ensure it works on both node.js and bun.js.

I'm not sure, but probably adding support for https://github.com/nodejs/node-core-test should be the aimed solution here.

node core test package in bun throws these two errors:

❯ bun test
bun test v1.0.30 (1424a196)

test/components.test.tsx:

[bun] Warning: async_hooks.executionAsyncId/triggerAsyncId are not implemented in Bun. It will return 0 every time.
[bun] Warning: async_hooks.createHook is not implemented in Bun. Hooks can still be created but will never be called.

 0 pass
 0 fail
Ran 0 tests across 1 files. [601.00ms]

arthurfiorette avatar Mar 25 '24 11:03 arthurfiorette

Node.js has published a user land package for node:test. Bun could use this to support node:test. Not sure how much work this requires to make it compatible with the bun test command and coverage reporter.

Conaclos avatar Oct 18 '24 23:10 Conaclos

Would require deep support for async_hooks, as my above comment said.

Sadly, unless using bun's native test and jest on node is a possible solution, i couldn't find another ways to run the same test suite on both runtimes.

arthurfiorette avatar Oct 21 '24 02:10 arthurfiorette

Would require deep support for async_hooks

Even if node:test could be just a wrapper around bun:test? I think it is what Deno is doing because running deno test is also compatible with node:test.

Conaclos avatar Oct 21 '24 07:10 Conaclos

Maybe, but special work would be needed around matchers, since node:test uses node:assert and bun:test uses their own jest based matchers.

arthurfiorette avatar Oct 21 '24 12:10 arthurfiorette

It looks like node:test does pretend to be bun:test now: https://github.com/oven-sh/bun/blob/main/src/js/node/test.ts

However I don't think its fully supported nicely as it uses assert from 'node:assert' instead of expect api

RiskyMH avatar Jan 31 '25 05:01 RiskyMH

node:test support is currently completely broken in bun as of 1.2.18 (and I did recheck on v1.2.19-canary.29) why does bun even pretend to support it?

ChALkeR avatar Jul 10 '25 04:07 ChALkeR

this should be disabled (at least in releases) until basic functionality (i.e. describe/test) works currently it appears to be present but doesn't run tests just marks anything as passing which could really confuse someone into thinking they are running tests on bun when they aren't

ChALkeR avatar Jul 10 '25 04:07 ChALkeR

However I don't think its fully supported nicely as it uses assert from 'node:assert' instead of expect api

For assert I am using https://jsr.io/@std/assert instead of node:assert and my problems went away =)

zvictor avatar Aug 29 '25 12:08 zvictor

I think this issue is still relevant: https://github.com/oven-sh/bun/issues/23077

alcuadrado avatar Nov 02 '25 22:11 alcuadrado