|
2 | 2 |
|
3 | 3 | const process = require('process') |
4 | 4 |
|
| 5 | +const promisify = require('util.promisify') |
5 | 6 | const test = require('ava') |
6 | 7 | const sinon = require('sinon') |
7 | 8 | const lolex = require('lolex') |
8 | 9 |
|
| 10 | +const pNextTick = promisify(process.nextTick) |
| 11 | + |
9 | 12 | // Required directly inside `src` because this is exposed through |
10 | 13 | // documentation, but not through code |
11 | 14 | // eslint-disable-next-line import/no-internal-modules |
@@ -79,5 +82,48 @@ repeatEvents((prefix, { eventName, emitEvent }) => { |
79 | 82 |
|
80 | 83 | unstubProcessExit({ clock, processExit }) |
81 | 84 | }) |
| 85 | + |
| 86 | + test(`${prefix} should delay process.exit(1) with async opts.log()`, async t => { |
| 87 | + const { clock, processExit } = stubProcessExit() |
| 88 | + |
| 89 | + const { promise, resolve } = getPromise() |
| 90 | + |
| 91 | + const { stopLogging } = startLogging({ |
| 92 | + exitOn: [eventName], |
| 93 | + eventName, |
| 94 | + log: () => promise, |
| 95 | + }) |
| 96 | + |
| 97 | + await emitEventAndWait(EXIT_TIMEOUT, { clock, emitEvent }) |
| 98 | + |
| 99 | + t.true(processExit.notCalled) |
| 100 | + |
| 101 | + await resolve() |
| 102 | + clock.tick(EXIT_TIMEOUT) |
| 103 | + |
| 104 | + t.true(processExit.called) |
| 105 | + |
| 106 | + stopLogging() |
| 107 | + |
| 108 | + unstubProcessExit({ clock, processExit }) |
| 109 | + }) |
82 | 110 | }) |
| 111 | + |
| 112 | +// Returns a promise that can be triggered from outside |
| 113 | +const getPromise = function() { |
| 114 | + // eslint-disable-next-line fp/no-let, init-declarations |
| 115 | + let resolveA |
| 116 | + // eslint-disable-next-line promise/avoid-new |
| 117 | + const promise = new Promise(resolve => { |
| 118 | + // eslint-disable-next-line fp/no-mutation |
| 119 | + resolveA = getResolve.bind(null, resolve) |
| 120 | + }) |
| 121 | + return { promise, resolve: resolveA } |
| 122 | +} |
| 123 | + |
| 124 | +const getResolve = async function(resolve) { |
| 125 | + resolve() |
| 126 | + await pNextTick() |
| 127 | +} |
| 128 | + |
83 | 129 | /* eslint-enable max-nested-callbacks */ |
0 commit comments