Jest TypeScript mocking store.dispatch导致进行0次调用 问题

Jest TypeScript mocking store.dispatch导致进行0次调用 问题,typescript,unit-testing,jestjs,Typescript,Unit Testing,Jestjs,我是测试新手。事实上,我已经阅读了所有关于jest的文档,它们并没有涵盖打字稿的具体案例,我正在研究我能做的。我正在尝试编写一个简单的测试,看看我的代码是否被触发 jest.mock('moment', () => { return { toLocaleString: jest.fn().mockReturnValue('test') } }); jest.mock('@/store/index') jest.mock('vuex'), () => {

我是测试新手。事实上,我已经阅读了所有关于jest的文档,它们并没有涵盖打字稿的具体案例,我正在研究我能做的。我正在尝试编写一个简单的测试,看看我的代码是否被触发

jest.mock('moment', () => {
    return {
        toLocaleString: jest.fn().mockReturnValue('test')
    }
});
jest.mock('@/store/index')
jest.mock('vuex'), () => {
    return {
        store: {
            dispatch: jest.fn()
        }
    }
};
jest.mock('@/services/JanusRequestService')
jest.mock('../../src/axiosConfig')
jest.mock('../../src/router', () => {
    return {
        test: jest.fn()
    }
})
jest.mock('../../src/interfaces/Request')

describe('copyRequestToClipboard', () => {
    let RequestMock = {
        Id: '1',
        createdBy: 'string',
        CreatedOnUtc: new Date(),
        modifiedOn: 'string',
        version: 2,
        workflowId: 'string',
        Status: 'string'
    }
    //@ts-ignore
    let writeTextMock = window.__defineGetter__('navigator', function () {
        return {
            clipboard: {
                writeText: jest.fn(x => x)
            }
        }
    })
    it('should call navigator.clipboard.writeText() correctly', () => {
        globalMixins.methods.copyRequestIdToClipboard(RequestMock)
        expect(writeTextMock).toHaveBeenCalled
    });

    it('should call triggerSnackBar correctly', () => {
        globalMixins.methods.triggerSnackBar = jest.fn()
        globalMixins.methods.copyRequestIdToClipboard(RequestMock)
        expect(globalMixins.methods.triggerSnackBar).toHaveBeenCalled
        expect(globalMixins.methods.triggerSnackBar).toHaveBeenCalledWith('Copied to Clipboard!', '')
    });
});
describe('triggerSnackBar', () => {
    let RequestMock: IRequest
    function constructData() {
        return {
            Id: '1',
            createdBy: 'string',
            CreatedOnUtc: new Date(),
            modifiedOn: 'string',
            version: 2,
            workflowId: 'string',
            Status: 'string'
        }
    }
    beforeEach(() => {
        RequestMock = constructData()
        let dispatchMock = store.dispatch = jest.fn();
    })

    it('should successfully call on dispatch 3 times', () => {
        globalMixins.methods.triggerSnackBar('test', 'green')
        expect(store.dispatch).toBeCalledTimes(3)
    });
    it('should have called with correct params', () => {
        globalMixins.methods.triggerSnackBar('test', 'green')
        expect(store.dispatch).toBeCalledWith({ text: 'test' })
    })
});
我得到的错误是

我在努力理解我做错了什么。我还记录了我需要如何测试以及使用什么模式。

我找到了答案

describe('triggerSnackBar', () => {
    let spy: any
    beforeEach(() => {
        spy = jest.spyOn(store, 'dispatch');
    })
    afterEach(() => {
        jest.clearAllMocks()
    })
    it('should successfully call on dispatch 3 times', () => {
        globalMixins.methods.triggerSnackBar('test', 'green')
        expect(store.dispatch).toBeCalledTimes(3)
    });
    it('should have called with correct params', () => {
        globalMixins.methods.triggerSnackBar('test', 'green')
        expect(store.dispatch).nthCalledWith(1, 'triggerSnackbarMessage', { text: 'test' })
        expect(store.dispatch).nthCalledWith(2, 'triggerSnackbarColor', { color: 'green' })
        expect(store.dispatch).nthCalledWith(3, 'triggerSnackbar')
    })
});