|
1 | 1 | 'use strict'; |
2 | | -require('../common'); |
| 2 | +const common = require('../common'); |
3 | 3 | const assert = require('node:assert'); |
4 | | -const { test, getTestContext, describe, it } = require('node:test'); |
| 4 | +const { |
| 5 | + test, |
| 6 | + getTestContext, |
| 7 | + describe, |
| 8 | + it, |
| 9 | + before, |
| 10 | + after, |
| 11 | + beforeEach, |
| 12 | + afterEach, |
| 13 | +} = require('node:test'); |
5 | 14 |
|
6 | 15 | // Outside a test — must return undefined |
7 | 16 | assert.strictEqual(getTestContext(), undefined); |
@@ -40,6 +49,63 @@ describe('getTestContext returns SuiteContext in suite', () => { |
40 | 49 | }); |
41 | 50 | }); |
42 | 51 |
|
| 52 | +describe('getTestContext inside hooks', () => { |
| 53 | + const suiteName = 'getTestContext inside hooks'; |
| 54 | + |
| 55 | + before(common.mustCall((t) => { |
| 56 | + const ctx = getTestContext(); |
| 57 | + assert.ok(ctx !== undefined); |
| 58 | + assert.strictEqual(ctx.name, suiteName); |
| 59 | + assert.strictEqual(ctx.name, t.name); |
| 60 | + })); |
| 61 | + |
| 62 | + beforeEach(common.mustCall(() => { |
| 63 | + const ctx = getTestContext(); |
| 64 | + assert.ok(ctx !== undefined); |
| 65 | + assert.strictEqual(ctx.name, suiteName); |
| 66 | + })); |
| 67 | + |
| 68 | + afterEach(common.mustCall(() => { |
| 69 | + const ctx = getTestContext(); |
| 70 | + assert.ok(ctx !== undefined); |
| 71 | + assert.strictEqual(ctx.name, suiteName); |
| 72 | + })); |
| 73 | + |
| 74 | + after(common.mustCall((t) => { |
| 75 | + const ctx = getTestContext(); |
| 76 | + assert.ok(ctx !== undefined); |
| 77 | + assert.strictEqual(ctx.name, suiteName); |
| 78 | + assert.strictEqual(ctx.name, t.name); |
| 79 | + })); |
| 80 | + |
| 81 | + it('runs inside the suite', () => { |
| 82 | + const ctx = getTestContext(); |
| 83 | + assert.ok(ctx !== undefined); |
| 84 | + assert.strictEqual(ctx.name, 'runs inside the suite'); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +test('getTestContext inside test-level hooks returns the parent test', async (t) => { |
| 89 | + const parentName = t.name; |
| 90 | + t.beforeEach(common.mustCall(() => { |
| 91 | + const ctx = getTestContext(); |
| 92 | + assert.ok(ctx !== undefined); |
| 93 | + assert.strictEqual(ctx.name, parentName); |
| 94 | + })); |
| 95 | + |
| 96 | + t.afterEach(common.mustCall(() => { |
| 97 | + const ctx = getTestContext(); |
| 98 | + assert.ok(ctx !== undefined); |
| 99 | + assert.strictEqual(ctx.name, parentName); |
| 100 | + })); |
| 101 | + |
| 102 | + await t.test('child', () => { |
| 103 | + const ctx = getTestContext(); |
| 104 | + assert.ok(ctx !== undefined); |
| 105 | + assert.strictEqual(ctx.name, 'child'); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
43 | 109 | test('getTestContext works in test body during async operations', async (t) => { |
44 | 110 | const ctx = getTestContext(); |
45 | 111 | assert.ok(ctx !== undefined); |
|
0 commit comments