0

I stub getElementById in beforeEach, and want to restore it before another test and stub again with anothter returns value. Because now I recieve error TypeError: Attempted to wrap getElementById which is already wrapped

 let loginUrl = 'loginUrl'
    const url = '/app/auth'
    const textContent = `{"${loginUrl}":"${url}"}`
    let htmlDecode

    describe('identityServer', () => {

        beforeEach(() => {
            htmlDecode = sinon.stub().returns(textContent)
            sinon.stub(document, 'getElementById').returns({textContent})
            sinon.stub(htmlEncoder, 'Encoder').returns({htmlDecode: () => htmlDecode})

            identityServerModel()
        })

        it('should return correct model for IdentityServer', () => {
            window.identityServer.getModel().should.deep.equal({[loginUrl]: url})
        })
    })

    describe('identityServer', () => {

        beforeEach(() => {
            htmlDecode = sinon.stub().returns(textContent)
            sinon.stub(document, 'getElementById').returns({innerHTML: textContent})
            sinon.stub(htmlEncoder, 'Encoder').returns({htmlDecode: () => htmlDecode})

            identityServerModel()
        })
        it('should return correct model using serialization HTML from innerHTML property when textContent is undefined', () => {
            window.identityServer.getModel().should.deep.equal({[loginUrl]: url})
        })
    })
Palaniichuk Dmytro
  • 2,943
  • 12
  • 36
  • 66

1 Answers1

0

Try add:

afterEach(() => {
    document.getElementById.restore();
})

into every describe(...).

Alejandro
  • 5,834
  • 1
  • 19
  • 36