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

Magic assert #1154

Merged
merged 39 commits into from
Feb 2, 2017
Merged

Magic assert #1154

merged 39 commits into from
Feb 2, 2017

Conversation

vadimdemedes
Copy link
Contributor

@vadimdemedes vadimdemedes commented Dec 12, 2016

Fixes #1155.

Todo:

  • Test t.jsxEqual() and t.jsxNotEqual()
  • Test lib/code-excerpt.js
  • Test lib/serialize-error.js
  • Test lib/format-assert-error.js
  • Update mini reporter
  • Update verbose reporter
  • Update mini reporter tests
  • Update verbose reporter tests
  • Skip output for t.fail()
  • Make snapshots compatible
  • Resolve TODOs in code
  • Insert err.message in the output
  • Make error output identical in mini & verbose
  • Clean up changed code
  • Resolve Bundle react-test-renderer with AVA #1175
  • Resolve Magic assert #1154 (comment)
  • Fix Magic assert #1154 (comment)
  • Fix Magic assert #1154 (comment)
  • Remove all NOTEs
  • Explain Magic assert #1154 (diff)
  • Remove t.jsxEqual and open a new issue describing what it's for
  • Transfer my fork of pretty-format
  • Trim code excerpts
  • Fix code excerpt extraction from non-test files when babel-register is involved (line number doesn't match the actual one)

const chalk = require('chalk');

function formatLineNumber(line, maxLines) {
return repeating(' ', String(maxLines).length - String(line).length) + line;
Copy link
Member

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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mistake?

Copy link
Contributor Author

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.

Copy link
Contributor Author

@vadimdemedes vadimdemedes Jan 9, 2017

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@sindresorhus
Copy link
Member

Can you comment on what's left?

@sindresorhus
Copy link
Member

I noticed $ ava and $ ava --verbose has some inconsistent output. We should really try to unify the output and share logic between them. The output below 1 failed and 1 test failed [02:03:57] should be identical:

screen shot 2016-12-17 at 02 03 44

screen shot 2016-12-17 at 02 03 57

@sindresorhus
Copy link
Member

For t.fail(), I think we should make an exception and not show the Actual/Expected output. It makes no sense and the code excerpt is clear enough.

screen shot 2016-12-17 at 02 06 33

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",
Copy link

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 :)

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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).

Copy link
Contributor Author

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.

Copy link

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.

Copy link
Contributor Author

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.

Copy link
Member

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');
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

const patch = diff.createPatch('string', err.actual, err.expected);
const msg = patch
.split('\n')
.splice(4)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use .slice(4).

Copy link
Member

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,
Copy link
Member

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.

@vadimdemedes
Copy link
Contributor Author

Can you comment on what's left?

From what I can remember, mostly tests. I also wanted to check if t.jsxEquals works equally great with Preact.

The output below 1 failed and 1 test failed [02:03:57] should be identical

I think this can be solved right now outside of this PR, as this change would be unrelated.

For t.fail(), I think we should make an exception and not show the Actual/Expected output. It makes no sense and the code excerpt is clear enough.

Sure, good catch.

@vadimdemedes
Copy link
Contributor Author

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.

@vadimdemedes vadimdemedes force-pushed the magic-assert branch 3 times, most recently from 776b37d to ca94ba5 Compare January 9, 2017 22:52
@sindresorhus
Copy link
Member

I squash commits right away, so there wasn't a quick way to track progress.

Can you not squash? It makes it impossible to review what changed. Just add commits and we'll squash at the end when merging.

@vadimdemedes
Copy link
Contributor Author

Good point.

@sindresorhus
Copy link
Member

@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) => {
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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...

@vadimdemedes
Copy link
Contributor Author

@sindresorhus
Copy link
Member

@vadimdemedes For reference, currently they output:

❯ ava

  1 passed
  1 exception

  Uncaught Exception
  function fooFn() {}
❯ ava

  1 passed
  1 exception

  Uncaught Exception
  () => {}

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:

❯ ava

  1 passed
  1 exception

  Uncaught Exception
  Threw non-error: function fooFn() {}

@mightyiam
Copy link
Contributor

"Threw non-error" seems sweet.

@vadimdemedes
Copy link
Contributor Author

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
Copy link
Contributor

jfmengels commented Jan 30, 2017

The error output is a bit odd when using t.throws.

image

  1. There is a comparison of actual and expected, which may make sense when comparing the thrown errors, but here there is one missing. I think it would be clearer by not having this comparison

  2. Missing expected exception has two dots at the end. I feel like this one has always been like this, so maybe for another PR. That said, I never found this error very comprehensive: Does t.throws expect me to pass an exception type/message, or did it run and the function simply did not throw? Happy to create a different issue to discuss this.

Edit: pretty much the same thing for notThrows when you have an error. Comparing does not make much sense, and the two dots are there too.
image

I don't think this is blocking, this could be improved upon in a later PR/version.

@vadimdemedes
Copy link
Contributor Author

@jfmengels Great catch, I'll disable assertion output for t.throws. And yes, dots were there before, will create a beginner-friendly issue.

Copy link
Member

@novemberborn novemberborn left a 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;
Copy link
Member

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",
Copy link
Member

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.

@vadimdemedes
Copy link
Contributor Author

@novemberborn @jfmengels PR updated, thanks! No more output for t.throws/t.doesNotThrow.

@vadimdemedes
Copy link
Contributor Author

Added a new todo:

Fix code excerpt extraction from non-test files when babel-register is involved (line number doesn't match the actual one)

@novemberborn
Copy link
Member

Fix code excerpt extraction from non-test files when babel-register is involved (line number doesn't match the actual one)

Is this a source maps issue? I've always assumed that babel-core/register causes conflicts in source-map-support. We'll be rolling our own solution with RFC 001 so perhaps this isn't worth worrying about.

@sindresorhus
Copy link
Member

Is this a source maps issue? I've always assumed that babel-core/register causes conflicts in source-map-support. We'll be rolling our own solution with RFC 001 so perhaps this isn't worth worrying about.

@vadimdemedes Can you open an issue about this and mention that this will be handled by RFC1?

@vadimdemedes
Copy link
Contributor Author

Is this a source maps issue?

@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.

@vadimdemedes
Copy link
Contributor Author

Done, AVA doesn't crash anymore when code excerpt points to the wrong location and is broken.

@sindresorhus sindresorhus merged commit c9e6e6f into master Feb 2, 2017
@sindresorhus sindresorhus deleted the magic-assert branch February 2, 2017 04:51
@sindresorhus
Copy link
Member

This is absolutely amazing @vadimdemedes! So good in fact, I almost want test failures now 😝

@vadimdemedes
Copy link
Contributor Author

vadimdemedes commented Feb 2, 2017

Thank you, thank you, thank you! I'm sooo excited! Can't wait for the first bug report :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants