How can I get component contains with shallow?












0















I'm trying to use Enzyme and shallow to check component contains. for example in this sample test I have this error :



My component code :






import React, { Component } from 'react';

class Home extends Component {
render() {
return(
<div>
<h1>Home</h1>
</div>
)
}
}

export default Home;





My test code :






import React from 'react';
import { render } from 'react-dom';
import Home from '../Home';
import { shallow } from 'enzyme';

test('checking title in component', function() {
const wrapper = shallow(<Home />);
const title = <h1>Home</h1>;
expect(wrapper.contains(title)).to.equal(true);
});





Error :






 FAIL  src/__tests__/Home.test.js
× checking title in component (15ms)

● checking title in component

TypeError: Cannot read property 'equal' of undefined

7 | const wrapper = shallow(<Home />);
8 | const title = <h1>Home</h1>;
> 9 | expect(wrapper.contains(title).to.equal(true));
| ^
10 | });





So, how can I check the contains ? It's the main tutorial of CRA package ...



Consider it, I have set the enzyme react-adapter-16










share|improve this question




















  • 3





    .toEqual() instead of to.equal

    – Boy With Silver Wings
    Nov 17 '18 at 5:48






  • 1





    I think you didn't save the latest test file and run the test. Because the error shows the wrong code with your original code you have added like expect(wrapper.contains(title).to.equal(true)). But the bracket should be close before to like in your original code expect(wrapper.contains(title)).to.equal(true)

    – kumar k
    Nov 17 '18 at 6:27


















0















I'm trying to use Enzyme and shallow to check component contains. for example in this sample test I have this error :



My component code :






import React, { Component } from 'react';

class Home extends Component {
render() {
return(
<div>
<h1>Home</h1>
</div>
)
}
}

export default Home;





My test code :






import React from 'react';
import { render } from 'react-dom';
import Home from '../Home';
import { shallow } from 'enzyme';

test('checking title in component', function() {
const wrapper = shallow(<Home />);
const title = <h1>Home</h1>;
expect(wrapper.contains(title)).to.equal(true);
});





Error :






 FAIL  src/__tests__/Home.test.js
× checking title in component (15ms)

● checking title in component

TypeError: Cannot read property 'equal' of undefined

7 | const wrapper = shallow(<Home />);
8 | const title = <h1>Home</h1>;
> 9 | expect(wrapper.contains(title).to.equal(true));
| ^
10 | });





So, how can I check the contains ? It's the main tutorial of CRA package ...



Consider it, I have set the enzyme react-adapter-16










share|improve this question




















  • 3





    .toEqual() instead of to.equal

    – Boy With Silver Wings
    Nov 17 '18 at 5:48






  • 1





    I think you didn't save the latest test file and run the test. Because the error shows the wrong code with your original code you have added like expect(wrapper.contains(title).to.equal(true)). But the bracket should be close before to like in your original code expect(wrapper.contains(title)).to.equal(true)

    – kumar k
    Nov 17 '18 at 6:27
















0












0








0








I'm trying to use Enzyme and shallow to check component contains. for example in this sample test I have this error :



My component code :






import React, { Component } from 'react';

class Home extends Component {
render() {
return(
<div>
<h1>Home</h1>
</div>
)
}
}

export default Home;





My test code :






import React from 'react';
import { render } from 'react-dom';
import Home from '../Home';
import { shallow } from 'enzyme';

test('checking title in component', function() {
const wrapper = shallow(<Home />);
const title = <h1>Home</h1>;
expect(wrapper.contains(title)).to.equal(true);
});





Error :






 FAIL  src/__tests__/Home.test.js
× checking title in component (15ms)

● checking title in component

TypeError: Cannot read property 'equal' of undefined

7 | const wrapper = shallow(<Home />);
8 | const title = <h1>Home</h1>;
> 9 | expect(wrapper.contains(title).to.equal(true));
| ^
10 | });





So, how can I check the contains ? It's the main tutorial of CRA package ...



Consider it, I have set the enzyme react-adapter-16










share|improve this question
















I'm trying to use Enzyme and shallow to check component contains. for example in this sample test I have this error :



My component code :






import React, { Component } from 'react';

class Home extends Component {
render() {
return(
<div>
<h1>Home</h1>
</div>
)
}
}

export default Home;





