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

Seamless usage when running within Jest #23

Merged
merged 4 commits into from
Jul 14, 2017

Conversation

tdd
Copy link
Contributor

@tdd tdd commented Jul 10, 2017

Hi all!

Thanks a ton for this module, it's super cool.

However, its existing behavior entirely focuses on people using snapshots outside of Jest, but a significant share of users still use Jest: they just favor Chai's assertion syntax and plugin ecosystem.

I do a lot of JS training, and this is what most of my trainees tell me. That's my situation, too.

This PR makes it so that, when running in Jest, we just delegate to Jest's built-in snapshot capability. No configuration, no setup cost, just registering the Chai plugin, like any other. As seamless as can be.

Tests, examples and docs are updated. I strived to follow the coding style I inferred from the source (even if I'm more of StandardJS fan myself 😉 ).

I hope you like it! Feel free to push back if you have any questions or want tweaks.

This addresses the one reason why I don't use this module in my trainings yet.

FWIW, I'm considering making it even nicer by automatically detecting Enzyme wrappers and then using enzyme-to-json to streamline the testing experience even more. This could be done for all modes, too, and be a nice one-up over Jest's built-in snapshotting. I'd love feedback on the idea.

Best,

@tdd
Copy link
Contributor Author

tdd commented Jul 10, 2017

Hey @suchipi! Would love your feedback on this.

Best,

@suchipi
Copy link
Owner

suchipi commented Jul 10, 2017

Hey! This looks interesting. My recommendation up until now has been for people to do something like this, but I can see how this would be useful for easy setup. I'll pull this down and give it a try later this week.

I don't want to automatically add enzyme-to-json because jest snapshots are used for more than just React and I want the scope of this module to be relatively small. However, snapshot serializers are a good way to automatically add functionality like that- a quick google search finds jest-serializer-enzyme. I'd have no problem adding a note to the README with instructions on how to add it, assuming it works- I haven't given it a try yet.

@tdd
Copy link
Contributor Author

tdd commented Jul 10, 2017

Hey there,

Re: Enzyme serialization: indeed, registering a serializer would make the most sense, that's a good point. Will adjust my own practice on this, and abandon the "too-smart built-in" approach 😉

@tdd
Copy link
Contributor Author

tdd commented Jul 10, 2017

Also re: Jest+Chai aliasing Jest's expect: yes, this is what I used to do, but having to switch between expectors and assertion syntaxes (jestExpect(x).toMatchSnapshot() vs. expect(x).to.matchSnapshot()) is just not a great developer experience. I like consistency 😬

Copy link
Owner

@suchipi suchipi left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this! There are a few things I'd like to change; most of them are just in regard to the npm scripts.

@@ -33,4 +42,19 @@ const buildMatchSnapshot = (utils, parseArgs) => {
};
};

