为什么断言不相等?

Why is the assertion not equal?

我正在尝试用 chai 测试我的 react reducer:

const initialState = {
    cartOpen: false
}

const Cart = (state = initialState, action) => {
    switch (action.type) {

        case 'INITIALIZE_CART':
           return 1

        default:
            return state
    }
}

这是我的测试:

it('should return the initial state', () => {
    expect(
        reducer(undefined, {})
    ).to.equal(
        { cartOpen: false }
    )
})

我不明白为什么会出现此错误:

cart reducer should return the initial state:                                                       

 AssertionError: expected { cartOpen: false } to equal { cartOpen: false }                          
 + expected - actual                                                                                


 at Context.<anonymous> (C:/Usersdevelop_5dec/foss/foss-frontend/test/cart.spec.js:11:14)  

看起来预期和实际一样?我该如何解决这个问题?

它们是在两个不同的地方创建的两个不同的对象引用。 .equal() 在后台使用 ===,比较引用。

如果您使用的是 chai,则可以使用 .deep

expect(
    reducer(undefined, {})
).to.deep.equal((
    { cartOpen: false }
)