oven-sh / bun

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

Inject bun:test globals in imported files as well #12034

Open sezanzeb opened 2 months ago

sezanzeb commented 2 months ago

What is the problem this feature would solve?

I'm importing some utility functions for my tests that call beforeEach and afterEach.

Right now, the tests are failing with Can't find variable: beforeEach

https://bun.sh/guides/test/migrate-from-jest says

If you're relying on Jest to inject test, expect, etc. as globals, Bun does that too.

However, this only seems to be the case in actual test files, but not in files that are imported.

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

Inject globals in imported files during tests

What alternatives have you considered?

No response

sezanzeb commented 2 months ago

Writing

global.beforeEach = beforeEach
global.afterEach = afterEach
global.expect = expect

in the file that contains the tests helps as a workaround

srosato commented 1 month ago

I also had to add this workaround to my pre-loaded file.

bunfig.toml:

[test]
preload = ["apps/__tests__/api/bun-preload.ts"]

bun-preload.ts:

/* eslint-disable @typescript-eslint/ban-ts-comment */
import { beforeEach, afterEach, afterAll, expect } from 'bun:test';

// @ts-ignore
global.beforeEach = beforeEach
// @ts-ignore
global.afterEach = afterEach
// @ts-ignore
global.afterAll = afterAll
// @ts-ignore
global.expect = expect

Which is less than ideal. Otherwise my test helpers using beforeEach, afterEach, etc.. will also fail with the same error reported by @sezanzeb