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.

assertion of style() for shallow()/render()

See original GitHub issue

I think there is bug (or perhaps a mis-documentation) of the style() assertion, namely that it only works if you mount() the component.

describe("Chai-enzyme style test", function() {

    it("should do style test on shallow component", function() {
        const component = (
                    <p style={{color:"red"}}>Hello, World</p>
        );
        const wrapper = shallow(component);
        expect(wrapper).to.have.style("color", "red");
    });

    it("should do style test on rendered component", function() {
        const component = (
            <p style={{color:"red"}}>Hello, World</p>
        );
        const wrapper = render(component);
        expect(wrapper).to.have.style("color", "red");
    });

    it("should do style test on mounted component", function() {
        const component = (
            <p style={{color:"red"}}>Hello, World</p>
        );
        const wrapper = mount(component);
        expect(wrapper).to.have.style("color", "red");
    });
});

Result is

Chai-enzyme style test
    ✖ should do style test on shallow component
    ✖ should do style test on rendered component
    ✔ should do style test on mounted component

Finished in 0.025 secs / 0.017 secs

SUMMARY:
✔ 1 test completed
✖ 2 tests failed

FAILED TESTS:
  Chai-enzyme style test
    ✖ should do style test on shallow component
      PhantomJS 2.1.1 (Mac OS X 0.0.0)
    Window is not a constructor (evaluating '(0, _cheerio2.default)(this.wrapper.html())')
    get@/Users/.../test.spec.js:38342:44 <- webpack:///~/chai-enzyme/build/ShallowTestWrapper.js:127:0
    style@/Users/.../test.spec.js:38276:19 <- webpack:///~/chai-enzyme/build/ShallowTestWrapper.js:61:0
    /Users/.../test.spec.js:37455:37 <- webpack:///~/chai-enzyme/build/assertions/generic.js:17:0
    /Users/...test.spec.js:37907:24 <- webpack:///~/chai-enzyme/build/ChaiWrapper.js:199:0
    /Users/.../test.spec.js:32360:31 <- webpack:///~/chai/lib/chai/utils/addMethod.js:41:0
    /Users/.../test.spec.js:78:50 <- webpack:///test.spec.js:16:38

    ✖ should do style test on rendered component
      PhantomJS 2.1.1 (Mac OS X 0.0.0)
    undefined is not a constructor (evaluating '_cheerio2.default.load(html)')
    render@/Users/.../test.spec.js:19762:33 <- webpack:///~/enzyme/build/index.js:61:0
    /Users/.../test.spec.js:88:43 <- webpack:///test.spec.js:25:31

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
ItsCosmocommented, Mar 23, 2016

@dpoindexter: Thanks for a very helpful response. This solved my issue. Specifically, I removed the line: cheerio': 'window',

from externals configuration, and added the line: { test: /\.json$/, loader: "json"}

to the module.loaders configuration

5reactions
dpoindextercommented, Mar 16, 2016

Were you using Webpack to do module bundling for karma, by any chance? I encountered essentially the same issue when using the checked assertion on a shallow render.

Enzyme’s documentation suggests configuring Webpack to treat Cheerio as an external module, which was the root cause of my problem. I was able to get it working by resolving Cheerio properly using Webpack’s JsonLoader.

If you’re not using Webpack, I don’t know what the problem is, sorry! But maybe someone else will come across this issue for the same reason I did.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shallow Renderer - React
Shallow rendering lets you render a component “one level deep” and assert facts ... shallowRenderer.render() is similar to root.render() but it doesn't ...
Read more >
Better assertions for shallow-rendered React components
The React TestUtils shallow rendering feature allows us to test React components in true isolation from other component classes, and removes the ...
Read more >
Shallow Rendering API - Enzyme - GitHub Pages
Shallow renders the current node and returns a shallow wrapper around it. .render() => CheerioWrapper. Returns a CheerioWrapper of the current node's subtree....
Read more >
Can I, (and should I), make assertions about what HTML ...
In order to test that the component works, I want to assert that it will render an HTML element <input/> , and I...
Read more >
enzymejs/react-shallow-renderer - GitHub
After shallowRenderer.render() has been called, you can use shallowRenderer.getRenderOutput() to get the shallowly rendered output. You can then begin to assert ...
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