@@ -440,12 +440,166 @@ same as [`it([name], { skip: true }[, fn])`][it options].
440440Shorthand for marking a test as ` TODO ` ,
441441same as [ ` it([name], { todo: true }[, fn]) ` ] [ it options ] .
442442
443+ ### ` before([, fn][, options]) `
444+
445+ * ` fn ` {Function|AsyncFunction} The hook function.
446+ If the hook uses callbacks,
447+ the callback function is passed as the second argument. ** Default:** A no-op
448+ function.
449+ * ` options ` {Object} Configuration options for the hook. The following
450+ properties are supported:
451+ * ` signal ` {AbortSignal} Allows aborting an in-progress hook
452+ * ` timeout ` {number} A number of milliseconds the hook will fail after.
453+ If unspecified, subtests inherit this value from their parent.
454+ ** Default:** ` Infinity ` .
455+
456+ This function is used to create a hook running before running a suite.
457+
458+ ``` js
459+ describe (' tests' , async () => {
460+ before (() => console .log (' about to run some test' ));
461+ it (' is a subtest' , () => {
462+ assert .ok (' some relevant assertion here' );
463+ });
464+ });
465+ ```
466+
467+ ### ` after([, fn][, options]) `
468+
469+ * ` fn ` {Function|AsyncFunction} The hook function.
470+ If the hook uses callbacks,
471+ the callback function is passed as the second argument. ** Default:** A no-op
472+ function.
473+ * ` options ` {Object} Configuration options for the hook. The following
474+ properties are supported:
475+ * ` signal ` {AbortSignal} Allows aborting an in-progress hook
476+ * ` timeout ` {number} A number of milliseconds the hook will fail after.
477+ If unspecified, subtests inherit this value from their parent.
478+ ** Default:** ` Infinity ` .
479+
480+ This function is used to create a hook running after running a suite.
481+
482+ ``` js
483+ describe (' tests' , async () => {
484+ after (() => console .log (' finished running tests' ));
485+ it (' is a subtest' , () => {
486+ assert .ok (' some relevant assertion here' );
487+ });
488+ });
489+ ```
490+
491+ ### ` beforeEach([, fn][, options]) `
492+
493+ * ` fn ` {Function|AsyncFunction} The hook function.
494+ If the hook uses callbacks,
495+ the callback function is passed as the second argument. ** Default:** A no-op
496+ function.
497+ * ` options ` {Object} Configuration options for the hook. The following
498+ properties are supported:
499+ * ` signal ` {AbortSignal} Allows aborting an in-progress hook
500+ * ` timeout ` {number} A number of milliseconds the hook will fail after.
501+ If unspecified, subtests inherit this value from their parent.
502+ ** Default:** ` Infinity ` .
503+
504+ This function is used to create a hook running
505+ before each subtest of the current suite.
506+
507+ ``` js
508+ describe (' tests' , async () => {
509+ beforeEach (() => t .diagnostics (' about to run a test' ));
510+ it (' is a subtest' , () => {
511+ assert .ok (' some relevant assertion here' );
512+ });
513+ });
514+ ```
515+
516+ ### ` afterEach([, fn][, options]) `
517+
518+ * ` fn ` {Function|AsyncFunction} The hook function.
519+ If the hook uses callbacks,
520+ the callback function is passed as the second argument. ** Default:** A no-op
521+ function.
522+ * ` options ` {Object} Configuration options for the hook. The following
523+ properties are supported:
524+ * ` signal ` {AbortSignal} Allows aborting an in-progress hook
525+ * ` timeout ` {number} A number of milliseconds the hook will fail after.
526+ If unspecified, subtests inherit this value from their parent.
527+ ** Default:** ` Infinity ` .
528+
529+ This function is used to create a hook running
530+ after each subtest of the current test.
531+
532+ ``` js
533+ describe (' tests' , async () => {
534+ afterEach (() => t .diagnostics (' about to run a test' ));
535+ it (' is a subtest' , () => {
536+ assert .ok (' some relevant assertion here' );
537+ });
538+ });
539+ ```
540+
443541## Class: ` TestContext `
444542
445543An instance of ` TestContext ` is passed to each test function in order to
446544interact with the test runner. However, the ` TestContext ` constructor is not
447545exposed as part of the API.
448546
547+ ### ` context.beforeEach([, fn][, options]) `
548+
549+ * ` fn ` {Function|AsyncFunction} The hook function. The first argument
550+ to this function is a [ ` TestContext ` ] [ ] object. If the hook uses callbacks,
551+ the callback function is passed as the second argument. ** Default:** A no-op
552+ function.
553+ * ` options ` {Object} Configuration options for the hook. The following
554+ properties are supported:
555+ * ` signal ` {AbortSignal} Allows aborting an in-progress hook
556+ * ` timeout ` {number} A number of milliseconds the hook will fail after.
557+ If unspecified, subtests inherit this value from their parent.
558+ ** Default:** ` Infinity ` .
559+
560+ This function is used to create a hook running
561+ before each subtest of the current test.
562+
563+ ``` js
564+ test (' top level test' , async (t ) => {
565+ t .beforeEach ((t ) => t .diagnostics (` about to run ${ t .name } ` ));
566+ await t .test (
567+ ' This is a subtest' ,
568+ (t ) => {
569+ assert .ok (' some relevant assertion here' );
570+ }
571+ );
572+ });
573+ ```
574+
575+ ### ` context.afterEach([, fn][, options]) `
576+
577+ * ` fn ` {Function|AsyncFunction} The hook function. The first argument
578+ to this function is a [ ` TestContext ` ] [ ] object. If the hook uses callbacks,
579+ the callback function is passed as the second argument. ** Default:** A no-op
580+ function.
581+ * ` options ` {Object} Configuration options for the hook. The following
582+ properties are supported:
583+ * ` signal ` {AbortSignal} Allows aborting an in-progress hook
584+ * ` timeout ` {number} A number of milliseconds the hook will fail after.
585+ If unspecified, subtests inherit this value from their parent.
586+ ** Default:** ` Infinity ` .
587+
588+ This function is used to create a hook running
589+ after each subtest of the current test.
590+
591+ ``` js
592+ test (' top level test' , async (t ) => {
593+ t .afterEach ((t ) => t .diagnostics (` finished running ${ t .name } ` ));
594+ await t .test (
595+ ' This is a subtest' ,
596+ (t ) => {
597+ assert .ok (' some relevant assertion here' );
598+ }
599+ );
600+ });
601+ ```
602+
449603### ` context.diagnostic(message) `
450604
451605- ` message ` {string} Message to be displayed as a TAP diagnostic.
@@ -454,6 +608,10 @@ This function is used to write TAP diagnostics to the output. Any diagnostic
454608information is included at the end of the test's results. This function does
455609not return a value.
456610
611+ ` context.name `
612+
613+ The name of the test
614+
457615### ` context.runOnly(shouldRunOnlyTests) `
458616
459617- ` shouldRunOnlyTests ` {boolean} Whether or not to run ` only ` tests.
@@ -528,6 +686,10 @@ An instance of `SuiteContext` is passed to each suite function in order to
528686interact with the test runner. However, the ` SuiteContext ` constructor is not
529687exposed as part of the API.
530688
689+ ### ` context.name `
690+
691+ The name of the suite
692+
531693### ` context.signal `
532694
533695* [ ` AbortSignal ` ] [ ] Can be used to abort test subtasks when the test has been aborted.
0 commit comments