oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.17k stars 2.77k forks source link

Cannot re-export `bun:test` contents #5400

Open lukeed opened 1 year ago

lukeed commented 1 year ago

What version of Bun is running?

1.0.1+31aec4ebe325982fc0ef27498984b0ad9969162

What platform is your computer?

Darwin 21.3.0 arm64 arm

What steps can reproduce the bug?

/// demo/index.test.ts
import { test, expect } from 'bun:test';

test('hello', () => {
    expect(1).pass();
});

test('world', () => {
    expect(123).toBeGreaterThan(0);
});

/// demo/foobar.test.ts
import { test, expect } from './wrapper';

test('hello', () => {
    expect(1).pass();
});

test('world', () => {
    expect(123).toBeGreaterThan(0);
});

/// demo/wrapper.ts
export { test, expect } from 'bun:test';

Then bun test demo

What is the expected behavior?

The foobar.test.ts should be treated exactly the same as index.test.ts

What do you see instead?

bun test v1.0.1 (31aec4eb)

other/index.test.ts:
✓ hello [0.02ms]
✓ world [0.01ms]

other/foobar.test.ts:

error: Cannot find package "test" from "/Users/.../demo/wrapper.ts"

 2 pass
 1 fail
 2 expect() calls
Ran 3 tests across 2 files. [13.00ms]

Additional information

This happens with describe, test/it, and expect (so far)

With expect, for example, the same Cannot find package "test" error appears, so my hunch is that on-the-fly transpilation is dropping the bun: namespace & looking for the test node_module.

mc-petry commented 8 months ago

Same issue

benmerckx commented 5 months ago

For anyone looking to workaround this to test in multiple runtimes, what you can do for now is pass import.meta and use it to declare bun's test function.

export function createTest(meta) {
  return Bun.jest(meta.path).test
}
// a.js
const test = createTest(import.meta)
test(...)
// b.js
const test = createTest(import.meta)
test(...)

I used this principle in a slightly nicer api to create a lib that can be used to define tests that run with Bun, Node and Deno's native runners (https://github.com/benmerckx/suite)