|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +constassert=require('node:assert'); |
| 4 | +const{ test, getTestContext, describe, it }=require('node:test'); |
| 5 | + |
| 6 | +// Outside a test — must return undefined |
| 7 | +assert.strictEqual(getTestContext(),undefined); |
| 8 | + |
| 9 | +test('getTestContext returns current context inside test',async()=>{ |
| 10 | +constctx=getTestContext(); |
| 11 | +assert.ok(ctx!==undefined); |
| 12 | +assert.strictEqual(ctx.name,'getTestContext returns current context inside test'); |
| 13 | +assert.strictEqual(typeofctx.signal,'object'); |
| 14 | +assert.strictEqual(typeofctx.fullName,'string'); |
| 15 | +}); |
| 16 | + |
| 17 | +test('getTestContext works in nested test',async(t)=>{ |
| 18 | +awaitt.test('child',async()=>{ |
| 19 | +constctx=getTestContext(); |
| 20 | +assert.ok(ctx!==undefined); |
| 21 | +assert.strictEqual(ctx.name,'child'); |
| 22 | +}); |
| 23 | +}); |
| 24 | + |
| 25 | +describe('getTestContext works in describe/it',()=>{ |
| 26 | +it('has correct name',()=>{ |
| 27 | +constctx=getTestContext(); |
| 28 | +assert.ok(ctx!==undefined); |
| 29 | +assert.strictEqual(ctx.name,'has correct name'); |
| 30 | +}); |
| 31 | +}); |
| 32 | + |
| 33 | +describe('getTestContext returns SuiteContext in suite',()=>{ |
| 34 | +it('suite context is available',()=>{ |
| 35 | +constctx=getTestContext(); |
| 36 | +assert.ok(ctx!==undefined); |
| 37 | +// Suite name appears as parent in nested test context |
| 38 | +assert.strictEqual(typeofctx.signal,'object'); |
| 39 | +assert.strictEqual(typeofctx.fullName,'string'); |
| 40 | +}); |
| 41 | +}); |
| 42 | + |
| 43 | +test('getTestContext works in test body during async operations',async(t)=>{ |
| 44 | +constctx=getTestContext(); |
| 45 | +assert.ok(ctx!==undefined); |
| 46 | +assert.strictEqual(ctx.name,'getTestContext works in test body during async operations'); |
| 47 | + |
| 48 | +// Also works in nested async context |
| 49 | +constctxInSetImmediate=awaitnewPromise((resolve)=>{ |
| 50 | +setImmediate(()=>resolve(getTestContext())); |
| 51 | +}); |
| 52 | +assert.ok(ctxInSetImmediate!==undefined); |
| 53 | +assert.strictEqual(ctxInSetImmediate.name,'getTestContext works in test body during async operations'); |
| 54 | +}); |
0 commit comments