Skip to content

Commit

Permalink
fix(expect): accept functions in toHaveLength matcher (#9796)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Apr 11, 2020
1 parent ff87b43 commit 317c413
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[expect]` Restore support for passing functions to `toHaveLength` matcher ([#9796](https://github.com/facebook/jest/pull/9796))

### Chore & Maintenance

### Performance
Expand Down
Expand Up @@ -3046,6 +3046,13 @@ Expected length: not <g>2</>
Received array: <r>[1, 2]</>
`;

exports[`.toHaveLength {pass: true} expect([Function anonymous]).toHaveLength(0) 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toHaveLength<d>(</><g>expected</><d>)</>

Expected length: not <g>0</>
Received function: <r>[Function anonymous]</>
`;

exports[`.toHaveLength error cases 1`] = `
<d>expect(</><r>received</><d>).</>toHaveLength<d>(</><g>expected</><d>)</>

Expand Down
1 change: 1 addition & 0 deletions packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -1694,6 +1694,7 @@ describe('.toHaveLength', () => {
[['a', 'b'], 2],
['abc', 3],
['', 0],
[() => {}, 0],
].forEach(([received, length]) => {
test(`{pass: true} expect(${stringify(
received,
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/matchers.ts
Expand Up @@ -630,7 +630,7 @@ const matchers: MatchersObject = {

if (
typeof received !== 'string' &&
(typeof received !== 'object' || typeof received.length !== 'number')
(!received || typeof received.length !== 'number')
) {
throw new Error(
matcherErrorMessage(
Expand Down

0 comments on commit 317c413

Please sign in to comment.