Skip to content

Commit

Permalink
docs: Updated match custom-assertion jsdoc (#2636)
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 authored Oct 4, 2024
1 parent 41002cd commit c37abe5
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions test/lib/custom-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
'use strict'
const assert = require('node:assert')
const { isSimpleObject } = require('../../lib/util/objects')
const typeMappings = {
String: 'string',
Number: 'number'
}

function assertExactClmAttrs(segmentStub, expectedAttrs) {
const attrs = segmentStub.addAttribute.args
Expand Down Expand Up @@ -177,24 +173,32 @@ function assertSegments(parent, expected, options) {
}
}

const TYPE_MAPPINGS = {
String: 'string',
Number: 'number'
}

/**
* Like `tap.prototype.match`. Verifies that `actual` satisfies the shape
* provided by `expected`.
* provided by `expected`. This does actual assertions with `node:assert`
*
* There is limited support for type matching
*
* This may eventually make its way into `node:assert`. See
* https://github.com/fastify/fastify/discussions/5628#discussioncomment-10392942
* @example
* match(obj, {
* key: String,
* number: Number
* })
*
* @example
* const input = {
* foo: /^foo.+bar$/,
* bar: [1, 2, '3']
* }
* // true
* match(input, {
* foo: 'foo is bar',
* bar: [1, 2, '3']
* })
* // false
* match(input, {
* foo: 'foo is bar',
* bar: [1, 2, '3', 4]
Expand All @@ -203,10 +207,9 @@ function assertSegments(parent, expected, options) {
* @param {string|object} actual The entity to verify.
* @param {string|object} expected What the entity should match against.
*
* @returns {boolean} `true` if `actual` satisfies `expected`. `false`
* otherwise.
*/
function match(actual, expected) {
// match substring
if (typeof actual === 'string' && typeof expected === 'string') {
assert.ok(actual.indexOf(expected) > -1)
return
Expand All @@ -216,7 +219,7 @@ function match(actual, expected) {
if (key in actual) {
if (typeof expected[key] === 'function') {
const type = expected[key]
assert.ok(typeof actual[key] === typeMappings[type.name])
assert.ok(typeof actual[key] === TYPE_MAPPINGS[type.name])
} else if (expected[key] instanceof RegExp) {
assert.ok(expected[key].test(actual[key]))
} else if (typeof expected[key] === 'object' && expected[key] !== null) {
Expand Down

0 comments on commit c37abe5

Please sign in to comment.