-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Jest + Preact #560
Comments
Possibly related to preactjs/preact-compat#249 and enzymejs/enzyme#715 |
I'd love an experimental PR for JSON output if anyone has the time - I haven't had time yet since I'm only just looking into Jest myself. FWIW I think most people test Preact components using the preact-jsx-chai plugin. It uses expect(
<Things items={['a', 'b']} />
).to.equal(
<div class="things">
<Item name="a" />
<Item name="b" />
</div>
); (it also supports shallow rendering via |
Great. This helps a lot. I'll close ticket. If I get time to play around with the code then I'll try sending a PR. |
Might help someone: I've been testing my Preact components with Jest and html-looks-like. It works fine and is pretty easy to set up. import { h } from 'preact';
import render from 'preact-render-to-string';
import htmlLooksLike from 'html-looks-like';
const Thing = () => (
<div class="some-class">
<span>Nope</span>
<span>Nope</span>
<span>Yep</span>
</div>
);
describe('Thing', () => {
it('has Yep', () => {
const actual = render(<Thing />);
const expected = `
<div>
{{ ... }}
<span>Yep</span>
</div>
`;
htmlLooksLike(actual, expected);
});
}); |
That is a ridiculously cool technique @rmsaksida - will be stealing it for some of my stuff haha. I love the syntax for ignoring parts of the tree! |
For anyone interested in using Jest for Preact environments, I've set up a PR for this at |
Anyone could test components that use react components as childrens? I'm having a good trouble with |
@armand1m shouldn't make a difference as long as you're aliasing preact-compat. |
@developit So, I'm aliasing react with preact-compat into my .babel-rc and webpack.config.js. When bundling the project, everything were working nice. When running it with jest, it could render simple elements that didn't use third-party react components nicely, but when trying to run a specific component using react-select, it was asking for react to render it. I started having this problem with 'react-intl', have migrated to 'preact-intl' and have trespassed this problem. Then I started having problems with 'react-select'. When trying to alias react into my package.json, inside the "jest" key, every test case was broken. It was trowing an error like:
I didn't had much time and mocked the 'react-select' to render what I needed from my component. I can send more detailed information later, but I couldn't find other easy way to render this component into a jest test harness. |
Ah - that error makes it seem like your JSX Pragma setting is being applied to all of |
@developit Nice, I will try it later and give some feedback after. Thank you! |
@developit Got it working. It was actually because when you specify a module to jest configuration in package.json, you're specifying actually a Regexp. When using like this: {
"jest": {
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less|scss)$": "identity-obj-proxy",
"react": "preact-compat",
"react-dom": "preact-compat"
},
"collectCoverageFrom": [
"app/**/*.{js,jsx}"
],
"moduleFileExtensions": [
"js",
"jsx"
],
"moduleDirectories": [
"node_modules"
]
}
} It will not even run a component, because it is looking actually at every component that has "react", for example, in its name, and replacing their calls to Got it working using this jest config in my package.json: {
"jest": {
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less|scss)$": "identity-obj-proxy",
"^react$": "preact-compat",
"^react-dom$": "preact-compat"
},
"collectCoverageFrom": [
"app/**/*.{js,jsx}"
],
"moduleFileExtensions": [
"js",
"jsx"
],
"moduleDirectories": [
"node_modules"
]
}
} and this {
"sourceMaps": true,
"presets": [
["es2015", {
"es2015": {
"loose": true,
"modules": false
}
}],
"stage-0"
],
"plugins": [
["transform-decorators-legacy"],
["transform-react-jsx", { "pragma":"h" }]
]
} The slight difference is in the regex for aliasing |
@rmsaksida That looks awesome! Do you have any repository with the whole setup that you can share? Thanks! |
would love to see where he's used it too! |
I have been using preact-render-to-string like the example over here + jest-serializer-html-string to sanitize html output with quite some success until now |
@ruyadorno that's a really nice solution. going to give it a try and maybe integrate it into the boilerplate! |
@armand1m thanks. It works like a charm |
@haikyuu I'm glad it helped you mate 😄 |
I bet this question has been asked a lot. I have searched for two hours and I am not able to find a solution. There doesn't seem to be much about
preact
with jest testing. I looked at the boilerplate and it uses Karma.Problem
When trying to use Jest with Preact I get the following error:
My setup
I followed the guidelines to setup babel with webpack. My .babelrc is
package.json uses
identity-obj-proxy
as suggestedAnd finally my test looks like
I am losing my mind. I added
react
but then I get a bunch of other errors.Questions
react-test-renderer
work with preact?The text was updated successfully, but these errors were encountered: