Babel const 防止突变?

Babel const prevents mutation?

我正在使用由 Babel 提供支持的 ES6 语法编写测试,并且我正在使用 Jest 运行 测试。这是我的测试代码:

test('case: response body contains a user, should update sessionData', () => {
  // current user info in the session
  const currentUserInfo = {
    firstName: 'User1',
    lastName: 'Old Info'
  }

  const mockSessionData = createSessionHelper(
    {
      data: {
        user: currentUserInfo
      }
    },
    mockLogger
  )

  // new user info in the body
  const newUserInfo = {
    firstName: 'User2',
    lastName: 'New Info'
  }

  const body = {
    user: newUserInfo
  }

  const result = handleSuccess({
    req: {
      sessionData: mockSessionData
    },
    body
  })

  expect(result.status).toBe(DEFAULT_SUCCESS_STATUS)
  expect(result.body).toEqual(body)
  expect(mockSessionData.get('user')).toEqual(newUserInfo)
})

如您所见,我使用相同的对象既用作我正在测试的函数的参数,又使结果与我的预期相匹配。

我在这里假设这些函数不应该改变那些参数。不过,这个假设是真的吗?

通过使用 Babel const,我的数据会保持不变,还是应该继续使用 Object.freeze()

正如the reference所说,

The value of a constant cannot change through re-assignment, and it can't be redeclared.

mockSessionData 无法重新分配。 const 不能也不应该阻止 mockSessionData 持有引用的对象被修改。如果不应更改对象,则可以使用 Object.freeze