-
Notifications
You must be signed in to change notification settings - Fork 17
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
Conversation
Hey @suchipi! Would love your feedback on this. Best, |
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 |
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 😉 |
Also re: Jest+Chai aliasing Jest's |
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.
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.
src/buildMatchSnapshot.js
Outdated
@@ -33,4 +42,19 @@ const buildMatchSnapshot = (utils, parseArgs) => { | |||
}; | |||
}; | |||
|
|||
const safeRequireJestExpect = () => { | |||
try { | |||
return require("jest-matchers"); |
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.
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;
}
src/spec-jest/index.spec.js
Outdated
@@ -0,0 +1,25 @@ | |||
import chai, { expect } from "chai"; |
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 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", |
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.
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" |
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.
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", |
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 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", |
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.
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) |
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.
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". |
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.
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") |
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.
@cpojer is there a more canonical way to detect that we're running in jest? Or is this fine?
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.
(JEST_MARKERS is:
const JEST_MARKERS = ["enableAutomock", "genMockFromModule", "clearAllMocks", "runAllTicks"];
)
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…). |
Looks great, thank you! |
I will pull this and confirm it works on my machine, then merge and cut a version. |
This has been published in v1.3.0. Thank you for the contribution! 🎉 |
'most welcome!
…On Fri, Jul 14, 2017, at 09:18, Stephen Scott wrote:
This has been published in v1.3.0. Thank you for the contribution! 🎉> —
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub[1], or mute the thread[2]. >
--
Christophe Porteneuve
[email protected]
Links:
1. #23 (comment)
2. https://github.com/notifications/unsubscribe-auth/AAAOqLbRTXNFm_z26krPi__z_hDl8iNUks5sNxZSgaJpZM4OSmpE
|
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,