question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

toBeArrayOfSize() throwing error on constructing message

See original GitHub issue

When using:

expect(arrayOfObjects).toBeArrayOfSize(n)

I am getting errors from expect-more on creation of the jest error message when the test fails:

  TypeError: Cannot convert object to primitive value
      at Array.join (<anonymous>)
      at Array.toString (<anonymous>)
      at message (node_modules/expect-more-jest/dist/to-be-array-of-size.js:7:51)
      at message (node_modules/expect-more-jest/dist/lib/create-result.js:6:62)
      at getMessage (node_modules/expect/build/index.js:209:15)
      at processResult (node_modules/expect/build/index.js:327:25)
      at Object.toBeArrayOfSize (node_modules/expect/build/index.js:396:16)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
JamieMasoncommented, May 2, 2020

Released in 4.0.2, please let me know of any issues and thanks for all your work on investigating this issue.

1reaction
robross0606commented, May 1, 2020

Here is a more formal test you could use:

describe('Prototypeless Bug', () => {
  const withPrototypes = []
  const withoutPrototypes = []

  beforeAll(() => {
    const createTestObject = (index, withProto) => {
      const obj = withProto ? {} : Object.create(null)
      obj.name = `Object ${index}`
      obj.id = index
      return obj
    }
    for (let i = 0; i < 5; i++) {
      withPrototypes[i] = createTestObject(i + 1, true)
      withoutPrototypes[i] = createTestObject(i + 1, false)
    }
  })

  it('Exception on failing test with array of objects', typeName => {
    let expectedError
    try {
      expect(withPrototypes).toBeArrayOfSize(1)
    } catch (error) {
      expect(error).toMatchInlineSnapshot(
        `[Error: expected [object Object],[object Object],[object Object],[object Object],[object Object] to be an array containing exactly 1 items]`
      )
      expectedError = error
    }
    expect(expect(withoutPrototypes).toBeArrayOfSize(1)).toThrow(error.message)
  })
})

This proves that object literals create objects with prototypes since the first set of expectations work. The second usage should match behavior of the first but fails due to objects not having prototypes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jest-community/jest-extended: Additional Jest matchers - GitHub
Problem. Jest is an amazing test runner and has some awesome assertion APIs built in by default. However, there are times when having...
Read more >
jest-extended - npm
toBeArrayOfSize when checking if a value is an Array of size x. ... test('throws an error of type TypeError with message "hello world"', ......
Read more >
jest-extended 1.0: Additional Matchers for Jest Users - Morioh
toBeArrayOfSize when checking if a value is an Array of size x. ... test('throws an error of type TypeError with message "hello world"',...
Read more >
Jest exception match both type and message - Stack Overflow
Your example code would look like this: test("should throw a CustomError with the right message", () => { expect( ...
Read more >
Scripting support - OpenTest
but when I include it in yaml file than it throws an error as the package which I installed cannot be found. 2....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found