-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Magic assert #1154
Magic assert #1154
Conversation
e4bd045
to
57af416
Compare
lib/code-excerpt.js
Outdated
const chalk = require('chalk'); | ||
|
||
function formatLineNumber(line, maxLines) { | ||
return repeating(' ', String(maxLines).length - String(line).length) + line; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use String#repeat()
now.
package.json
Outdated
"pretty-ms": "^2.0.0", | ||
"react": "^15.4.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mistake?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, need to check that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
React is needed by react-element-to-jsx-string
module. I could try submitting a PR (if possible) to avoid the dependency on React.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Can you comment on what's left? |
package.json
Outdated
"power-assert-context-formatter": "^1.0.4", | ||
"power-assert-renderer-assertion": "^1.0.1", | ||
"power-assert-renderer-succinct": "^1.0.1", | ||
"pretty-format": "github:vadimdemedes/pretty-format#colors", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi pretty-format 18 is out :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a fork with support for colorizing the output, from what I can see the latest release doesn't have smth similar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vadimdemedes What's the intention here? Upstream those changes? We cannot depend on a git branch, so we'll either have to upstream or you need to publish your fork to npm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're forking, we could publish to @ava/pretty-format
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will publish it as @ava/pretty-format
and resubmit my syntax highlighting proposal to facebook/jest (original at https://github.com/thejameskyle/pretty-format/issues/57).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will move my fork over to our org too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How I did not know about pretty-format
. :-(
Seems like I reinvented the wheel https://www.npmjs.com/package/prettyprint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha yeah, it took me a while to find out about it too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to remove this line.
`${indentString(err.actual, 2)}\n`, | ||
'Expected:\n', | ||
`${indentString(err.expected, 2)}\n` | ||
].join('\n'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use a single template string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried that, but didn't work out, because the resulting string contains code's indentation:
Actual:
...
Expected:
...
Am I doing smth wrong here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that's a common issue when using template strings like that. You either need to just remove the indent, which looks weird, or use something like https://github.com/sindresorhus/redent In this case, I don't think it's worth it.
lib/format-assert-error.js
Outdated
const patch = diff.createPatch('string', err.actual, err.expected); | ||
const msg = patch | ||
.split('\n') | ||
.splice(4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use .slice(4)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nudge.
lib/cli.js
Outdated
@@ -112,7 +114,7 @@ exports.run = function () { | |||
explicitTitles: cli.flags.watch, | |||
match: arrify(cli.flags.match), | |||
babelConfig: babelConfig.validate(conf.babel), | |||
resolveTestsFrom: cli.input.length === 0 ? pkgDir : process.cwd(), | |||
resolveTestsFrom: resolveTestsFrom, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can just specify resolveTestsFrom,
if you like.
From what I can remember, mostly tests. I also wanted to check if t.jsxEquals works equally great with Preact.
I think this can be solved right now outside of this PR, as this change would be unrelated.
Sure, good catch. |
57af416
to
ff62d97
Compare
8a4c3ca
to
6cd9799
Compare
Added a todo list, so that you guys know what's going on. I squash commits right away, so there wasn't a quick way to track progress. |
776b37d
to
ca94ba5
Compare
Can you not squash? It makes it impossible to review what changed. Just add commits and we'll squash at the end when merging. |
Good point. |
@vadimdemedes Note this issue when using this PR: #1176 (I've confirmed it) |
lib/assert.js
Outdated
test(treeEquals, create(val, expected, '===', msg, x.jsxEqual)); | ||
}; | ||
|
||
x.jsxNotEqual = (val, expected, msg) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be notJsxEqual
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was changing it back and forth, probably should be that way indeed. I didn't like capitalization of jsx
is different in jsxEqual
and notJsxEqual
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree about it looking uglier, but better to be consistent and always have not
first. In a perfect world we would use snakecase in JS...
What output should we display for these tests? https://github.com/avajs/ava/blob/master/test/fixture/throw-named-function.js https://github.com/avajs/ava/blob/master/test/fixture/throw-anonymous-function.js |
@vadimdemedes For reference, currently they output:
I don't think we can do much better than that, as we have no information when people throw non-errors. We could maybe better indicate that it's a non-error. For example:
|
"Threw non-error" seems sweet. |
ca9eb64
to
38ec942
Compare
f16e16f
to
9e9642f
Compare
Attention everyone, magic-assert just crossed off all points from the todo list and is very short from landing in master! I would very appreciate, if you tested it out in some real-world projects and report any issues here. Thank you! |
@jfmengels Great catch, I'll disable assertion output for t.throws. And yes, dots were there before, will create a beginner-friendly issue. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good stuff @vadimdemedes!
lib/assert.js
Outdated
const result = toMatchSnapshot.call(context, tree); | ||
// symbols can't be serialized and saved in a snapshot, | ||
// that's why tree is saved in `jsx` prop, so that JSX can be detected later | ||
const serializedTree = tree.$$typeof === Symbol.for('react.test.json') ? {jsx: tree} : tree; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make the jsx
key a bit more obscure? E.g. __ava_jsx
. Given the deserialization if your tree happens to include a top-level jsx
key you're bound to get odd results.
package.json
Outdated
"power-assert-context-formatter": "^1.0.4", | ||
"power-assert-renderer-assertion": "^1.0.1", | ||
"power-assert-renderer-succinct": "^1.0.1", | ||
"pretty-format": "github:vadimdemedes/pretty-format#colors", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to remove this line.
0c107fd
to
198ad2e
Compare
@novemberborn @jfmengels PR updated, thanks! No more output for |
198ad2e
to
9ea5af4
Compare
Added a new todo:
|
Is this a source maps issue? I've always assumed that |
@vadimdemedes Can you open an issue about this and mention that this will be handled by RFC1? |
@novemberborn Yes, I think so, because it points to a compiled file, not the original. Cool, I'll open an issue, so that we don't forget about this. Until then, I'll add a change, that prevents AVA from crashing, when there's no code excerpt available. |
Done, AVA doesn't crash anymore when code excerpt points to the wrong location and is broken. |
This is absolutely amazing @vadimdemedes! So good in fact, I almost want test failures now 😝 |
Fixes #1155.
Todo:
t.jsxEqual()
andt.jsxNotEqual()
lib/code-excerpt.js
lib/serialize-error.js
lib/format-assert-error.js
t.fail()
err.message
in the outputNOTE
st.jsxEqual
and open a new issue describing what it's forpretty-format
babel-register
is involved (line number doesn't match the actual one)