每次测试前酶还原getEelemenById

时间:2018-07-01 18:32:41

标签: javascript sinon enzyme sinon-chai chai-enzyme

我在getElementById中存根了beforeEach,并希望在另一个测试之前将其恢复,然后再次使用另一个返回值。因为现在我收到错误 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})
        })
    })

1 个答案:

答案 0 :(得分:0)

尝试添加:

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

进入每个describe(...)