Mobx 连续获取计算值产生不同的引用

Mobx consecutive getting computed values produces different references

class Store {
    @computed get staticItems(): number[] {
        return [1, 2, 3]
    }
}

describe('mobx', () => {
    it('computed static items should be same', (done) => {
        let store = new Store();
        let items = store.staticItems;
        setTimeout(() => {
            expect(items).toBe(store.staticItems);
            done()
        }, 500);
    });
})

使用 jest 进行测试会抛出

Expected: [1, 2, 3]
    Received: serializes to the same string

      50 |         let items = store.staticItems;
      51 |         setTimeout(() => {
    > 52 |             expect(items).toBe(store.staticItems);

此测试通过toEqual但失败toBe,这意味着两个引用都指向不同的对象。我错过了什么?使用 React 这将不必要地更改子组件的道具。

MobX 计算值在反应中未使用时会自动暂停。来自 documentation:

This automatic suspension is very convenient. If a computed value is no longer observed, for example the UI in which it was used no longer exists, MobX can automatically garbage collect it. This differs from autorun's values where you must dispose of them yourself. It sometimes confuses people new to MobX, that if you create a computed property but don't use it anywhere in a reaction, it will not cache its value and recompute more often than seems necessary. However, in real life situations this is by far the best default, and you can always forcefully keep a computed value awake if you need to, by using either observe or keepAlive.