Jest + Preact
See original GitHub issueI 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:
FAIL src/widget/index.test.js
● Test suite failed to run
Cannot find module 'react/lib/ReactComponentTreeHook' from 'ReactDebugTool.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:169:17)
at Object.<anonymous> (node_modules/react-test-renderer/lib/ReactDebugTool.js:16:30)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.601s
My setup
I followed the guidelines to setup babel with webpack. My .babelrc is
{
"plugins": [
["transform-react-jsx", { "pragma":"h" }],
"transform-class-properties"
],
"presets": [
["es2015", {
"modules": false
}]
],
"env": {
"test": {
"plugins": [
"transform-es2015-modules-commonjs",
["transform-react-jsx", { "pragma":"h" }],
"transform-class-properties"
]
}
}
}
package.json uses identity-obj-proxy
as suggested
"jest": {
"moduleNameMapper": {
"\\.(css)$": "identity-obj-proxy"
}
}
And finally my test looks like
import Widget from './index';
import renderer from 'react-test-renderer';
test('Tests for widget to have been rendered', () => {
const tree = renderer.create(
<Widget href="http://test.com" target="_blank" data-variation="test"
innerHTML="<b>test</b> foo bar"/>
).toJSON();
expect(tree).toMatchSnapshot();
});
I am losing my mind. I added react
but then I get a bunch of other errors.
Questions
- Should
react-test-renderer
work with preact? - Is there a better way to do this?
Issue Analytics
- State:
- Created 7 years ago
- Comments:18 (7 by maintainers)
Top Results From Across the Web
Testing with Preact Testing Library
The Preact Testing Library is a lightweight wrapper around preact/test-utils . It provides a set of query methods for accessing the rendered DOM...
Read more >preactjs/jest-preset-preact - GitHub
Jest preset containing all required configuration for writing tests for preact. Features: Transpiles JSX to h(); Aaliases for "react" imports to point to...
Read more >jest-preset-preact - npm
Jest preset for testing Preact apps. Latest version: 4.0.5, last published: a year ago. Start using jest-preset-preact in your project by ...
Read more >Writing Unit Test Cases for Preact - Stack Overflow
They're using jest to run the tests. Below is, as a I understand, the relevant parts to get up and running. package.json "jest":...
Read more >API - Testing Library
import {render} from '@testing-library/preact' ... Keep in mind mainly when using h / Preact. ... const cb = jest.fn() function Counter() {
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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:
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
preact-compat
.Got it working using this jest config in my package.json:
and this
.babelrc
:The slight difference is in the regex for aliasing
react
andreact-dom
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.