1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const { spawnSync } = require('child_process');
5const { setTimeout } = require('timers/promises');
6
7if (process.argv[2] === 'child') {
8  const test = require('node:test');
9
10  if (process.argv[3] === 'abortSignal') {
11    assert.throws(() => test({ signal: {} }), {
12      code: 'ERR_INVALID_ARG_TYPE',
13      name: 'TypeError'
14    });
15
16    let testSignal;
17    test({ timeout: 10 }, common.mustCall(async ({ signal }) => {
18      assert.strictEqual(signal.aborted, false);
19      testSignal = signal;
20      await setTimeout(50);
21    })).finally(common.mustCall(() => {
22      test(() => assert.strictEqual(testSignal.aborted, true));
23    }));
24
25    // TODO(benjamingr) add more tests to describe + AbortSignal
26    // this just tests the parameter is passed
27    test.describe('Abort Signal in describe', common.mustCall(({ signal }) => {
28      test.it('Supports AbortSignal', () => {
29        assert.strictEqual(signal.aborted, false);
30      });
31    }));
32  } else assert.fail('unreachable');
33} else {
34  const child = spawnSync(process.execPath, [__filename, 'child', 'abortSignal']);
35  const stdout = child.stdout.toString();
36  assert.match(stdout, /^# pass 2$/m);
37  assert.match(stdout, /^# fail 0$/m);
38  assert.match(stdout, /^# cancelled 1$/m);
39  assert.strictEqual(child.status, 1);
40  assert.strictEqual(child.signal, null);
41}
42