2022-07-29 08:42:55 +00:00
|
|
|
import { spawnPromisified } from '../common/index.mjs';
|
2020-08-06 00:56:17 +00:00
|
|
|
import fixtures from '../common/fixtures.js';
|
2022-07-29 08:42:55 +00:00
|
|
|
import assert from 'node:assert';
|
|
|
|
import { execPath } from 'node:process';
|
|
|
|
import { describe, it } from 'node:test';
|
|
|
|
|
2020-08-06 00:56:17 +00:00
|
|
|
|
2022-05-04 15:51:12 +00:00
|
|
|
const commonArgs = [
|
|
|
|
'--input-type=module',
|
|
|
|
'--eval',
|
|
|
|
];
|
|
|
|
|
2024-03-23 21:11:28 +00:00
|
|
|
describe('ESM: unsettled and rejected promises', { concurrency: !process.env.TEST_PARALLEL }, () => {
|
2024-03-07 04:43:44 +00:00
|
|
|
it('should exit for an unsettled TLA promise via --eval with a warning', async () => {
|
2022-07-29 08:42:55 +00:00
|
|
|
const { code, stderr, stdout } = await spawnPromisified(execPath, [
|
|
|
|
...commonArgs,
|
|
|
|
'await new Promise(() => {})',
|
|
|
|
]);
|
|
|
|
|
2024-03-07 04:43:44 +00:00
|
|
|
assert.match(stderr, /Warning: Detected unsettled top-level await at.+\[eval1\]:1/);
|
|
|
|
assert.match(stderr, /await new Promise/);
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(code, 13);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should exit for an unsettled TLA promise via --eval without warning', async () => {
|
|
|
|
const { code, stderr, stdout } = await spawnPromisified(execPath, [
|
|
|
|
'--no-warnings',
|
|
|
|
...commonArgs,
|
|
|
|
'await new Promise(() => {})',
|
|
|
|
]);
|
|
|
|
|
2022-07-29 08:42:55 +00:00
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(code, 13);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw for a rejected TLA promise via --eval', async () => {
|
|
|
|
// Rejected TLA promise, --eval
|
|
|
|
const { code, stderr, stdout } = await spawnPromisified(execPath, [
|
|
|
|
...commonArgs,
|
|
|
|
'await Promise.reject(new Error("Xyz"))',
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.match(stderr, /Error: Xyz/);
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(code, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should exit for an unsettled TLA promise and respect explicit exit code via --eval', async () => {
|
|
|
|
// Rejected TLA promise, --eval
|
|
|
|
const { code, stderr, stdout } = await spawnPromisified(execPath, [
|
2022-05-04 15:51:12 +00:00
|
|
|
...commonArgs,
|
|
|
|
'process.exitCode = 42;await new Promise(() => {})',
|
2022-07-29 08:42:55 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(code, 42);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw for a rejected TLA promise and ignore explicit exit code via --eval', async () => {
|
|
|
|
// Rejected TLA promise, --eval
|
|
|
|
const { code, stderr, stdout } = await spawnPromisified(execPath, [
|
2022-05-04 15:51:12 +00:00
|
|
|
...commonArgs,
|
|
|
|
'process.exitCode = 42;await Promise.reject(new Error("Xyz"))',
|
2022-07-29 08:42:55 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
assert.match(stderr, /Error: Xyz/);
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(code, 1);
|
|
|
|
});
|
|
|
|
|
2024-03-07 04:43:44 +00:00
|
|
|
it('should exit for an unsettled TLA promise with warning', async () => {
|
|
|
|
const { code, stderr, stdout } = await spawnPromisified(execPath, [
|
|
|
|
fixtures.path('es-modules/tla/unresolved.mjs'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.match(stderr, /Warning: Detected unsettled top-level await at.+unresolved\.mjs:1/);
|
|
|
|
assert.match(stderr, /await new Promise/);
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(code, 13);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should exit for an unsettled TLA promise without warning', async () => {
|
2022-07-29 08:42:55 +00:00
|
|
|
const { code, stderr, stdout } = await spawnPromisified(execPath, [
|
|
|
|
'--no-warnings',
|
|
|
|
fixtures.path('es-modules/tla/unresolved.mjs'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(code, 13);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw for a rejected TLA promise via stdin', async () => {
|
|
|
|
const { code, stderr, stdout } = await spawnPromisified(execPath, [
|
|
|
|
'--no-warnings',
|
|
|
|
fixtures.path('es-modules/tla/rejected.mjs'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.match(stderr, /Error: Xyz/);
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(code, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should exit for an unsettled TLA promise and respect explicit exit code via stdin', async () => {
|
|
|
|
const { code, stderr, stdout } = await spawnPromisified(execPath, [
|
|
|
|
'--no-warnings',
|
|
|
|
fixtures.path('es-modules/tla/unresolved-withexitcode.mjs'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(code, 42);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw for a rejected TLA promise and ignore explicit exit code via stdin', async () => {
|
|
|
|
const { code, stderr, stdout } = await spawnPromisified(execPath, [
|
|
|
|
'--no-warnings',
|
|
|
|
fixtures.path('es-modules/tla/rejected-withexitcode.mjs'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.match(stderr, /Error: Xyz/);
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(code, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should exit successfully when calling `process.exit()` in `.mjs` file', async () => {
|
|
|
|
const { code, stderr, stdout } = await spawnPromisified(execPath, [
|
|
|
|
'--no-warnings',
|
|
|
|
fixtures.path('es-modules/tla/process-exit.mjs'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(code, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be unaffected by `process.exit()` in worker thread', async () => {
|
2024-03-07 04:43:44 +00:00
|
|
|
const { code, stderr, stdout } = await spawnPromisified(execPath, [
|
|
|
|
fixtures.path('es-modules/tla/unresolved-with-worker-process-exit.mjs'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.match(stderr, /Warning: Detected unsettled top-level await at.+with-worker-process-exit\.mjs:5/);
|
|
|
|
assert.match(stderr, /await new Promise/);
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(code, 13);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be unaffected by `process.exit()` in worker thread without warning', async () => {
|
2022-07-29 08:42:55 +00:00
|
|
|
const { code, stderr, stdout } = await spawnPromisified(execPath, [
|
|
|
|
'--no-warnings',
|
|
|
|
fixtures.path('es-modules/tla/unresolved-with-worker-process-exit.mjs'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
assert.strictEqual(stdout, '');
|
|
|
|
assert.strictEqual(code, 13);
|
|
|
|
});
|
|
|
|
});
|