toBeGreaterThan message correction for failing case
See original GitHub issueImprovement
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)
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
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:17 (10 by maintainers)
Top Results From Across the Web
Expect - Jest
In this case, toBe is the matcher function. ... your tests will still work, but the error messages on failing tests will look...
Read more >Jest Expect - w3resource
This is a string that allows you to display a clear and correct ... You need to craft a precise failure message to...
Read more >Expect · Jest
toBeGreaterThan (number) . ... In this case, toBe is the matcher function. ... You should craft a precise failure message to make sure...
Read more >API Reference | Vitest
toBeGreaterThan asserts if actual value is greater than received one. Equal values will fail the test. ts
Read more >To Adjust or Not to Adjust? When a “Confounder” Is Only Meas...
In another case–control study of migraine and the risk of stroke, adjustment was made for several confounders, again measured at the time of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
toBeGreaterThanOrEqual
already exists in Jest core https://jestjs.io/docs/en/expect#tobegreaterthanorequalnumberI agree that the error message seems pretty clear to me, @Abhishek-Modi-Happyperks would you be able to suggest an alternative error message?
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.