|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const assert = 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 | + const ctx = getTestContext(); |
| 11 | + assert.ok(ctx !== undefined); |
| 12 | + assert.strictEqual(ctx.name, 'getTestContext returns current context inside test'); |
| 13 | + assert.strictEqual(typeof ctx.signal, 'object'); |
| 14 | + assert.strictEqual(typeof ctx.fullName, 'string'); |
| 15 | +}); |
| 16 | + |
| 17 | +test('getTestContext works in nested test', async (t) => { |
| 18 | + await t.test('child', async () => { |
| 19 | + const ctx = 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 | + const ctx = 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 | + const ctx = getTestContext(); |
| 36 | + assert.ok(ctx !== undefined); |
| 37 | + // Suite name appears as parent in nested test context |
| 38 | + assert.strictEqual(typeof ctx.signal, 'object'); |
| 39 | + assert.strictEqual(typeof ctx.fullName, 'string'); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +test('getTestContext works in test body during async operations', async (t) => { |
| 44 | + const ctx = 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 | + const ctxInSetImmediate = await new Promise((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