You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue中用Vitest测试Pinia插件时store.$subscribe未按预期工作

Pinia撤销/重做插件测试问题

我正在开发一个Pinia插件,用来给store实现基础动量模式的撤销/重做功能。插件在应用里运行正常,但测试无法正常执行。

测试前置代码如下:

describe('UndoRedoPlugin', () => {
  beforeEach(() => {
    const app = createApp({});
    const pinia = createPinia();
    pinia.use(UndoRedoPlugin);
    app.use(pinia);
    setActivePinia(pinia);
  });
});

测试时我定义了一个简单的计数器store,调用它的increment动作理应触发插件更新,但实际插件似乎未被调用,且store的响应性存在问题。测试代码如下:

it('Allows undo when store is modified', () => {
    const useCounterStore = defineStore('counter', {
      state: () => ({ count: 0 }),
      actions: {
        increment() {
          this.count++;
        },
      },
      undoRedoPluginSettings: { max: 3 },
    });

    const counter = useCounterStore();

    expect(counter.canUndo).toBe(false);

    console.log('increment 1');
    counter.increment();
    expect(counter.count).toBe(1);

    console.log('increment 2');
    counter.increment();
    expect(counter.count).toBe(2);

    // 这里应该为true
    expect(counter.canUndo).toBe(true);
  });

运行测试只需在终端执行命令 npm run test

内容的提问来源于stack exchange,提问作者V. Sambor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 07:01:16