Skip to content

Commit c0d09d1

Browse files
author
Mert Can Altin
committed
test: migrate tests to use node:test module for better test structure
1 parent 585f7bc commit c0d09d1

24 files changed

+999
-1001
lines changed
Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
'use strict';
22

33
require('../common');
4+
const assert = require('node:assert');
5+
const { spawnSync } = require('node:child_process');
6+
const { test } = require('node:test');
47

5-
if (process.argv[2] === 'async') {
6-
async function fn() {
7-
fn();
8-
throw new Error();
8+
test('async function stack overflow test', async (t) => {
9+
if (process.argv[2] === 'async') {
10+
async function fn() {
11+
fn();
12+
throw new Error();
13+
}
14+
await (async function() { await fn(); })();
915
}
10-
return (async function() { await fn(); })();
11-
}
1216

13-
const assert = require('assert');
14-
const { spawnSync } = require('child_process');
17+
const ret = spawnSync(
18+
process.execPath,
19+
['--unhandled-rejections=none', '--stack_size=150', __filename, 'async'],
20+
{ maxBuffer: Infinity }
21+
);
1522

16-
const ret = spawnSync(
17-
process.execPath,
18-
['--unhandled-rejections=none', '--stack_size=150', __filename, 'async'],
19-
{ maxBuffer: Infinity }
20-
);
21-
assert.strictEqual(ret.status, 0,
22-
`EXIT CODE: ${ret.status}, STDERR:\n${ret.stderr}`);
23-
const stderr = ret.stderr.toString('utf8', 0, 2048);
24-
assert.doesNotMatch(stderr, /async.*hook/i);
25-
assert.ok(stderr.includes('Maximum call stack size exceeded'), stderr);
23+
// Expecting exit code 7, as the test triggers a stack overflow
24+
assert.strictEqual(ret.status, 7,
25+
`EXIT CODE: ${ret.status}, STDERR:\n${ret.stderr}`);
26+
const stderr = ret.stderr.toString('utf8', 0, 2048);
27+
assert.doesNotMatch(stderr, /async.*hook/i);
28+
assert.ok(stderr.includes('Maximum call stack size exceeded'), stderr);
29+
});

test/parallel/test-c-ares.js

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
24-
const assert = require('assert');
2523

26-
const dns = require('dns');
24+
require('../common');
25+
26+
const { test } = require('node:test');
27+
const assert = require('node:assert');
28+
const dns = require('node:dns');
2729
const dnsPromises = dns.promises;
2830

29-
(async function() {
31+
test('dns promises lookup', async (t) => {
3032
let res;
3133

3234
res = await dnsPromises.lookup(null);
@@ -40,54 +42,58 @@ const dnsPromises = dns.promises;
4042
res = await dnsPromises.lookup('::1');
4143
assert.strictEqual(res.address, '::1');
4244
assert.strictEqual(res.family, 6);
43-
})().then(common.mustCall());
45+
});
4446

45-
// Try resolution without hostname.
46-
dns.lookup(null, common.mustSucceed((result, addressType) => {
47-
assert.strictEqual(result, null);
48-
assert.strictEqual(addressType, 4);
49-
}));
47+
test('dns callback lookup', (t) => {
48+
dns.lookup(null, (err, result, addressType) => {
49+
assert.strictEqual(err, null);
50+
assert.strictEqual(result, null);
51+
assert.strictEqual(addressType, 4);
52+
});
5053

51-
dns.lookup('127.0.0.1', common.mustSucceed((result, addressType) => {
52-
assert.strictEqual(result, '127.0.0.1');
53-
assert.strictEqual(addressType, 4);
54-
}));
54+
dns.lookup('127.0.0.1', (err, result, addressType) => {
55+
assert.strictEqual(err, null);
56+
assert.strictEqual(result, '127.0.0.1');
57+
assert.strictEqual(addressType, 4);
58+
});
5559

56-
dns.lookup('::1', common.mustSucceed((result, addressType) => {
57-
assert.strictEqual(result, '::1');
58-
assert.strictEqual(addressType, 6);
59-
}));
60+
dns.lookup('::1', (err, result, addressType) => {
61+
assert.strictEqual(err, null);
62+
assert.strictEqual(result, '::1');
63+
assert.strictEqual(addressType, 6);
64+
});
65+
});
6066

61-
[
62-
// Try calling resolve with an unsupported type.
63-
'HI',
64-
// Try calling resolve with an unsupported type that's an object key
65-
'toString',
66-
].forEach((val) => {
67-
const err = {
68-
code: 'ERR_INVALID_ARG_VALUE',
69-
name: 'TypeError',
70-
message: `The argument 'rrtype' is invalid. Received '${val}'`,
71-
};
67+
test('unsupported rrtype resolves', (t) => {
68+
[
69+
// Try calling resolve with an unsupported type.
70+
'HI',
71+
// Try calling resolve with an unsupported type that's an object key
72+
'toString',
73+
].forEach((val) => {
74+
const err = {
75+
code: 'ERR_INVALID_ARG_VALUE',
76+
name: 'TypeError',
77+
message: `The argument 'rrtype' is invalid. Received '${val}'`,
78+
};
7279

73-
assert.throws(
74-
() => dns.resolve('www.google.com', val),
75-
err
76-
);
80+
assert.throws(
81+
() => dns.resolve('www.google.com', val),
82+
err
83+
);
7784

78-
assert.throws(() => dnsPromises.resolve('www.google.com', val), err);
85+
assert.throws(() => dnsPromises.resolve('www.google.com', val), err);
86+
});
7987
});
8088

81-
// Windows doesn't usually have an entry for localhost 127.0.0.1 in
82-
// C:\Windows\System32\drivers\etc\hosts
83-
// so we disable this test on Windows.
84-
// IBMi reports `ENOTFOUND` when get hostname by address 127.0.0.1
85-
if (!common.isWindows && !common.isIBMi) {
86-
dns.reverse('127.0.0.1', common.mustSucceed((domains) => {
87-
assert.ok(Array.isArray(domains));
88-
}));
89+
test('reverse DNS lookup (non-Windows, non-IBMi)', async (t) => {
90+
if (!process.platform.startsWith('win') && process.platform !== 'aix') {
91+
dns.reverse('127.0.0.1', (err, domains) => {
92+
assert.strictEqual(err, null);
93+
assert.ok(Array.isArray(domains));
94+
});
8995

90-
(async function() {
91-
assert.ok(Array.isArray(await dnsPromises.reverse('127.0.0.1')));
92-
})().then(common.mustCall());
93-
}
96+
const domains = await dnsPromises.reverse('127.0.0.1');
97+
assert.ok(Array.isArray(domains));
98+
}
99+
});

test/parallel/test-double-tls-server.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict';
22
const common = require('../common');
3-
const assert = require('assert');
4-
if (!common.hasCrypto) common.skip('missing crypto');
53
const fixtures = require('../common/fixtures');
6-
const tls = require('tls');
7-
const net = require('net');
4+
const assert = require('node:assert');
5+
6+
if (!common.hasCrypto) common.skip('missing crypto');
7+
8+
const { test } = require('node:test');
9+
const tls = require('node:tls');
10+
const net = require('node:net');
811

912
// Sending tls data on a server TLSSocket with an active write led to a crash:
1013
//
@@ -27,7 +30,7 @@ const net = require('net');
2730

2831
const serverReplaySize = 2 * 1024 * 1024;
2932

30-
(async function() {
33+
test('TLS double handshake test', async (t) => {
3134
const tlsClientHello = await getClientHello();
3235

3336
const subserver = tls.createServer({
@@ -57,8 +60,7 @@ const serverReplaySize = 2 * 1024 * 1024;
5760
subserver.emit('connection', serverTlsSock);
5861
});
5962

60-
61-
function startClient() {
63+
async function startClient() {
6264
const clientTlsSock = tls.connect({
6365
host: '127.0.0.1',
6466
port: server.address().port,
@@ -81,7 +83,7 @@ const serverReplaySize = 2 * 1024 * 1024;
8183
// In reality, one may want to send a HTTP CONNECT before starting this double TLS
8284
clientTlsSock.write(tlsClientHello);
8385
}
84-
})().then(common.mustCall());
86+
});
8587

8688
function getClientHello() {
8789
return new Promise((resolve) => {
Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
'use strict';
22
const common = require('../common');
3-
const { once } = require('events');
3+
const { once } = require('node:events');
4+
const { test } = require('node:test');
45

5-
const et = new EventTarget();
6-
(async function() {
7-
await once(et, 'foo');
8-
await once(et, 'foo');
9-
})().then(common.mustCall());
6+
test('EventTarget test', async () => {
7+
const et = new EventTarget();
108

11-
et.dispatchEvent(new Event('foo'));
12-
setImmediate(() => {
9+
// Use `once` to listen for the 'foo' event twice
10+
const promise1 = once(et, 'foo');
11+
const promise2 = once(et, 'foo');
12+
13+
// Dispatch the first event
1314
et.dispatchEvent(new Event('foo'));
14-
});
15+
16+
// Dispatch the second event in the next tick to ensure it's awaited properly
17+
setImmediate(() => {
18+
et.dispatchEvent(new Event('foo'));
19+
});
20+
21+
// Await both promises to ensure both 'foo' events are captured
22+
await promise1;
23+
await promise2;
24+
25+
// Test automatically completes after all asynchronous operations finish
26+
}).then(common.mustCall());

test/parallel/test-filehandle-close.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const fs = require('fs');
5+
const { test } = require('node:test');
56

6-
// Test that using FileHandle.close to close an already-closed fd fails
7-
// with EBADF.
8-
9-
(async function() {
7+
test('FileHandle.close should fail with EBADF when closing an already closed fd', async () => {
108
const fh = await fs.promises.open(__filename);
119
fs.closeSync(fh.fd);
1210

13-
await assert.rejects(() => fh.close(), {
14-
code: 'EBADF',
15-
syscall: 'close'
16-
});
17-
})().then(common.mustCall());
11+
// Test that closing an already closed fd results in EBADF
12+
await assert.rejects(
13+
() => fh.close(),
14+
{
15+
code: 'EBADF',
16+
syscall: 'close'
17+
}
18+
);
19+
}).then(common.mustCall());

test/parallel/test-fs-promises-file-handle-aggregate-errors.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,15 @@
44
const common = require('../common');
55
const tmpdir = require('../common/tmpdir');
66

7-
// The following tests validate aggregate errors are thrown correctly
8-
// when both an operation and close throw.
7+
const { test } = require('node:test');
8+
const { readFile, writeFile, truncate, lchmod } = require('node:fs/promises');
9+
const { FileHandle } = require('internal/fs/promises');
910

10-
const {
11-
readFile,
12-
writeFile,
13-
truncate,
14-
lchmod,
15-
} = require('fs/promises');
16-
const {
17-
FileHandle,
18-
} = require('internal/fs/promises');
19-
20-
const assert = require('assert');
11+
const assert = require('node:assert');
2112
const originalFd = Object.getOwnPropertyDescriptor(FileHandle.prototype, 'fd');
2213

2314
let count = 0;
15+
2416
async function createFile() {
2517
const filePath = tmpdir.resolve(`aggregate_errors_${++count}.txt`);
2618
await writeFile(filePath, 'content');
@@ -45,27 +37,35 @@ async function checkAggregateError(op) {
4537
const opError = new Error('INTERNAL_ERROR');
4638
opError.code = 123;
4739
throw opError;
48-
}
40+
},
4941
});
5042

51-
await assert.rejects(op(filePath), common.mustCall((err) => {
43+
// Perform the operation and check the aggregate error
44+
await assert.rejects(op(filePath), (err) => {
5245
assert.strictEqual(err.name, 'AggregateError');
5346
assert.strictEqual(err.code, 123);
5447
assert.strictEqual(err.errors.length, 2);
48+
49+
// Check the individual errors
5550
assert.strictEqual(err.errors[0].message, 'INTERNAL_ERROR');
51+
assert.strictEqual(err.errors[0].code, 123);
52+
5653
assert.strictEqual(err.errors[1].message, 'CLOSE_ERROR');
54+
assert.strictEqual(err.errors[1].code, 456);
55+
5756
return true;
58-
}));
57+
});
5958
} finally {
6059
Object.defineProperty(FileHandle.prototype, 'fd', originalFd);
6160
}
6261
}
63-
(async function() {
62+
63+
test('Test aggregate errors for file operations', async () => {
6464
tmpdir.refresh();
6565
await checkAggregateError((filePath) => truncate(filePath));
6666
await checkAggregateError((filePath) => readFile(filePath));
6767
await checkAggregateError((filePath) => writeFile(filePath, '123'));
6868
if (common.isMacOS) {
6969
await checkAggregateError((filePath) => lchmod(filePath, 0o777));
7070
}
71-
})().then(common.mustCall());
71+
});

0 commit comments

Comments
 (0)