jest-extended icon indicating copy to clipboard operation
jest-extended copied to clipboard

Suggestion: add a new matcher - toHaveAllMembersOfType(...)

Open ghost opened this issue 6 years ago • 3 comments

Feature Request

Description:

I often find it useful (considering the nature of the project I'm working on) to test that all array-members are of the same type, which can currently be done by using the toSatisfyAll matcher:

expect(arr).toSatisfyAll(s => typeof s === 'string')

However, this could be done by a dedicated matcher and have a shorter syntax.

Possible solution:

Introduce a new matcher, say toHaveAllMembersOfType(), that will be used like:

expect(arr).toHaveAllMembersOfType(Number)

If that makes sense, I don't mind submitting a PR for that.

ghost avatar Nov 04 '18 09:11 ghost

Hey @harelmoshe I like this!

I would suggest changing the name to .toIncludeAllMembersOfType(Type) to be consistent with current array matchers.

Do you want to send a PR for this?

mattphillips avatar Nov 05 '18 09:11 mattphillips

@mattphillips sounds great :) I would love to send a PR, will do in the next couple of days.

ghost avatar Nov 05 '18 09:11 ghost

@mattphillips Which option would you think of as more intuitive to represent the type:

expect([1, 2, 3]).toIncludeAllMembersOfType(Number)

or

expect([1, 2, 3]).toIncludeAllMembersOfType(expect.any(Number))

?

The first one is more concise but can be misleading (since Number is not the typeof 1, e.g.), while the second one is more jest-ish but kind of verbose. Any thoughts here?

ghost avatar Nov 12 '18 20:11 ghost