| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b58920c commit bdd02a4
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1496,12 +1496,36 @@ Emitted when a test starts. | |||
| 1496 | 1496 | added: | |
| 1497 | 1497 | - v18.0.0 | |
| 1498 | 1498 | - v16.17.0 | |
| 1499 | + changes: | ||
| 1500 | + - version: REPLACEME | ||
| 1501 | + pr-url: https://github.com/nodejs/node/pull/47586 | ||
| 1502 | + description: The `before` function was added to TestContext. | ||
| 1499 | 1503 | --> | |
| 1500 | 1504 | ||
| 1501 | 1505 | An instance of `TestContext` is passed to each test function in order to | |
| 1502 | 1506 | interact with the test runner. However, the `TestContext` constructor is not | |
| 1503 | 1507 | exposed as part of the API. | |
| 1504 | 1508 | ||
| 1509 | + ### `context.before([fn][, options])` | ||
| 1510 | + | ||
| 1511 | + <!-- YAML | ||
| 1512 | + added: REPLACEME | ||
| 1513 | + --> | ||
| 1514 | + | ||
| 1515 | + * `fn` {Function|AsyncFunction} The hook function. The first argument | ||
| 1516 | + to this function is a [`TestContext`][] object. If the hook uses callbacks, | ||
| 1517 | + the callback function is passed as the second argument. **Default:** A no-op | ||
| 1518 | + function. | ||
| 1519 | + * `options` {Object} Configuration options for the hook. The following | ||
| 1520 | + properties are supported: | ||
| 1521 | + * `signal` {AbortSignal} Allows aborting an in-progress hook. | ||
| 1522 | + * `timeout` {number} A number of milliseconds the hook will fail after. | ||
| 1523 | + If unspecified, subtests inherit this value from their parent. | ||
| 1524 | + **Default:** `Infinity`. | ||
| 1525 | + | ||
| 1526 | + This function is used to create a hook running before | ||
| 1527 | + subtest of the current test. | ||
| 1528 | + | ||
| 1505 | 1529 | ### `context.beforeEach([fn][, options])` | |
| 1506 | 1530 | ||
| 1507 | 1531 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -127,6 +127,10 @@ class TestContext { | |||
| 127 | 127 | return subtest.start(); | |
| 128 | 128 | } | |
| 129 | 129 | ||
| 130 | + before(fn, options) { | ||
| 131 | + this.#test.createHook('before', fn, options); | ||
| 132 | + } | ||
| 133 | + | ||
| 130 | 134 | after(fn, options) { | |
| 131 | 135 | this.#test.createHook('after', fn, options); | |
| 132 | 136 | } | |
@@ -414,6 +418,9 @@ class Test extends AsyncResource { | |||
| 414 | 418 | validateOneOf(name, 'hook name', kHookNames); | |
| 415 | 419 | // eslint-disable-next-line no-use-before-define | |
| 416 | 420 | const hook = new TestHook(fn, options); | |
| 421 | + if (name === 'before') { | ||
| 422 | + hook.run = runOnce(hook.run); | ||
| 423 | + } | ||
| 417 | 424 | ArrayPrototypePush(this.hooks[name], hook); | |
| 418 | 425 | return hook; | |
| 419 | 426 | } | |
@@ -525,6 +532,9 @@ class Test extends AsyncResource { | |||
| 525 | 532 | if (this.parent?.hooks.beforeEach.length > 0) { | |
| 526 | 533 | await this.parent.runHook('beforeEach', { args, ctx }); | |
| 527 | 534 | } | |
| 535 | + if (this.parent?.hooks.before.length > 0) { | ||
| 536 | + await this.parent.runHook('before', this.parent.getRunArgs()); | ||
| 537 | + } | ||
| 528 | 538 | const stopPromise = stopTest(this.timeout, this.signal); | |
| 529 | 539 | const runArgs = ArrayPrototypeSlice(args); | |
| 530 | 540 | ArrayPrototypeUnshift(runArgs, this.fn, ctx); | |
@@ -561,7 +571,7 @@ class Test extends AsyncResource { | |||
| 561 | 571 | this.pass(); | |
| 562 | 572 | } catch (err) { | |
| 563 | 573 | try { await after(); } catch { /* Ignore error. */ } | |
| 564 | - try { await afterEach(); } catch { /* test is already failing, let's the error */ } | ||
| 574 | + try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ } | ||
| 565 | 575 | if (isTestFailureError(err)) { | |
| 566 | 576 | if (err.failureType === kTestTimeoutFailure) { | |
| 567 | 577 | this.#cancel(err); | |
@@ -793,7 +803,7 @@ class Suite extends Test { | |||
| 793 | 803 | ||
| 794 | 804 | this.pass(); | |
| 795 | 805 | } catch (err) { | |
| 796 | - try { await afterEach(); } catch { /* test is already failing, let's the error */ } | ||
| 806 | + try { await afterEach(); } catch { /* test is already failing, let's ignore the error */ } | ||
| 797 | 807 | if (isTestFailureError(err)) { | |
| 798 | 808 | this.fail(err); | |
| 799 | 809 | } else { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,8 @@ const common = require('../../../common'); | |||
| 4 | 4 | const assert = require('assert'); | |
| 5 | 5 | const { test, describe, it, before, after, beforeEach, afterEach } = require('node:test'); | |
| 6 | 6 | ||
| 7 | + before((t) => t.diagnostic('before 1 called')); | ||
| 8 | + | ||
| 7 | 9 | describe('describe hooks', () => { | |
| 8 | 10 | const testArr = []; | |
| 9 | 11 | before(function() { | |
@@ -91,6 +93,7 @@ describe('afterEach throws and test fails', () => { | |||
| 91 | 93 | test('test hooks', async (t) => { | |
| 92 | 94 | const testArr = []; | |
| 93 | 95 | ||
| 96 | + t.before((t) => testArr.push('before ' + t.name)); | ||
| 94 | 97 | t.after(common.mustCall((t) => testArr.push('after ' + t.name))); | |
| 95 | 98 | t.beforeEach((t) => testArr.push('beforeEach ' + t.name)); | |
| 96 | 99 | t.afterEach((t) => testArr.push('afterEach ' + t.name)); | |
@@ -105,7 +108,7 @@ test('test hooks', async (t) => { | |||
| 105 | 108 | }); | |
| 106 | 109 | ||
| 107 | 110 | assert.deepStrictEqual(testArr, [ | |
| 108 | - 'beforeEach 1', '1', 'afterEach 1', | ||
| 111 | + 'beforeEach 1', 'before test hooks', '1', 'afterEach 1', | ||
| 109 | 112 | 'beforeEach 2', '2', 'afterEach 2', | |
| 110 | 113 | 'beforeEach nested', | |
| 111 | 114 | 'nested beforeEach nested 1', 'nested1', 'nested afterEach nested 1', | |
@@ -114,6 +117,13 @@ test('test hooks', async (t) => { | |||
| 114 | 117 | ]); | |
| 115 | 118 | }); | |
| 116 | 119 | ||
| 120 | + test('t.before throws', async (t) => { | ||
| 121 | + t.after(common.mustCall()); | ||
| 122 | + t.before(() => { throw new Error('before'); }); | ||
| 123 | + await t.test('1', () => {}); | ||
| 124 | + await t.test('2', () => {}); | ||
| 125 | + }); | ||
| 126 | + | ||
| 117 | 127 | test('t.beforeEach throws', async (t) => { | |
| 118 | 128 | t.after(common.mustCall()); | |
| 119 | 129 | t.beforeEach(() => { throw new Error('beforeEach'); }); | |
@@ -149,3 +159,5 @@ test('t.after() is called if test body throws', (t) => { | |||
| 149 | 159 | }); | |
| 150 | 160 | throw new Error('bye'); | |
| 151 | 161 | }); | |
| 162 | + | ||
| 163 | + before((t) => t.diagnostic('before 2 called')); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -67,6 +67,7 @@ not ok 2 - before throws | |||
| 67 | 67 | * | |
| 68 | 68 | * | |
| 69 | 69 | * | |
| 70 | + * | ||
| 70 | 71 | ... | |
| 71 | 72 | # Subtest: after throws | |
| 72 | 73 | # Subtest: 1 | |
@@ -307,6 +308,53 @@ ok 8 - test hooks | |||
| 307 | 308 | --- | |
| 308 | 309 | duration_ms: * | |
| 309 | 310 | ... | |
| 311 | + # Subtest: t.before throws | ||
| 312 | + # Subtest: 1 | ||
| 313 | + not ok 1 - 1 | ||
| 314 | + --- | ||
| 315 | + duration_ms: * | ||
| 316 | + failureType: 'hookFailed' | ||
| 317 | + error: 'failed running before hook' | ||
| 318 | + code: 'ERR_TEST_FAILURE' | ||
| 319 | + stack: |- | ||
| 320 | + * | ||
| 321 | + * | ||
| 322 | + * | ||
| 323 | + * | ||
| 324 | + * | ||
| 325 | + * | ||
| 326 | + * | ||
| 327 | + * | ||
| 328 | + * | ||
| 329 | + * | ||
| 330 | + ... | ||
| 331 | + # Subtest: 2 | ||
| 332 | + not ok 2 - 2 | ||
| 333 | + --- | ||
| 334 | + duration_ms: * | ||
| 335 | + failureType: 'hookFailed' | ||
| 336 | + error: 'failed running before hook' | ||
| 337 | + code: 'ERR_TEST_FAILURE' | ||
| 338 | + stack: |- | ||
| 339 | + * | ||
| 340 | + * | ||
| 341 | + * | ||
| 342 | + * | ||
| 343 | + * | ||
| 344 | + * | ||
| 345 | + * | ||
| 346 | + * | ||
| 347 | + * | ||
| 348 | + * | ||
| 349 | + ... | ||
| 350 | + 1..2 | ||
| 351 | + not ok 9 - t.before throws | ||
| 352 | + --- | ||
| 353 | + duration_ms: * | ||
| 354 | + failureType: 'subtestsFailed' | ||
| 355 | + error: '2 subtests failed' | ||
| 356 | + code: 'ERR_TEST_FAILURE' | ||
| 357 | + ... | ||
| 310 | 358 | # Subtest: t.beforeEach throws | |
| 311 | 359 | # Subtest: 1 | |
| 312 | 360 | not ok 1 - 1 | |
@@ -347,7 +395,7 @@ ok 8 - test hooks | |||
| 347 | 395 | * | |
| 348 | 396 | ... | |
| 349 | 397 | 1..2 | |
| 350 | - not ok 9 - t.beforeEach throws | ||
| 398 | + not ok 10 - t.beforeEach throws | ||
| 351 | 399 | --- | |
| 352 | 400 | duration_ms: * | |
| 353 | 401 | failureType: 'subtestsFailed' | |
@@ -394,7 +442,7 @@ not ok 9 - t.beforeEach throws | |||
| 394 | 442 | * | |
| 395 | 443 | ... | |
| 396 | 444 | 1..2 | |
| 397 | - not ok 10 - t.afterEach throws | ||
| 445 | + not ok 11 - t.afterEach throws | ||
| 398 | 446 | --- | |
| 399 | 447 | duration_ms: * | |
| 400 | 448 | failureType: 'subtestsFailed' | |
@@ -419,15 +467,14 @@ not ok 10 - t.afterEach throws | |||
| 419 | 467 | * | |
| 420 | 468 | * | |
| 421 | 469 | * | |
| 422 | - * | ||
| 423 | 470 | ... | |
| 424 | 471 | # Subtest: 2 | |
| 425 | 472 | ok 2 - 2 | |
| 426 | 473 | --- | |
| 427 | 474 | duration_ms: * | |
| 428 | 475 | ... | |
| 429 | 476 | 1..2 | |
| 430 | - not ok 11 - afterEach when test fails | ||
| 477 | + not ok 12 - afterEach when test fails | ||
| 431 | 478 | --- | |
| 432 | 479 | duration_ms: * | |
| 433 | 480 | failureType: 'subtestsFailed' | |
@@ -452,7 +499,6 @@ not ok 11 - afterEach when test fails | |||
| 452 | 499 | * | |
| 453 | 500 | * | |
| 454 | 501 | * | |
| 455 | - * | ||
| 456 | 502 | ... | |
| 457 | 503 | # Subtest: 2 | |
| 458 | 504 | not ok 2 - 2 | |
@@ -474,15 +520,15 @@ not ok 11 - afterEach when test fails | |||
| 474 | 520 | * | |
| 475 | 521 | ... | |
| 476 | 522 | 1..2 | |
| 477 | - not ok 12 - afterEach throws and test fails | ||
| 523 | + not ok 13 - afterEach throws and test fails | ||
| 478 | 524 | --- | |
| 479 | 525 | duration_ms: * | |
| 480 | 526 | failureType: 'subtestsFailed' | |
| 481 | 527 | error: '2 subtests failed' | |
| 482 | 528 | code: 'ERR_TEST_FAILURE' | |
| 483 | 529 | ... | |
| 484 | 530 | # Subtest: t.after() is called if test body throws | |
| 485 | - not ok 13 - t.after() is called if test body throws | ||
| 531 | + not ok 14 - t.after() is called if test body throws | ||
| 486 | 532 | --- | |
| 487 | 533 | duration_ms: * | |
| 488 | 534 | failureType: 'testCodeFailure' | |
@@ -493,16 +539,15 @@ not ok 13 - t.after() is called if test body throws | |||
| 493 | 539 | * | |
| 494 | 540 | * | |
| 495 | 541 | * | |
| 496 | - * | ||
| 497 | - * | ||
| 498 | - * | ||
| 499 | 542 | ... | |
| 500 | 543 | # - after() called | |
| 501 | - 1..13 | ||
| 502 | - # tests 35 | ||
| 544 | + 1..14 | ||
| 545 | + # before 1 called | ||
| 546 | + # before 2 called | ||
| 547 | + # tests 38 | ||
| 503 | 548 | # suites 8 | |
| 504 | 549 | # pass 14 | |
| 505 | - # fail 19 | ||
| 550 | + # fail 22 | ||
| 506 | 551 | # cancelled 2 | |
| 507 | 552 | # skipped 0 | |
| 508 | 553 | # todo 0 | |
| Back | FazBrowse Home | New Git URL |
0 commit comments