Skip to content

Commit

Permalink
fix(test): ensure all tests are run and fix failing ones (nodejs#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Jul 30, 2022
1 parent c6f554c commit 215621e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 13 deletions.
6 changes: 6 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,9 @@ E('ERR_TEST_FAILURE', function (error, failureType) {
this.cause = error
return msg
}, Error)
E('ERR_INVALID_ARG_TYPE',
(name, expected, actual) => `Expected ${name} to be ${expected}, got type ${typeof actual}`,
TypeError)
E('ERR_OUT_OF_RANGE',
(name, expected, actual) => `Expected ${name} to be ${expected}, got ${actual}`,
RangeError)
3 changes: 2 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const { once } = require('events')
const { AbortController } = require('#internal/abort_controller')
const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_TEST_FAILURE
},
kIsNodeError,
Expand Down Expand Up @@ -167,7 +168,7 @@ class Test extends AsyncResource {
break

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

if (timeout != null && timeout !== Infinity) {
Expand Down
25 changes: 17 additions & 8 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
// https://github.com/nodejs/node/blob/60da0a1b364efdd84870269d23b39faa12fb46d8/lib/internal/validators.js
const {
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE
} = require('#internal/errors').codes

function isUint32 (value) {
return value === (value >>> 0)
}

function validateNumber (value, name, min = undefined, max) {
if (typeof value !== 'number') { throw new TypeError(`Expected ${name} to be a number, got ${value}`) }
if (typeof value !== 'number') {
throw new ERR_INVALID_ARG_TYPE(name, 'a number', value)
}

if ((min != null && value < min) || (max != null && value > max) ||
((min != null || max != null) && Number.isNaN(value))) {
throw new RangeError(`Expected ${name} to be ${
`${min != null ? `>= ${min}` : ''}${min != null && max != null ? ' && ' : ''}${max != null ? `<= ${max}` : ''}`
}, got ${value}`)
throw new ERR_OUT_OF_RANGE(
name,
`${min != null ? `>= ${min}` : ''}${min != null && max != null ? ' && ' : ''}${max != null ? `<= ${max}` : ''}`,
value
)
}
}

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

const validateUint32 = (value, name, positive) => {
if (typeof value !== 'number') {
throw new TypeError(`Expected ${name} to be a number, got ${value}`)
throw new ERR_INVALID_ARG_TYPE(name, 'a number', value)
}
if (!Number.isInteger(value)) {
throw new RangeError(`Expected ${name} to be an integer, got ${value}`)
throw new ERR_OUT_OF_RANGE(name, 'an integer', value)
}
const min = positive ? 1 : 0
// 2 ** 32 === 4294967296
const max = 4_294_967_295
if (value < min || value > max) {
throw new RangeError(`Expected ${name} to be ${`>= ${min} && <= ${max}`}, got ${value}`)
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value)
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test": "npm run test:lint && npm run test:unit",
"test:lint": "standard",
"test:lint:fix": "standard --fix",
"test:unit": "node test/parallel/* && node test/message && node test/node-core-test.js"
"test:unit": "node test/parallel.mjs && node test/message.js && node test/node-core-test.js"
},
"devDependencies": {
"standard": "^17.0.0"
Expand Down
21 changes: 21 additions & 0 deletions test/parallel.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

import { once } from 'node:events'
import { spawn } from 'node:child_process'
import fs from 'node:fs/promises'
import process from 'node:process'
import { fileURLToPath } from 'node:url'

const PARALLEL_DIR = new URL('./parallel/', import.meta.url)
const dir = await fs.opendir(PARALLEL_DIR)

for await (const { name } of dir) {
if (!name.endsWith('.js')) continue
const cp = spawn(
process.execPath,
[fileURLToPath(new URL(name, PARALLEL_DIR))],
{ stdio: 'inherit' }
)
const [code] = await once(cp, 'exit')
if (code) process.exit(code)
}
4 changes: 2 additions & 2 deletions test/parallel/test-runner-misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
const common = require('../common')
const assert = require('assert')
const { spawnSync } = require('child_process')
const { setTimeout } = require('timers/promises')
const { setTimeout } = require('#timers/promises')

if (process.argv[2] === 'child') {
const test = require('node:test')
const test = require('#node:test')

if (process.argv[3] === 'abortSignal') {
assert.throws(() => test({ signal: {} }), {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-runner-option-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict'
require('../common')
const assert = require('assert')
const test = require('node:test');
const test = require('#node:test');

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

0 comments on commit 215621e

Please sign in to comment.