javascript - How to make Jest mocked function toHaveBeenCalledTimes has a fresh count for each test suite? - Stack Overflow

When I assert the number of time the mocked function been called the imported mocked function always re

When I assert the number of time the mocked function been called the imported mocked function always return the bined number of invocations.

For example, the first test suite has a function called the mocked function import { get } from 'axios' once and the expected toHaveBeenCalledTimes is 1. However, the second test suite, the function called get again and toHaveBeenCalledTimes is 2 instead of 1.

How to make the mocked function toHaveBeenCalledTimes return a refresh count for each test suit?

describe('fetchAData', () => {
    it('should return the right A data response', (done) => {
        const sampleResponse = { data: dataASample };

        get.mockImplementationOnce(() => {
            return Promise.resolve(sampleResponse);
        });

        fetchAData().then(() => {
            expect(get).toHaveBeenCalledTimes(1);

            done();
        });
    });
});

describe('fetchBData', () => {
    it('should return the right B data response', (done) => {
        const sampleResponse = { data: dataBSample };

        get.mockImplementationOnce(() => {
            return Promise.resolve(sampleResponse);
        });

        fetchBData().then(() => {
            expect(get).toHaveBeenCalledTimes(1); // -> Return `2`
            done();
        });
    });
});

When I assert the number of time the mocked function been called the imported mocked function always return the bined number of invocations.

For example, the first test suite has a function called the mocked function import { get } from 'axios' once and the expected toHaveBeenCalledTimes is 1. However, the second test suite, the function called get again and toHaveBeenCalledTimes is 2 instead of 1.

How to make the mocked function toHaveBeenCalledTimes return a refresh count for each test suit?

describe('fetchAData', () => {
    it('should return the right A data response', (done) => {
        const sampleResponse = { data: dataASample };

        get.mockImplementationOnce(() => {
            return Promise.resolve(sampleResponse);
        });

        fetchAData().then(() => {
            expect(get).toHaveBeenCalledTimes(1);

            done();
        });
    });
});

describe('fetchBData', () => {
    it('should return the right B data response', (done) => {
        const sampleResponse = { data: dataBSample };

        get.mockImplementationOnce(() => {
            return Promise.resolve(sampleResponse);
        });

        fetchBData().then(() => {
            expect(get).toHaveBeenCalledTimes(1); // -> Return `2`
            done();
        });
    });
});
Share Improve this question edited Nov 2, 2018 at 18:56 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Oct 8, 2018 at 3:26 XYZXYZ 27.5k15 gold badges94 silver badges167 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

mockFn.mockReset() just did the trick, https://jestjs.io/docs/en/mock-function-api#mockfnmockreset

Does everything that mockFn.mockClear() does, and also removes any mocked return values or implementations.

beforeEach(() => {
    get.mockReset();
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745341269a4623337.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信