@@ -68,6 +68,63 @@ export function test(
6868 }
6969 } ,
7070 } ) ;
71+ } else if ( "Bun" in globalThis ) {
72+ let failed : unknown = undefined ;
73+ // deno-lint-ignore no-inner-declarations
74+ function step ( def : Deno . TestStepDefinition ) : Promise < boolean > ;
75+ // deno-lint-ignore no-inner-declarations
76+ function step (
77+ name : string ,
78+ fn : ( ctx : Deno . TestContext ) => void | Promise < void > ,
79+ ) : Promise < boolean > ;
80+ // deno-lint-ignore no-inner-declarations
81+ function step (
82+ fn : ( ctx : Deno . TestContext ) => void | Promise < void > ,
83+ ) : Promise < boolean > ;
84+ // deno-lint-ignore no-inner-declarations
85+ async function step (
86+ defOrNameOrFn :
87+ | Deno . TestStepDefinition
88+ | string
89+ | ( ( ctx : Deno . TestContext ) => void | Promise < void > ) ,
90+ fn ?: ( ctx : Deno . TestContext ) => void | Promise < void > ,
91+ ) : Promise < boolean > {
92+ let def : Deno . TestStepDefinition ;
93+ if ( typeof defOrNameOrFn === "string" ) {
94+ def = { name : defOrNameOrFn , fn : fn ! } ;
95+ } else if ( typeof defOrNameOrFn === "function" ) {
96+ def = { name : defOrNameOrFn . name , fn : defOrNameOrFn } ;
97+ } else {
98+ def = defOrNameOrFn ;
99+ }
100+ if ( def . ignore ) return true ;
101+ try {
102+ await def . fn ( {
103+ name : def . name ,
104+ origin : "" ,
105+ step,
106+ } ) ;
107+ } catch ( e ) {
108+ failed ??= e ;
109+ return false ;
110+ }
111+ return true ;
112+ }
113+ const ctx : Deno . TestContext = {
114+ name : def . name ,
115+ origin : "" ,
116+ step,
117+ } ;
118+ // deno-lint-ignore no-inner-declarations
119+ async function fn ( ) {
120+ await def . fn ( ctx ) ;
121+ if ( failed ) throw failed ;
122+ }
123+ // @ts -ignore: Bun exists in the global scope in Bun
124+ const bunTest = Bun . jest ( caller ( ) ) . test ;
125+ if ( def . ignore ) bunTest . skip ( def . name , fn ) ;
126+ else if ( def . only ) bunTest . only ( def . name , fn ) ;
127+ else bunTest ( def . name , fn ) ;
71128 } else {
72129 nodeTest ( def . name , { only : def . only , skip : def . ignore } , async ( t ) => {
73130 await def . fn ( intoDenoTestContext ( def . name , t ) ) ;
@@ -120,3 +177,21 @@ function intoDenoTestContext(
120177 } ;
121178 return denoCtx ;
122179}
180+
181+ // Below code is borrowed from https://github.com/oven-sh/bun/issues/11660#issuecomment-2506832106
182+
183+ /** Retrieve caller test file. */
184+ function caller ( ) {
185+ const Trace = Error as unknown as {
186+ prepareStackTrace : ( error : Error , stack : CallSite [ ] ) => unknown ;
187+ } ;
188+ const _ = Trace . prepareStackTrace ;
189+ Trace . prepareStackTrace = ( _ , stack ) => stack ;
190+ const { stack } = new Error ( ) ;
191+ Trace . prepareStackTrace = _ ;
192+ const caller = ( stack as unknown as CallSite [ ] ) [ 2 ] ;
193+ return caller . getFileName ( ) . replaceAll ( "\\" , "/" ) ;
194+ }
195+
196+ /** V8 CallSite (subset). */
197+ type CallSite = { getFileName : ( ) => string } ;
0 commit comments