-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-runner-get-test-context.js
More file actions
54 lines (47 loc) · 1.88 KB
/
test-runner-get-test-context.js
File metadata and controls
54 lines (47 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
require('../common');
const assert = require('node:assert');
const { test, getTestContext, describe, it } = require('node:test');
// Outside a test — must return undefined
assert.strictEqual(getTestContext(), undefined);
test('getTestContext returns current context inside test', async () => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
assert.strictEqual(ctx.name, 'getTestContext returns current context inside test');
assert.strictEqual(typeof ctx.signal, 'object');
assert.strictEqual(typeof ctx.fullName, 'string');
});
test('getTestContext works in nested test', async (t) => {
await t.test('child', async () => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
assert.strictEqual(ctx.name, 'child');
});
});
describe('getTestContext works in describe/it', () => {
it('has correct name', () => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
assert.strictEqual(ctx.name, 'has correct name');
});
});
describe('getTestContext returns SuiteContext in suite', () => {
it('suite context is available', () => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
// Suite name appears as parent in nested test context
assert.strictEqual(typeof ctx.signal, 'object');
assert.strictEqual(typeof ctx.fullName, 'string');
});
});
test('getTestContext works in test body during async operations', async (t) => {
const ctx = getTestContext();
assert.ok(ctx !== undefined);
assert.strictEqual(ctx.name, 'getTestContext works in test body during async operations');
// Also works in nested async context
const ctxInSetImmediate = await new Promise((resolve) => {
setImmediate(() => resolve(getTestContext()));
});
assert.ok(ctxInSetImmediate !== undefined);
assert.strictEqual(ctxInSetImmediate.name, 'getTestContext works in test body during async operations');
});