@@ -25,14 +25,22 @@ export async function collectSuites() {
25
25
}
26
26
27
27
export function registerSuites ( bench : Bench ) {
28
- for ( const { tasks, hooks } of Object . values ( suites ) ) {
29
- for ( const t of tasks ) {
30
- /**
31
- * `beforeAll` in tinybench is called once before all iterations of a single operation.
32
- * This is functionally equivalent to `beforeEach` in jest and vitest.
33
- */
34
- const options = hooks . beforeEach ? { beforeAll : hooks . beforeEach } : { } ;
28
+ for ( const { hooks, tasks } of Object . values ( suites ) ) {
29
+ /**
30
+ * In tinybench, `beforeAll` and `afterAll` refer to all iterations of
31
+ * a single task, while `beforeEach` and `afterEach` refer to each iteration.
32
+ *
33
+ * In jest and vitest, `beforeAll` and `afterAll` refer to all tests in a suite,
34
+ * while `beforeEach` and `afterEach` refer to each individual test.
35
+ *
36
+ * The API renames tinybench's hooks to prevent confusion from this difference.
37
+ */
38
+ const options : Record < string , Callback > = { } ;
39
+
40
+ if ( hooks . beforeEach ) options . beforeAll = hooks . beforeEach ;
41
+ if ( hooks . afterEach ) options . afterAll = hooks . afterEach ;
35
42
43
+ for ( const t of tasks ) {
36
44
bench . add ( t . description , t . operation , options ) ;
37
45
}
38
46
}
@@ -70,3 +78,14 @@ export function beforeEach(fn: Callback) {
70
78
suites [ filePath ] ||= { hooks : { } , tasks : [ ] } ;
71
79
suites [ filePath ] . hooks . beforeEach = fn ;
72
80
}
81
+
82
+ export function afterEach ( fn : Callback ) {
83
+ const filePath = suiteFilePath ( ) ;
84
+
85
+ if ( suites [ filePath ] ?. hooks . afterEach ) {
86
+ throw new DuplicateHookError ( 'afterEach' , filePath ) ;
87
+ }
88
+
89
+ suites [ filePath ] ||= { hooks : { } , tasks : [ ] } ;
90
+ suites [ filePath ] . hooks . afterEach = fn ;
91
+ }
0 commit comments