|
| 1 | +// Flags: --no-async-context-frame |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +require('../common'); |
| 5 | + |
| 6 | +const { |
| 7 | + AsyncLocalStorage, |
| 8 | +} = require('async_hooks'); |
| 9 | + |
| 10 | +const { |
| 11 | + strictEqual, |
| 12 | + throws, |
| 13 | +} = require('assert'); |
| 14 | + |
| 15 | +// ============================================================================ |
| 16 | +// The defaultValue option |
| 17 | +const als1 = new AsyncLocalStorage(); |
| 18 | +strictEqual(als1.getStore(), undefined, 'value should be undefined'); |
| 19 | + |
| 20 | +const als2 = new AsyncLocalStorage({ defaultValue: 'default' }); |
| 21 | +strictEqual(als2.getStore(), 'default', 'value should be "default"'); |
| 22 | + |
| 23 | +const als3 = new AsyncLocalStorage({ defaultValue: 42 }); |
| 24 | +strictEqual(als3.getStore(), 42, 'value should be 42'); |
| 25 | + |
| 26 | +const als4 = new AsyncLocalStorage({ defaultValue: null }); |
| 27 | +strictEqual(als4.getStore(), null, 'value should be null'); |
| 28 | + |
| 29 | +throws(() => new AsyncLocalStorage(null), { |
| 30 | + code: 'ERR_INVALID_ARG_TYPE', |
| 31 | +}); |
| 32 | + |
| 33 | +// ============================================================================ |
| 34 | +// The name option |
| 35 | + |
| 36 | +const als5 = new AsyncLocalStorage({ name: 'test' }); |
| 37 | +strictEqual(als5.name, 'test'); |
| 38 | + |
| 39 | +const als6 = new AsyncLocalStorage(); |
| 40 | +strictEqual(als6.name, undefined); |
| 41 | + |
| 42 | +throws(() => new AsyncLocalStorage({ name: 1 }), { |
| 43 | + code: 'ERR_INVALID_ARG_TYPE', |
| 44 | +}); |
0 commit comments