My test code :






import React from 'react';
import { render } from 'react-dom';
import Home from '../Home';
import { shallow } from 'enzyme';

test('checking title in component', function() {
const wrapper = shallow(<Home />);
const title = <h1>Home</h1>;
expect(wrapper.contains(title)).to.equal(true);
});





Error :






 FAIL  src/__tests__/Home.test.js
× checking title in component (15ms)

● checking title in component

TypeError: Cannot read property 'equal' of undefined

7 | const wrapper = shallow(<Home />);
8 | const title = <h1>Home</h1>;
> 9 | expect(wrapper.contains(title).to.equal(true));
| ^
10 | });





So, how can I check the contains ? It's the main tutorial of CRA package ...



Consider it, I have set the enzyme react-adapter-16






import React, { Component } from 'react';

class Home extends Component {
render() {
return(
<div>
<h1>Home</h1>
</div>
)
}
}

export default Home;





import React, { Component } from 'react';

class Home extends Component {
render() {
return(
<div>
<h1>Home</h1>
</div>
)
}
}

export default Home;





import React from 'react';
import { render } from 'react-dom';
import Home from '../Home';
import { shallow } from 'enzyme';

test('checking title in component', function() {
const wrapper = shallow(<Home />);
const title = <h1>Home</h1>;
expect(wrapper.contains(title)).to.equal(true);
});





import React from 'react';
import { render } from 'react-dom';
import Home from '../Home';
import { shallow } from 'enzyme';

test('checking title in component', function() {
const wrapper = shallow(<Home />);
const title = <h1>Home</h1>;
expect(wrapper.contains(title)).to.equal(true);
});





 FAIL  src/__tests__/Home.test.js
× checking title in component (15ms)

● checking title in component

TypeError: Cannot read property 'equal' of undefined

7 | const wrapper = shallow(<Home />);
8 | const title = <h1>Home</h1>;
> 9 | expect(wrapper.contains(title).to.equal(true));
| ^
10 | });





 FAIL  src/__tests__/Home.test.js
× checking title in component (15ms)

● checking title in component

TypeError: Cannot read property 'equal' of undefined

7 | const wrapper = shallow(<Home />);
8 | const title = <h1>Home</h1>;
> 9 | expect(wrapper.contains(title).to.equal(true));
| ^
10 | });






reactjs tdd jestjs enzyme






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 8:42









skyboyer

3,57611128




3,57611128










asked Nov 17 '18 at 5:32









irbglsirbgls

83




83








  • 3





    .toEqual() instead of to.equal

    – Boy With Silver Wings
    Nov 17 '18 at 5:48






  • 1





    I think you didn't save the latest test file and run the test. Because the error shows the wrong code with your original code you have added like expect(wrapper.contains(title).to.equal(true)). But the bracket should be close before to like in your original code expect(wrapper.contains(title)).to.equal(true)

    – kumar k
    Nov 17 '18 at 6:27
















  • 3





    .toEqual() instead of to.equal

    – Boy With Silver Wings
    Nov 17 '18 at 5:48






  • 1





    I think you didn't save the latest test file and run the test. Because the error shows the wrong code with your original code you have added like expect(wrapper.contains(title).to.equal(true)). But the bracket should be close before to like in your original code expect(wrapper.contains(title)).to.equal(true)

    – kumar k
    Nov 17 '18 at 6:27










3




3





.toEqual() instead of to.equal

– Boy With Silver Wings
Nov 17 '18 at 5:48





.toEqual() instead of to.equal

– Boy With Silver Wings
Nov 17 '18 at 5:48




1




1





I think you didn't save the latest test file and run the test. Because the error shows the wrong code with your original code you have added like expect(wrapper.contains(title).to.equal(true)). But the bracket should be close before to like in your original code expect(wrapper.contains(title)).to.equal(true)

– kumar k
Nov 17 '18 at 6:27







I think you didn't save the latest test file and run the test. Because the error shows the wrong code with your original code you have added like expect(wrapper.contains(title).to.equal(true)). But the bracket should be close before to like in your original code expect(wrapper.contains(title)).to.equal(true)

– kumar k
Nov 17 '18 at 6:27














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348543%2fhow-can-i-get-component-contains-with-shallow%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348543%2fhow-can-i-get-component-contains-with-shallow%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini