Skip to content

Allow any iterable in toInclude*Members #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

bogdanb
Copy link

@bogdanb bogdanb commented Oct 30, 2020

What

Modifies the three toInclude{All,Any,Some}Members matchers to also match on iterables.

Why

This is meant mostly for testing things that return Set and Map instances, but it’s generally useful for any Iterable.

Notes

I’m not sure exactly what’s wrong with the typing file, I get TS2304: Cannot find name 'Iterable'. errors.

Housekeeping

  • Unit tests
  • Documentation is up to date
  • No additional lint warnings
  • Typescript definitions are added/updated where relevant

@bogdanb
Copy link
Author

bogdanb commented Nov 2, 2020

I’m not actually sure where to update the documentation. I updated the comments in the TypeScript definition file (to say Iterable instead of Array), but I’m not sure if there’s anything else to update.

The type definitions show errors, though, on the type of the argument (which can also be an Iterable, now), so maybe this also needs some config update to see the Iterable definition?

@SimenB
Copy link
Member

SimenB commented Oct 6, 2021

I’m not actually sure where to update the documentation. I updated the comments in the TypeScript definition file (to say Iterable instead of Array), but I’m not sure if there’s anything else to update.

Could you update the docs? It currently says "Array", and maybe add an example using a Set?

@keeganwitt
Copy link
Collaborator

This won't work for maps. contains() uses findIndex(), which won't work for maps.

One way to test whether something is iterable is with

function isIterable(obj) {
  // checks for null and undefined
  if (obj == null) {
    return false;
  }
  return typeof obj[Symbol.iterator] === 'function';
}

But this also returns true for a string -- do we think that's expected behavior for these functions?

@WORMSS
Copy link

WORMSS commented May 18, 2025

But this also returns true for a string -- do we think that's expected behavior for these functions?

Since string IS an iterable, it would be weird if it said false..
Is there advantage of a if null check instead of doing

return typeof obj?.[Symbol.iterator] === 'function';

@keeganwitt
Copy link
Collaborator

Since string IS an iterable, it would be weird if it said false..

I agree, I just wasn't sure that was something considered when this was opened.

Is there advantage of a if null check instead of doing

return typeof obj?.[Symbol.iterator] === 'function';

Nope, just didn't think of it when I grabbed that example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants