使用 jest 进行测试时,我看到该属性innerText未定义,而未在测试中它具有正确的值。
  it('get text from div', () => {
    const div = document.createElement('DIV')
    div.innerHTML = '<br>a<br>b<br>c'
    console.log('innerText', div.innerText) // undefined
    console.log('textContent', div.textContent) // 'abc'
    // expect(getTextFromDiv(div).length).toMatchSnapshot()
  })
但是当在笑话测试中使用相同的代码时,innerText 显示:
'a
b
c'
和 textContent 是'abc'.
为什么在玩笑中的innerText 是未定义的,当它不是在玩笑中时,它的值是真实的?
这是它工作的代码(不是开玩笑):
const addTextInRichTextToPdf = (doc, text, offsetY) => {
  const div = document.createElement('DIV')
  div.innerHTML = '<br>a<br>b<br>c'
  console.log('innerText', div.innerText) // print the real value
  console.log('textContent', div.textContent) // 'abc'
  ...