const safeRequireJestExpect = () => {
try {
return require("jest-matchers");
Copy link
Owner

Choose a reason for hiding this comment

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

Jest is planning to rename jest-matchers to expect. Could we return the global expect here instead?

if (typeof expect !== "undefined") {
  return expect;
} else {
  return null;
}

@@ -0,0 +1,25 @@
import chai, { expect } from "chai";
Copy link
Owner

Choose a reason for hiding this comment

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

This file is redundant, because it does the same thing as examples/4_jestMode/spec.js. Could you remove it?

In fact you can remove the whole spec-jest directory

package.json Outdated
@@ -6,12 +6,13 @@
"repository": "https://github.com/suchipi/chai-jest-snapshot",
"scripts": {
"build": "rm -rf dist/* && babel src --out-dir dist",
"mocha": "mocha -r 'dist/spec/index' 'dist/**/*.spec.js'",
"jest": "jest --roots dist/spec-jest",
Copy link
Owner

Choose a reason for hiding this comment

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

We don't need this jest script here because it's handled by the example, so let's remove it.

package.json Outdated
"examples": "cd examples && npm test",
"example_1:watch": "npm run build && cd examples && npm run example_1:watch",
"example_2:watch": "npm run build && cd examples && npm run example_2:watch",
"example_3:watch": "npm run build && cd examples && npm run example_3:watch",
"test": "npm run build && npm run mocha && npm run examples"
"test": "npm run build && npm run mocha && npm run jest && npm run examples"
Copy link
Owner

Choose a reason for hiding this comment

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

We don't need to run jest here at the top level because it gets run by the examples, so let's remove it from the test script.

@@ -6,12 +6,13 @@
"repository": "https://github.com/suchipi/chai-jest-snapshot",
"scripts": {
"build": "rm -rf dist/* && babel src --out-dir dist",
"mocha": "mocha -r 'dist/spec/index' 'dist/**/*.spec.js'",
"jest": "jest --roots dist/spec-jest",
"mocha": "mocha -r 'dist/spec/index' 'dist/spec/*.spec.js'",
"examples": "cd examples && npm test",
"example_1:watch": "npm run build && cd examples && npm run example_1:watch",
"example_2:watch": "npm run build && cd examples && npm run example_2:watch",
"example_3:watch": "npm run build && cd examples && npm run example_3:watch",
Copy link
Owner

Choose a reason for hiding this comment

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

Could you add a script for example_4:watch here like the other examples have?

"example_4:watch": "npm run build && cd examples && npm run example_4:watch",

package.json Outdated
@@ -34,6 +35,7 @@
"babel-preset-es2015": "6.9.0",
"babel-preset-react": "6.11.1",
"chai": "3.5.0",
"jest": "20.0.4",
Copy link
Owner

Choose a reason for hiding this comment

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

Since jest isn't used by the project's tests, just the examples, could you move this devDependency to examples/package.json?

README.md Outdated

chai.use(chaiJestSnapshot);
```

### Framework-agnostic Configuration Mode (Recommended for Non-Mocha Users)
Copy link
Owner

Choose a reason for hiding this comment

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

Let's update this to say "Recommended for Non-Mocha/Jest Users"

README.md Outdated

chai.use(chaiJestSnapshot);
```

### Framework-agnostic Configuration Mode (Recommended for Non-Mocha Users)
If you are not using mocha as your test runner, it is recommended to use chai-jest-snapshot in "framework-agnostic configuration mode".
Copy link
Owner

Choose a reason for hiding this comment

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

Let's update this to say "If you are not using mocha or jest as your test runner"


const thisRunsInJest = () => (
typeof jest === "object" &&
JEST_MARKERS.every((marker) => typeof jest[marker] === "function")
Copy link
Owner

Choose a reason for hiding this comment

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

@cpojer is there a more canonical way to detect that we're running in jest? Or is this fine?

Copy link
Owner

Choose a reason for hiding this comment

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

(JEST_MARKERS is:

const JEST_MARKERS = ["enableAutomock", "genMockFromModule", "clearAllMocks", "runAllTicks"];

)

@tdd
Copy link
Contributor Author

tdd commented Jul 14, 2017

Hey @suchipi I just pushed a commit addressing all your concerns.

As for canonical Jest detection, I couldn't find anything on Google or Jest's site to begin with, hence this home-baked heuristic. I intentionally focused on methods not likely to be found in other test runners (plus, the object is "jest", so…).

@suchipi
Copy link
Owner

suchipi commented Jul 14, 2017

Looks great, thank you!

@suchipi
Copy link
Owner

suchipi commented Jul 14, 2017

I will pull this and confirm it works on my machine, then merge and cut a version.

@suchipi suchipi merged commit cc36edc into suchipi:master Jul 14, 2017
@suchipi
Copy link
Owner

suchipi commented Jul 14, 2017

This has been published in v1.3.0. Thank you for the contribution! 🎉

@suchipi suchipi mentioned this pull request Jul 14, 2017
@tdd
Copy link
Contributor Author

tdd commented Jul 14, 2017 via email

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.

2 participants