Skip to content

Make it easier to have multiple checks for the same values #108

Open

Description

@LeaVerou

I just ran into this scenario, which I suspect is pretty common: while testing an object (a subclass of Set), I wanted to test both the values as well as value type (to make sure it was the subclass I was defining and not Set).

One potential solution is to make check() take an array, in which case multiple tests are generated, one for each check.
One tricky bit is that what happens if you want to specify a map() function on only some of them?

Perhaps the solution would be to just utilize nesting which already works:

export default {
	// test params here
	tests: [ 
		// all parent params inherited, so we only specify what changes
		{check: check.is("MyFoo")], 
		{map: v => [...v]}
	],
}

However, this doesn't scale when you have multiple tests.
It seems this is another instance of the more general problem "You have M and Ns and you want to test every combination of M x N", which is also the subject of #106 .

I wonder if the right solution might be some way to basically automate the nested solution, i.e. provide tests that will be automatically added to all leafs under a certain group.
E.g. assuming we call that property leafTests (to be bikeshedded) this means one can do:

export default {
	run (arg) {
		return arg + 1;
	},
	expect: 1,
	leafTests: [ 
		{ /* default equality check */ }
		{ check: check.is("number") }
	],
	tests: [ 
		{ arg: 1 }
		{ arg: Number("1") },
		{ arg: new Number("1") },
	],
}

Which will basically be a macro for doing:

const leafTests = [ 
	{ /* default equality check */ }
	{ check: check.is("number") }
];
export default {
	run (arg) {
		return arg + 1;
	},
	expect: 1,
	tests: [ 
		{ arg: 1, tests: leafTests }
		{ arg: Number("1"), tests: leafTests },
		{ arg: new Number("1"), tests: leafTests },
	],
}

And note that given how nesting works, either of these can be nested, and it should just work.
This allows people providing multiple sets of not just checking functions, but also data, run(), basically any key we support.

I think I'm becoming convinced this is the way to go, but we need to find a good name for this concept.
Some ideas:

  • checks (since in most cases this will be about check/map)
  • childTests
  • autoChildren
  • forEach
  • each
  • cross (since this is basically doing a cross-product)
  • crossTests
  • with
  • withTests

Activity

  1. Zearin commented on Dec 13, 2025

    @Zearin
    Contributor

    −1 for:

    • checks
      • (it sounds like an alias for check.and(), which is not what this does)
    • childTests
      • Given the inheritance nature of the framework, childTests would create confusion, since you can already nest tests as much as you like
    • autoChildren
      • …This just doesn’t sound right to me :)
    • forEach
      • My gut tells me to avoid reusing a name that’s way more familiar in a different context (the Array method forEach)
    • each
      • Not particularly descriptive of what it does
    • with
      • Same rationale as forEach
      • Caveat: Unlike forEach(), with is universally discouraged, so maybe fewer people have an existing association for the word
    • withTests
      • The user is already writing tests, so withTests makes me wonder something like, “Does this mean that I’m using regular tests wrong, or inefficiently?”

    +1 for:

    • crossTests
      • Even for users that are not mathematically inclined (including me), or who are not familiar with cross products (I kinda sorta know it’s a special kind of matrix multiplication, but that’s about all), this can also be read as “across” or “criss-cross” tests, suggesting an orthogonal relationship to the main tests. So this name is descriptive for 2 totally different ways brain types/thinking styles, each getting an accurate “gut sense” of what this might do.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions