|
2 | 2 | const common = require('../../../common');
|
3 | 3 | const assert = require('assert');
|
4 | 4 | const { test, describe, it, before, after, beforeEach, afterEach } = require('node:test');
|
| 5 | +const { setTimeout } = require('node:timers/promises'); |
5 | 6 |
|
6 | 7 | before((t) => t.diagnostic('before 1 called'));
|
7 | 8 | after((t) => t.diagnostic('after 1 called'));
|
@@ -148,7 +149,6 @@ test('test hooks', async (t) => {
|
148 | 149 | }));
|
149 | 150 | });
|
150 | 151 |
|
151 |
| - |
152 | 152 | test('test hooks - no subtests', async (t) => {
|
153 | 153 | const testArr = [];
|
154 | 154 |
|
@@ -253,5 +253,54 @@ describe('run after when before throws', () => {
|
253 | 253 | it('1', () => {});
|
254 | 254 | });
|
255 | 255 |
|
| 256 | + |
| 257 | +test('test hooks - async', async (t) => { |
| 258 | + const testArr = []; |
| 259 | + |
| 260 | + t.before(async (t) => { |
| 261 | + testArr.push('before starting ' + t.name); |
| 262 | + await setTimeout(10); |
| 263 | + testArr.push('before ending ' + t.name); |
| 264 | + }); |
| 265 | + t.after(async (t) => { |
| 266 | + testArr.push('after starting ' + t.name); |
| 267 | + await setTimeout(10); |
| 268 | + testArr.push('after ending ' + t.name); |
| 269 | + }); |
| 270 | + t.beforeEach(async (t) => { |
| 271 | + testArr.push('beforeEach starting ' + t.name); |
| 272 | + await setTimeout(10); |
| 273 | + testArr.push('beforeEach ending ' + t.name); |
| 274 | + }); |
| 275 | + t.afterEach(async (t) => { |
| 276 | + testArr.push('afterEach starting ' + t.name); |
| 277 | + await setTimeout(10); |
| 278 | + testArr.push('afterEach ending ' + t.name); |
| 279 | + }); |
| 280 | + await t.test('1', async () => { |
| 281 | + testArr.push('1 starting'); |
| 282 | + await setTimeout(10); |
| 283 | + testArr.push('1 ending'); |
| 284 | + }); |
| 285 | + await t.test('2', async () => { |
| 286 | + testArr.push('2 starting'); |
| 287 | + await setTimeout(10); |
| 288 | + testArr.push('2 ending'); |
| 289 | + }); |
| 290 | + |
| 291 | + t.after(common.mustCall(() => { |
| 292 | + assert.deepStrictEqual(testArr, [ |
| 293 | + 'before starting test hooks - async', 'before ending test hooks - async', |
| 294 | + 'beforeEach starting 1', 'beforeEach ending 1', |
| 295 | + '1 starting', '1 ending', |
| 296 | + 'afterEach starting 1', 'afterEach ending 1', |
| 297 | + 'beforeEach starting 2', 'beforeEach ending 2', |
| 298 | + '2 starting', '2 ending', |
| 299 | + 'afterEach starting 2', 'afterEach ending 2', |
| 300 | + 'after starting test hooks - async', 'after ending test hooks - async', |
| 301 | + ]); |
| 302 | + })); |
| 303 | +}); |
| 304 | + |
256 | 305 | before((t) => t.diagnostic('before 2 called'));
|
257 | 306 | after((t) => t.diagnostic('after 2 called'));
|
0 commit comments