Skip to content

Commit 215621e

Browse files
authored
fix(test): ensure all tests are run and fix failing ones (#33)
1 parent c6f554c commit 215621e

File tree

7 files changed

+50
-13
lines changed

7 files changed

+50
-13
lines changed

lib/internal/errors.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,9 @@ E('ERR_TEST_FAILURE', function (error, failureType) {
359359
this.cause = error
360360
return msg
361361
}, Error)
362+
E('ERR_INVALID_ARG_TYPE',
363+
(name, expected, actual) => `Expected ${name} to be ${expected}, got type ${typeof actual}`,
364+
TypeError)
365+
E('ERR_OUT_OF_RANGE',
366+
(name, expected, actual) => `Expected ${name} to be ${expected}, got ${actual}`,
367+
RangeError)

lib/internal/test_runner/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const { once } = require('events')
2222
const { AbortController } = require('#internal/abort_controller')
2323
const {
2424
codes: {
25+
ERR_INVALID_ARG_TYPE,
2526
ERR_TEST_FAILURE
2627
},
2728
kIsNodeError,
@@ -167,7 +168,7 @@ class Test extends AsyncResource {
167168
break
168169

169170
default:
170-
if (concurrency != null) throw new TypeError(`Expected options.concurrency to be a number or boolean, got ${concurrency}`)
171+
if (concurrency != null) throw new ERR_INVALID_ARG_TYPE('options.concurrency', 'a number or boolean', concurrency)
171172
}
172173

173174
if (timeout != null && timeout !== Infinity) {

lib/internal/validators.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
// https://github.com/nodejs/node/blob/60da0a1b364efdd84870269d23b39faa12fb46d8/lib/internal/validators.js
2+
const {
3+
ERR_INVALID_ARG_TYPE,
4+
ERR_OUT_OF_RANGE
5+
} = require('#internal/errors').codes
6+
27
function isUint32 (value) {
38
return value === (value >>> 0)
49
}
510

611
function validateNumber (value, name, min = undefined, max) {
7-
if (typeof value !== 'number') { throw new TypeError(`Expected ${name} to be a number, got ${value}`) }
12+
if (typeof value !== 'number') {
13+
throw new ERR_INVALID_ARG_TYPE(name, 'a number', value)
14+
}
815

916
if ((min != null && value < min) || (max != null && value > max) ||
1017
((min != null || max != null) && Number.isNaN(value))) {
11-
throw new RangeError(`Expected ${name} to be ${
12-
`${min != null ? `>= ${min}` : ''}${min != null && max != null ? ' && ' : ''}${max != null ? `<= ${max}` : ''}`
13-
}, got ${value}`)
18+
throw new ERR_OUT_OF_RANGE(
19+
name,
20+
`${min != null ? `>= ${min}` : ''}${min != null && max != null ? ' && ' : ''}${max != null ? `<= ${max}` : ''}`,
21+
value
22+
)
1423
}
1524
}
1625

@@ -19,22 +28,22 @@ const validateAbortSignal = (signal, name) => {
1928
(signal === null ||
2029
typeof signal !== 'object' ||
2130
!('aborted' in signal))) {
22-
throw new TypeError(`Expected ${name} to be an AbortSignal, got ${signal}`)
31+
throw new ERR_INVALID_ARG_TYPE(name, 'an AbortSignal', signal)
2332
}
2433
}
2534

2635
const validateUint32 = (value, name, positive) => {
2736
if (typeof value !== 'number') {
28-
throw new TypeError(`Expected ${name} to be a number, got ${value}`)
37+
throw new ERR_INVALID_ARG_TYPE(name, 'a number', value)
2938
}
3039
if (!Number.isInteger(value)) {
31-
throw new RangeError(`Expected ${name} to be an integer, got ${value}`)
40+
throw new ERR_OUT_OF_RANGE(name, 'an integer', value)
3241
}
3342
const min = positive ? 1 : 0
3443
// 2 ** 32 === 4294967296
3544
const max = 4_294_967_295
3645
if (value < min || value > max) {
37-
throw new RangeError(`Expected ${name} to be ${`>= ${min} && <= ${max}`}, got ${value}`)
46+
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value)
3847
}
3948
}
4049

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"test": "npm run test:lint && npm run test:unit",
2121
"test:lint": "standard",
2222
"test:lint:fix": "standard --fix",
23-
"test:unit": "node test/parallel/* && node test/message && node test/node-core-test.js"
23+
"test:unit": "node test/parallel.mjs && node test/message.js && node test/node-core-test.js"
2424
},
2525
"devDependencies": {
2626
"standard": "^17.0.0"

test/parallel.mjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env node
2+
3+
import { once } from 'node:events'
4+
import { spawn } from 'node:child_process'
5+
import fs from 'node:fs/promises'
6+
import process from 'node:process'
7+
import { fileURLToPath } from 'node:url'
8+
9+
const PARALLEL_DIR = new URL('./parallel/', import.meta.url)
10+
const dir = await fs.opendir(PARALLEL_DIR)
11+
12+
for await (const { name } of dir) {
13+
if (!name.endsWith('.js')) continue
14+
const cp = spawn(
15+
process.execPath,
16+
[fileURLToPath(new URL(name, PARALLEL_DIR))],
17+
{ stdio: 'inherit' }
18+
)
19+
const [code] = await once(cp, 'exit')
20+
if (code) process.exit(code)
21+
}

test/parallel/test-runner-misc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
const common = require('../common')
55
const assert = require('assert')
66
const { spawnSync } = require('child_process')
7-
const { setTimeout } = require('timers/promises')
7+
const { setTimeout } = require('#timers/promises')
88

99
if (process.argv[2] === 'child') {
10-
const test = require('node:test')
10+
const test = require('#node:test')
1111

1212
if (process.argv[3] === 'abortSignal') {
1313
assert.throws(() => test({ signal: {} }), {

test/parallel/test-runner-option-validation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'use strict'
44
require('../common')
55
const assert = require('assert')
6-
const test = require('node:test');
6+
const test = require('#node:test');
77

88
// eslint-disable-next-line symbol-description
99
[Symbol(), {}, [], () => {}, 1n, true, '1'].forEach((timeout) => {

0 commit comments

Comments
 (0)