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

Make snapshots usable again #1317

Merged
merged 2 commits into from
Apr 2, 2017
Merged

Make snapshots usable again #1317

merged 2 commits into from
Apr 2, 2017

Conversation

novemberborn
Copy link
Member

@novemberborn novemberborn commented Mar 21, 2017

Had to start with another refactor, this time of Runner.

Anyway, the cool part is making snapshots work again:

It's still not ideal (see #1275), especially since the diff output is different from other assertions, but at least it makes snapshots usable.

@novemberborn novemberborn changed the base branch from test-overhaul to master March 23, 2017 15:34
@novemberborn novemberborn force-pushed the make-snapshot-work branch 2 times, most recently from b21d2a3 to ac73897 Compare March 26, 2017 18:08
lib/runner.js Outdated

return Promise.resolve(this.tests.build().run()).then(this._buildStats);
this.tests.on('test', result => this.addTestResult(result));
return new Promise(resolve => {
Copy link
Member

Choose a reason for hiding this comment

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

This is exactly what Promise#try is useful for.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah Promise here is indeed from bluebird. I guess I'll rename it to Bluebird.try().

lib/runner.js Outdated
this.hasStarted = true;

return Promise.resolve(this.tests.build().run()).then(this._buildStats);
this.tests.on('test', result => this.addTestResult(result));
Copy link
Member

Choose a reason for hiding this comment

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

I bit unrelated, but I usually only use inline arrow functions when I actually make use of the return value. This makes it clear whether something is returned or not. What do you think of such approach?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.

@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
Copy link
Member

Choose a reason for hiding this comment

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

We need to change this to AVA Snapshot v1. We don't want to mislead people with being fully compatible with Jest and it's also confusing for AVA users that it doesn't say AVA.

We might need to open an issue on Jest about getting an option for this, but for now I guess we can just read the file in, modify it, and write it out again.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think jest-snapshot just treats these files as CommonJS modules so rewriting the header should be harmless. Will do.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually no, jest-snapshot requires snapshot files to start with this header: https://github.com/facebook/jest/blob/38ca942f6f2979ed25f54510173dbd2761dd7e04/packages/jest-snapshot/src/utils.js#L37

We could add a comment after but that becomes annoyingly complex.

Copy link
Member

Choose a reason for hiding this comment

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

@novemberborn Can you open an issue on Jest to make it configurable? They seem open to making it more generic when needed.

I'm happy to land this as is for now, but long-term we'll want our own naming and versioning.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can you open an issue on Jest to make it configurable? They seem open to making it more generic when needed.

I don't know, if you look at the error messages it very much assumes running inside Jest.

I'm happy to land this as is for now, but long-term we'll want our own naming and versioning.

I think we want our own alternative, really.

Copy link
Member

Choose a reason for hiding this comment

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

Too bad, would have been nice to reuse that code.

// @cpojer

@sindresorhus
Copy link
Member

This looks very good. I almost couldn't tell the difference with our diff. Usable now is better than perfect at some point.

* Don't mix option chain with the prototype, simply expose it on the
instance. Actual code only exposed the `test` method to the user, and
that happens to be the default starting point anyway

* `run()` doesn't need to return stats

* Ensure `run()` always returns a promise, and does not throw

* Simplify `buildStats()`

* Derive chainable methods from the actual chain when generating the
TypeScript definition

* Don't bother prefixing methods / properties with underscores
* Make the Runner manage the snapshot state. Thread an accessor to the
`t.snapshot()` assertion.

* Save snapshots when runner has finished. Fixes #1218.

* Use jest-snapshot directly, without serializing values. Use jest-diff
to generate the diff if the snapshot doesn't match. This does mean the
output is not colored and has other subtle differences with how other
assertions format values, but that's OK for now. Fixes #1220, #1254.

* Pin jest-snapshot and jest-diff versions. This isn't ideal but we're
using private APIs and making other assumptions, so I'm not comfortable
with using a loose SemVer range.
@novemberborn novemberborn merged commit 57fd051 into master Apr 2, 2017
@novemberborn novemberborn deleted the make-snapshot-work branch April 2, 2017 13:48
@cpojer
Copy link

cpojer commented Apr 3, 2017

They are still Jest snapshots though, aren't they? If we can, we should keep the header because it looks to me like they are compatible. We can update error messages and the documentation link that it links to, though. Feel free to send PRs to Jest to make the public API more usable as we'll undoubtedly break private APIs in the future.

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