• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const assert = require('assert');
4
5function getTestCases(isWorker = false) {
6  const cases = [];
7  function exitsOnExitCodeSet() {
8    process.exitCode = 42;
9    process.on('exit', (code) => {
10      assert.strictEqual(process.exitCode, 42);
11      assert.strictEqual(code, 42);
12    });
13  }
14  cases.push({ func: exitsOnExitCodeSet, result: 42 });
15
16  function changesCodeViaExit() {
17    process.exitCode = 99;
18    process.on('exit', (code) => {
19      assert.strictEqual(process.exitCode, 42);
20      assert.strictEqual(code, 42);
21    });
22    process.exit(42);
23  }
24  cases.push({ func: changesCodeViaExit, result: 42 });
25
26  function changesCodeZeroExit() {
27    process.exitCode = 99;
28    process.on('exit', (code) => {
29      assert.strictEqual(process.exitCode, 0);
30      assert.strictEqual(code, 0);
31    });
32    process.exit(0);
33  }
34  cases.push({ func: changesCodeZeroExit, result: 0 });
35
36  function exitWithOneOnUncaught() {
37    process.exitCode = 99;
38    process.on('exit', (code) => {
39      // cannot use assert because it will be uncaughtException -> 1 exit code
40      // that will render this test useless
41      if (code !== 1 || process.exitCode !== 1) {
42        console.log('wrong code! expected 1 for uncaughtException');
43        process.exit(99);
44      }
45    });
46    throw new Error('ok');
47  }
48  cases.push({
49    func: exitWithOneOnUncaught,
50    result: 1,
51    error: /^Error: ok$/,
52  });
53
54  function changeCodeInsideExit() {
55    process.exitCode = 95;
56    process.on('exit', (code) => {
57      assert.strictEqual(process.exitCode, 95);
58      assert.strictEqual(code, 95);
59      process.exitCode = 99;
60    });
61  }
62  cases.push({ func: changeCodeInsideExit, result: 99 });
63
64  function zeroExitWithUncaughtHandler() {
65    process.on('exit', (code) => {
66      assert.strictEqual(process.exitCode, 0);
67      assert.strictEqual(code, 0);
68    });
69    process.on('uncaughtException', () => { });
70    throw new Error('ok');
71  }
72  cases.push({ func: zeroExitWithUncaughtHandler, result: 0 });
73
74  function changeCodeInUncaughtHandler() {
75    process.on('exit', (code) => {
76      assert.strictEqual(process.exitCode, 97);
77      assert.strictEqual(code, 97);
78    });
79    process.on('uncaughtException', () => {
80      process.exitCode = 97;
81    });
82    throw new Error('ok');
83  }
84  cases.push({ func: changeCodeInUncaughtHandler, result: 97 });
85
86  function changeCodeInExitWithUncaught() {
87    process.on('exit', (code) => {
88      assert.strictEqual(process.exitCode, 1);
89      assert.strictEqual(code, 1);
90      process.exitCode = 98;
91    });
92    throw new Error('ok');
93  }
94  cases.push({
95    func: changeCodeInExitWithUncaught,
96    result: 98,
97    error: /^Error: ok$/,
98  });
99
100  function exitWithZeroInExitWithUncaught() {
101    process.on('exit', (code) => {
102      assert.strictEqual(process.exitCode, 1);
103      assert.strictEqual(code, 1);
104      process.exitCode = 0;
105    });
106    throw new Error('ok');
107  }
108  cases.push({
109    func: exitWithZeroInExitWithUncaught,
110    result: 0,
111    error: /^Error: ok$/,
112  });
113
114  function exitWithThrowInUncaughtHandler() {
115    process.on('uncaughtException', () => {
116      throw new Error('ok')
117    });
118    throw new Error('bad');
119  }
120  cases.push({
121    func: exitWithThrowInUncaughtHandler,
122    result: isWorker ? 1 : 7,
123    error: /^Error: ok$/,
124  });
125
126  function exitWithUndefinedFatalException() {
127    process._fatalException = undefined;
128    throw new Error('ok');
129  }
130  cases.push({
131    func: exitWithUndefinedFatalException,
132    result: 6,
133  });
134  return cases;
135}
136exports.getTestCases = getTestCases;
137