Sinon可以与vitest合并吗?

发布于 2025-02-08 03:43:57 字数 2248 浏览 3 评论 0原文

我要迁移到Vite和Vitest。现在,我正在学习如何使用vitest,突然间我发现Vitest在加载模块Sinon方面存在问题。

Sinon与Vitest兼容吗?

当我使用“ NPM Run测试:单位”进行测试时,它的响应如下:

FAIL  src/stores/__tests__/counter.spec.ts [ src/stores/__tests__/counter.spec.ts ]
Error: [vite-node] Failed to load sinon
 ❯ async src/stores/__tests__/counter.spec.ts:9:31
      7| describe('useCounterStore', () => {
      8|     beforeEach(() => {
      9|         setActivePinia(createPinia());
       |                               ^
     10|     });
     11| 

我知道Vitest建议使用MSW来模拟请求,但是现在我想尝试一下Sinon,因为我的同事已经熟悉Sinon。

这是测试文件counter.spec.ts:

import { describe, it, expect, beforeEach } from 'vitest';
import { useCounterStore } from '../counter';
import { setActivePinia, createPinia } from 'pinia';
import axios from 'axios';
import sinon from 'sinon';

describe('useCounterStore', () => {
    beforeEach(() => {
        setActivePinia(createPinia());
    });

    it.skip('Counter Store - increment()', () => {
        const counter = useCounterStore();
        expect(counter.counter).toBe(0);
        counter.increment();
        expect(counter.counter).toBe(1);
    });

    it('getName - axios success', () => {
        const counter = useCounterStore();
        sinon.stub(axios, 'get').resolves({ data: 'abc' });

        counter.getName();

        expect(counter.name).toEqual('abc');
    });
});

这是Counter.ts(Pinia Store)

import axios from 'axios';
import * as pinia from 'pinia';

export const useCounterStore = pinia.defineStore({
    id: 'counter',
    state: () => ({
        counter: 0,
        name: '',
    }),
    getters: {
        doubleCount: state => state.counter * 2,
    },
    actions: {
        increment() {
            this.counter++;
        },
        async getName() {
            const rlt = await axios
                .get('https://jsonplaceholder.typicode.com/todos/1')
                .then((response:any) => response.data)
                .catch(error => console.log(error));
            // console.log(rlt);
            this.name = rlt.data;
        },
    },
});


I'm going to migrate to vite and vitest. Now I'm learning how to use vitest and suddenly I found vitest has a problem loading module sinon.

Is sinon compatible with vitest?

When I run the test with 'npm run test:unit', it responds as follows:

FAIL  src/stores/__tests__/counter.spec.ts [ src/stores/__tests__/counter.spec.ts ]
Error: [vite-node] Failed to load sinon
 ❯ async src/stores/__tests__/counter.spec.ts:9:31
      7| describe('useCounterStore', () => {
      8|     beforeEach(() => {
      9|         setActivePinia(createPinia());
       |                               ^
     10|     });
     11| 

I know that vitest suggest using msw to mock request but for now I'd like to give sinon a try since my colleagues are already familiar with sinon.

Here is the test file counter.spec.ts:

import { describe, it, expect, beforeEach } from 'vitest';
import { useCounterStore } from '../counter';
import { setActivePinia, createPinia } from 'pinia';
import axios from 'axios';
import sinon from 'sinon';

describe('useCounterStore', () => {
    beforeEach(() => {
        setActivePinia(createPinia());
    });

    it.skip('Counter Store - increment()', () => {
        const counter = useCounterStore();
        expect(counter.counter).toBe(0);
        counter.increment();
        expect(counter.counter).toBe(1);
    });

    it('getName - axios success', () => {
        const counter = useCounterStore();
        sinon.stub(axios, 'get').resolves({ data: 'abc' });

        counter.getName();

        expect(counter.name).toEqual('abc');
    });
});

And here is the counter.ts (Pinia store)

import axios from 'axios';
import * as pinia from 'pinia';

export const useCounterStore = pinia.defineStore({
    id: 'counter',
    state: () => ({
        counter: 0,
        name: '',
    }),
    getters: {
        doubleCount: state => state.counter * 2,
    },
    actions: {
        increment() {
            this.counter++;
        },
        async getName() {
            const rlt = await axios
                .get('https://jsonplaceholder.typicode.com/todos/1')
                .then((response:any) => response.data)
                .catch(error => console.log(error));
            // console.log(rlt);
            this.name = rlt.data;
        },
    },
});


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑,眼淚并存 2025-02-15 03:43:57

最后,我关注 3种模拟axios in Jest 并选择了#3方式 - Axios Mock-Adapter。

当我想模拟HTTP请求时,这将比MSW更容易,而Axios Mock-Audapter比Sinon更强大。

Finally, I follow 3 Ways To Mock Axios In Jest and chose the #3 way - axios-mock-adapter.

This would be easier than msw and axios-mock-adapter is more powerful than sinon when I want to mock a http request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文