| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6a4c4b7 commit 4f1426d
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,7 +21,7 @@ const { | |||
| 21 | 21 | }, | |
| 22 | 22 | } = require('internal/errors'); | |
| 23 | 23 | const { exitCodes: { kGenericUserError } } = internalBinding('errors'); | |
| 24 | - const { kCancelledByParent, Test, Suite, TestContext, SuiteContext } = require('internal/test_runner/test'); | ||
| 24 | + const { kCancelledByParent, Test, Suite } = require('internal/test_runner/test'); | ||
| 25 | 25 | const { | |
| 26 | 26 | parseCommandLine, | |
| 27 | 27 | reporterScope, | |
@@ -439,16 +439,7 @@ function getTestContext() { | |||
| 439 | 439 | if (test === undefined || test === reporterScope) { | |
| 440 | 440 | return undefined; | |
| 441 | 441 | } | |
| 442 | - // For hooks (hookType is set), return the test/suite being hooked (the parent) | ||
| 443 | - const actualTest = test.hookType !== undefined ? test.parent : test; | ||
| 444 | - if (actualTest === undefined) { | ||
| 445 | - return undefined; | ||
| 446 | - } | ||
| 447 | - // Return SuiteContext for suites, TestContext for tests | ||
| 448 | - if (actualTest instanceof Suite) { | ||
| 449 | - return new SuiteContext(actualTest); | ||
| 450 | - } | ||
| 451 | - return new TestContext(actualTest); | ||
| 442 | + return test.getCtx(); | ||
| 452 | 443 | } | |
| 453 | 444 | ||
| 454 | 445 | module.exports = { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1233,8 +1233,14 @@ class Test extends AsyncResource { | |||
| 1233 | 1233 | } | |
| 1234 | 1234 | } | |
| 1235 | 1235 | ||
| 1236 | + #ctx; | ||
| 1237 | + getCtx() { | ||
| 1238 | + this.#ctx ??= new TestContext(this); | ||
| 1239 | + return this.#ctx; | ||
| 1240 | + } | ||
| 1241 | + | ||
| 1236 | 1242 | getRunArgs() { | |
| 1237 | - const ctx = new TestContext(this); | ||
| 1243 | + const ctx = this.getCtx(); | ||
| 1238 | 1244 | return { __proto__: null, ctx, args: [ctx] }; | |
| 1239 | 1245 | } | |
| 1240 | 1246 | ||
@@ -1703,6 +1709,11 @@ class TestHook extends Test { | |||
| 1703 | 1709 | this.#args = args; | |
| 1704 | 1710 | return super.run(); | |
| 1705 | 1711 | } | |
| 1712 | + | ||
| 1713 | + getCtx() { | ||
| 1714 | + return this.parentTest.getCtx(); | ||
| 1715 | + } | ||
| 1716 | + | ||
| 1706 | 1717 | getRunArgs() { | |
| 1707 | 1718 | return this.#args; | |
| 1708 | 1719 | } | |
@@ -1794,6 +1805,12 @@ class Suite extends Test { | |||
| 1794 | 1805 | this.buildPhaseFinished = true; | |
| 1795 | 1806 | } | |
| 1796 | 1807 | ||
| 1808 | + #ctx; | ||
| 1809 | + getCtx() { | ||
| 1810 | + this.#ctx ??= new TestContext(this); | ||
| 1811 | + return this.#ctx; | ||
| 1812 | + } | ||
| 1813 | + | ||
| 1797 | 1814 | getRunArgs() { | |
| 1798 | 1815 | const ctx = new SuiteContext(this); | |
| 1799 | 1816 | return { __proto__: null, ctx, args: [ctx] }; | |
@@ -1866,6 +1883,4 @@ module.exports = { | |||
| 1866 | 1883 | kUnwrapErrors, | |
| 1867 | 1884 | Suite, | |
| 1868 | 1885 | Test, | |
| 1869 | - TestContext, | ||
| 1870 | - SuiteContext, | ||
| 1871 | 1886 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,16 @@ | |||
| 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); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -63,7 +63,9 @@ function isMustCallOrMustCallAtLeast(str) { | |||
| 63 | 63 | } | |
| 64 | 64 | ||
| 65 | 65 | function isMustCallOrTest(str) { | |
| 66 | - return str === 'test' || str === 'it' || isMustCallOrMustCallAtLeast(str); | ||
| 66 | + return str === 'test' || str === 'it' || str === 'describe' || str === 'suite' || | ||
| 67 | + str === 'before' || str === 'after' || str === 'beforeEach' || str === 'afterEach' || | ||
| 68 | + isMustCallOrMustCallAtLeast(str); | ||
| 67 | 69 | } | |
| 68 | 70 | ||
| 69 | 71 | module.exports = { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments