question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Snapshot testing returning empty object with jest and preact

See original GitHub issue

Current behavior

shallow and mount are returning ShallowWrapper{} and ReactWrapper{} when used with jest and preact. I’ve used enzyme-with-json for serialization, which returns the expected result, but expect fails when used with toMatchSnapshot since it compares it with Empty Object.

import { shallowToJson } from 'enzyme-to-json';
import { shallow } from 'enzyme';
import Loader from './index';

describe('Loader Component', () => {
  it('should render correctly', () => {
    const component = shallow(<Loader />);
    console.log(component); // returns ShallowWrapper{}
    expect(shallowToJson(component)).toMatchSnapshot();
  });
});

Expected behavior

The snapshot should return correct output of the contents of <Loader /> component, and the test should pass.

Content received by the test are :

<div className="loader u-RTLPositive" style={Object {"width": "30px"}}>
      <svg className="loader__circular" viewBox="25 25 50 50">
        <circle className="loader__path" cx="50" cy="50" r="20" fill="none" strokeMiterlimit="10" />
      </svg>
 </div>

And Snapshot, to which it is compared is : ShallowWrapper {}

Your environment

“preact” : 10.3.3, “jest”: 26.0.1, “jest-enzyme”: 7.1.2 “enzyme-to-json”: 3.5.0

Here’s my setup file provided to Jest :

import { configure } from 'enzyme';
import 'jest-enzyme';
import Adapter from 'enzyme-adapter-preact-pure';

configure({ adapter: new Adapter() });

API

  • [✔️] shallow
  • [✔️] mount
  • [✔️] render

Version

library version
enzyme 3.11.0
react N/A
react-dom N/A
react-test-renderer N/A
adapter (below)

Adapter

  • [✔️ ] others (enzyme-adapter-preact-pure : 2.2.0 )

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
saher-elgendycommented, Sep 23, 2020

@cadence09 thank you but i am trying for two days now, tried everything possible and the solution was just to press u after the the snapshot fail in the first time!!!

0reactions
ljharbcommented, Sep 23, 2020

Since this seems to be an issue with whatever snapshotting tool you’re using, and not with enzyme, I’ll close this.

(I’d suggest avoiding snapshot testing - in practice, devs just end up rubber-stamping them, and they aren’t actually testing things about your components that are important)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Snapshot testing returning empty object with enzyme, jest and ...
I am using combination of Enzyme and Jest to do snapshot testing in Preact project shallow and mount are returning ShallowWrapper{} and ...
Read more >
SOLVED Empty ShallowWrapper Snapshot Object in Jest and ...
In my code, I am working with Jest v26 and Enzyme v3. In unit testing, I am doing snapshot testing with a code...
Read more >
What's wrong with snapshot tests. Since snapshot ... - Medium
When you write expect(mycomponent).toMatchSnapshot() , Jest creates a snapshot file, like mytest.spec.js.snap , with all snapshots of your test file. You ...
Read more >
@storybook/addon-storyshots - npm
To use StoryShots, you must use your existing Storybook stories as the input for Jest Snapshot Testing. Getting Started. Add the following ...
Read more >
@shopify/preact-testing - npm package | Snyk
Takes an object of options passed by a user of your custom mount (or an empty object), and should return an object containing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found