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

toInclude*Members should be declared with readonly arguments

Open bogdanb opened this issue 4 years ago • 0 comments

Bug

  • jest-extended version: 0.11.5
  • node version: 14.7.0
  • npm (or yarn) version: 6.14.8

Relevant code or config

const expected : readonly number[] = [1, 2, 3];
expect ([...expected]).toIncludeAllMembers(expected);

What you did: Running code like the above (this is much simplified) leads to TypeScript complaining with an error like this:

TS2345: Argument of type 'readonly number[]' is not assignable to parameter of type 'any[]'.
The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'any[]'.

What happened (please provide anything you think will help):

The issue is that all the matchers are declared like this:

toIncludeSameMembers(members: any[]): R;

They should probably have been declared like this:

toIncludeSameMembers(members: readonly any[]): R;

Because the matcher never needs to mutate the array. As far as I can tell, the following matchers are affected:

  • toBeOneOf
  • toIncludeAllMembers
  • toIncludeAnyMembers
  • toIncludeSameMembers
  • toContainKeys
  • toContainAllKeys
  • toContainAnyKeys
  • toContainValues
  • toContainAllValues
  • toContainAnyValues
  • toContainEntries
  • toContainAllEntries
  • toContainAnyEntries
  • toIncludeMultiple

This applies to both jest.Matchers and jest.Expect interfaces.

bogdanb avatar Oct 30 '20 15:10 bogdanb