window
| library | version
| ------------------- | -------
| enzyme | "3.6.0"
I followed the documentation and an error occurred.
// create a div in the document to mount into
const div = global.document.createElement('div');
global.document.body.appendChild(div);
// div is empty. body has the div attached.
expect(document.body.childNodes).to.have.lengthOf(1);
expect(div.childNodes).to.have.lengthOf(0);
error:

my code
describe('document', () => {
it('should render correct', () => {
const div = global.document.createElement('div')
global.document.body.appendChild(div)
expect(document.body.childNodes).to.have.lengthOf(1)
expect(div.childNodes).to.have.lengthOf(0)
})
})
Is not .to.have.lengthOf a api of jest???
I find .to.have.lengthOf is a api of chain https://www.chaijs.com/api/bdd/
Yes, we use chai's expect exclusively.
Jest can be used without Enzyme to render components and test with snapshots, Enzyme simply adds additional functionality.
Enzyme can be used without Jest, however Enzyme must be paired with another test runner if Jest is not used (in the documentation they use chai as the test runner as i know)
so in react , when you using create-react-app in your react Application ,it use Jest as unit testing runner in its configuration .. so you must use :
Jest as the test runner, assertion library, and mocking library
Enzyme to provide additional testing utilities to interact with elements
so in your case you do this :
describe('document', () => {
it('should render correct', () => {
const div = global.document.createElement('div')
global.document.body.appendChild(div)
const wrapper=shallow( global.document.body)
expect(wrapper.length).toBe(1)
expect(wrapper.find('div').shallow.length).toBe(0)
})
})
and you can optimize it with your needs , but i recommand you to use shallow and read this article
https://medium.com/codeclan/testing-react-with-jest-and-enzyme-20505fec4675
if i'm wrong please reply to me , because i'm currently try to learn and use enzyme and that is what i found.
thank u
@ElagrebiAli chai is an assertion library; by using it, we can use jest or mocha interchangeably on all of our tests and examples.
first of all ,thank you for ur respond .
yeah , you are right , i was wrong when i see "they use chai as the test runner"
but what i mean exactly ,when can use only the expectation of jest , we don't need to configure or install chai .. as i know jest do its best for assertion .
and thank you for ur patience .
Right - we use chai explicitly so that we aren't locked in to jest, and can switch at any time.
Great .i got it now .
thank you for your understanding @ljharb
might be helpful to add note in documentation that the example is using 'chai assertions, not jest', most people will be using jest so it causes confusion
“most people will be using jest” is both untrue and impossible to prove; it would cause confusion either way.
Most helpful comment
might be helpful to add note in documentation that the example is using 'chai assertions, not jest', most people will be using jest so it causes confusion