Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best practice assertions for string inclusion and dynamic regex #381

Closed
alubbe opened this issue Dec 28, 2015 · 6 comments
Closed

Best practice assertions for string inclusion and dynamic regex #381

alubbe opened this issue Dec 28, 2015 · 6 comments
Labels
blocked waiting on something else to be resolved first enhancement new functionality

Comments

@alubbe
Copy link

alubbe commented Dec 28, 2015

Today, I've run into two cases where I ended up writing helpers to get better errors. After writing them, I realized that it is somewhat against the spirit of "just writing assertions and getting readable errors". So I'd like to find out what you think is best practice here and whether it might make sense to expand what power-assert is displaying.

String inclusion

I've got some big dynamic string and want to make sure it contains another dynamic string. So I wrote

t.ok(bigString.includes(shortString))

When this fails, it will print the value of shortString and the return value of .includes(), which is false, but will not show me the value of bigString, which I need to properly debug it. So either I re-run the test with a console.log or I create helpers and refactor to

function includes(a,b){return a.includes(b)}
....
t.ok(includes(bigString, shortString))

In the case of a failure, this will print both bigString and shortString, allowing me to figure out what went wrong at the first glance.

Dynamic Regex

I've got a dynamic regex and want to run it against another dynamic string. So I wrote

t.ok(dynamicRegex.test(dynamicString))

Like in the above case, when this fails, it will print the value of dynamicString and the return value of .test(), which is false, but will not show me the value of dynamicRegex, which I need to properly debug it. I could use dynamicString.match(dynamicRegex), which will show the value of dynamicRegex, but it will also hide the value of dynamicString. So, like above, I wrote a helper

function test(a,b){return a.test(b)}
...
t.ok(test(dynamicRegex, dynamicString))

Conclusion

I'm guessing that power-assert avoids printing the top level object by default to avoid visual clutter, but in the case of strings and regex-objects (and probably numbers and booleans, as well), I would find it useful to print them.

This might end up being a power-assert issue, feel free to point that out and I will close & create an issue at its repo.

@alubbe
Copy link
Author

alubbe commented Feb 18, 2016

t.regex has now effectively solved one of my 2 issues.
Any chance to get a better helper for string includes?

@novemberborn
Copy link
Member

@twada @jamestalmage perhaps you could provide some insight into this?

@twada
Copy link
Contributor

twada commented Mar 1, 2016

@sindresorhus sindresorhus added enhancement new functionality blocked waiting on something else to be resolved first labels Mar 1, 2016
@novemberborn
Copy link
Member

AVA now prints all values.

@zvictor
Copy link

zvictor commented Nov 18, 2020

AVA now prints all values.

I am not seeing that happening.


t.true(a.includes(b))

prints only

  Value is not `true`:

  false

@novemberborn
Copy link
Member

@zvictor well I wrote that three years ago 😉

AVA no longer prints all values. Per https://github.com/avajs/ava/blob/master/docs/03-assertions.md#enhanced-assertion-messages this requires you to enable Babel, and it only works with t.assert().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked waiting on something else to be resolved first enhancement new functionality
Projects
None yet
Development

No branches or pull requests

5 participants