@@ -123,6 +123,10 @@ export async function capsule({
123123 type : CapsulePropertyTypes . Literal ,
124124 value : [ ] as string [ ] ,
125125 } ,
126+ _capturedDescribeStack : {
127+ type : CapsulePropertyTypes . Literal ,
128+ value : null as string [ ] | null ,
129+ } ,
126130 _snapshotCounters : {
127131 type : CapsulePropertyTypes . Literal ,
128132 value : new Map < string , number > ( ) ,
@@ -196,7 +200,9 @@ export async function capsule({
196200 const expect = this . bunTest . expect
197201
198202 // Build the snapshot key from describe stack + current it name
199- const parts = [ ...this . _describeStack ]
203+ // Use captured stack from it() if available, otherwise fall back to _describeStack
204+ const stack = this . _capturedDescribeStack ?? this . _describeStack
205+ const parts = [ ...stack ]
200206 if ( this . _currentItName ) parts . push ( this . _currentItName )
201207 const baseKey = parts . join ( ' > ' )
202208
@@ -282,20 +288,22 @@ export async function capsule({
282288 const self = this
283289 const bunTestModule = this . bunTest
284290 const describeMethod = ( name : string , fn : ( ) => void ) => {
285- return bunTestModule . describe ( name , async ( ) => {
291+ // Wrap in bun's describe - push/pop happens INSIDE the callback
292+ // because bun executes describe callbacks asynchronously
293+ return bunTestModule . describe ( name , ( ) => {
286294 self . _describeStack . push ( name )
287295 try {
288- await fn ( )
296+ fn ( )
289297 } finally {
290298 self . _describeStack . pop ( )
291299 }
292300 } )
293301 }
294302 describeMethod . skip = ( name : string , fn : ( ) => void ) => {
295- return bunTestModule . describe . skip ( name , async ( ) => {
303+ return bunTestModule . describe . skip ( name , ( ) => {
296304 self . _describeStack . push ( name )
297305 try {
298- await fn ( )
306+ fn ( )
299307 } finally {
300308 self . _describeStack . pop ( )
301309 }
@@ -310,7 +318,11 @@ export async function capsule({
310318 const self = this
311319 const bunTestModule = this . bunTest
312320 const itMethod = ( name : string , fn : ( ) => void | Promise < void > , options ?: number | BunTest . TestOptions ) => {
321+ // Capture describe stack at registration time (synchronous)
322+ const capturedStack = [ ...self . _describeStack ]
313323 return bunTestModule . it ( name , async ( ) => {
324+ // Set captured stack for snapshot key generation
325+ self . _capturedDescribeStack = capturedStack
314326 self . _currentItName = name
315327 try {
316328 await fn ( )
@@ -326,16 +338,20 @@ export async function capsule({
326338 }
327339 throw error
328340 } finally {
341+ self . _capturedDescribeStack = null
329342 self . _currentItName = null
330343 }
331344 } , options )
332345 }
333346 itMethod . skip = ( name : string , fn : ( ) => void | Promise < void > , options ?: number | BunTest . TestOptions ) => {
347+ const capturedStack = [ ...self . _describeStack ]
334348 return bunTestModule . it . skip ( name , async ( ) => {
349+ self . _capturedDescribeStack = capturedStack
335350 self . _currentItName = name
336351 try {
337352 await fn ( )
338353 } finally {
354+ self . _capturedDescribeStack = null
339355 self . _currentItName = null
340356 }
341357 } , options )
0 commit comments