jest: toBeGreaterThan message correction for failing case

Improvement

This bug was While a case fails for the condition where user expect the result to be greater than certain value , jest reply with the message of expecting a value same as the argument that is passed to function toBeGreaterThan() . It should show something more detailed like >0 or min 1 In case of argument value as 0.

To Reproduce

Steps to reproduce the behavior: 1 Write the function registerUsername as mentioned:-

function registerUsername(username) {
   if (!username) throw new Error('Username is required.');
   return { id: 0, username: username }
}

2 Write a testcase as mentioned:-

describe('registerUser', () => {
    it('should return a user object if valid username is passed', () => {
        const result = lib.registerUser('Abhi')
        expect(result).toMatchObject({ username: 'Abhi'})
        expect(result.id).toBeGreaterThan(0)
    })
})

3 Now run the test.

Expected behavior

> testing-demo@1.0.0 test /var/www/html/11.6- Writing Your First Test/testing-demo
> jest

 FAIL  tests/lib.test.js
  registerUser
    ✕ should return a user object if valid username is passed (11ms)

● registerUser › should return a user object if valid username is passed

expect(received).toBeGreaterThan(expected)

Expected value to be greater than: 0
Received: 0

  51 |         const result = lib.registerUser('Abhi')
  52 |         expect(result).toMatchObject({ username: 'Abhi'})
> 53 |         expect(result.id).toBeGreaterThan(0)
     |                           ^
  54 |     })
  55 | })

  at Object.toBeGreaterThan (tests/lib.test.js:53:27)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 7 passed, 8 total
Snapshots:   0 total
Time:        0.867s, estimated 1s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

Behavior On 23.6.0

> testing-demo@1.0.0 test /var/www/html/11.6- Writing Your First Test/testing-demo
> jest

 FAIL  tests/lib.test.js
  registerUser
    ✕ should return a user object if valid username is passed (11ms)

● registerUser › should return a user object if valid username is passed

expect(received).toBeGreaterThan(expected)

Expected: 0
Received: 0

  51 |         const result = lib.registerUser('Abhi')
  52 |         expect(result).toMatchObject({ username: 'Abhi'})
> 53 |         expect(result.id).toBeGreaterThan(0)
     |                           ^
  54 |     })
  55 | })

  at Object.toBeGreaterThan (tests/lib.test.js:53:27)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 7 passed, 8 total
Snapshots:   0 total
Time:        0.867s, estimated 1s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

Link to repl or repo (highly encouraged)

repl.it demo

The Should Work like this link provided but i am getting different result in 23.6.0

Run npx envinfo --preset jest

Paste the results here:

npx: installed 1 in 2.748s

  System:
    OS: Linux 4.15 Ubuntu 18.04.1 LTS (Bionic Beaver)
    CPU: x64 Intel(R) Core(TM) i3-7100 CPU @ 3.90GHz
  Binaries:
    Node: 8.10.0 - /usr/bin/node
    npm: 6.4.1 - /usr/local/bin/npm


About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 17 (12 by maintainers)

Commits related to this issue

Most upvoted comments

toBeGreaterThanOrEqual already exists in Jest core https://jestjs.io/docs/en/expect#tobegreaterthanorequalnumber

I agree that the error message seems pretty clear to me, @Abhishek-Modi-Happyperks would you be able to suggest an alternative error message?