javascript - How do I reset mocked values in Jest? - Stack Overflow

I am trying my hand jest and writing unit tests.I have written unit tests for couple of functions. The

I am trying my hand jest and writing unit tests. I have written unit tests for couple of functions. These functions use an object of constants imported from a different file. So I have mocked these constants.

describe('testing helpers', () => {

    beforeEach(() => jest.resetModules());

    describe('reset board', () => {
        // first test using original constant values
        test('with default constants', () => {
            const game = {
                board: [
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0]
                ],
                count: 0
            };

            const helper = require('./helper');
            expect(helper.resetBoard()).toEqual(game);
        });

        // second test using mocked constant values
        test('reset board', () => {
            const game = {
                board: [
                    [0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0]
                ],
                count: 0
            };  

            jest.mock("./constants", () => ({ ROWS: 4, COLUMNS: 5 }));

            const helper = require('./helper');
            expect(helper.resetBoard()).toEqual(game);
        });
    });

    describe('make move', () => {
        // third test with original constant values
        test('player 1 move', () => {

            const testBoard = [
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0]
                ];
            const testTurn = 'YELLOW';
            const testColumn = 0;

            const expectedBoard = [
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [1, 0, 0, 0, 0, 0, 0]
                ];

            const helper = require('./helper');
            helper.makeMove(testBoard, testTurn, testColumn);
            expect(testBoard).toEqual(expectedBoard);
        });
    });
});

But when the third test which is in the second describe block is running it is picking up the mocked values instead of the original values. I thought this beforeEach(() => jest.resetModules()); would reset the mocked values but it is not working. Please help with this. Any other tips for improving on the tests will be appreciated.

I am trying my hand jest and writing unit tests. I have written unit tests for couple of functions. These functions use an object of constants imported from a different file. So I have mocked these constants.

describe('testing helpers', () => {

    beforeEach(() => jest.resetModules());

    describe('reset board', () => {
        // first test using original constant values
        test('with default constants', () => {
            const game = {
                board: [
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0]
                ],
                count: 0
            };

            const helper = require('./helper');
            expect(helper.resetBoard()).toEqual(game);
        });

        // second test using mocked constant values
        test('reset board', () => {
            const game = {
                board: [
                    [0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0]
                ],
                count: 0
            };  

            jest.mock("./constants", () => ({ ROWS: 4, COLUMNS: 5 }));

            const helper = require('./helper');
            expect(helper.resetBoard()).toEqual(game);
        });
    });

    describe('make move', () => {
        // third test with original constant values
        test('player 1 move', () => {

            const testBoard = [
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0]
                ];
            const testTurn = 'YELLOW';
            const testColumn = 0;

            const expectedBoard = [
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                    [1, 0, 0, 0, 0, 0, 0]
                ];

            const helper = require('./helper');
            helper.makeMove(testBoard, testTurn, testColumn);
            expect(testBoard).toEqual(expectedBoard);
        });
    });
});

But when the third test which is in the second describe block is running it is picking up the mocked values instead of the original values. I thought this beforeEach(() => jest.resetModules()); would reset the mocked values but it is not working. Please help with this. Any other tips for improving on the tests will be appreciated.

Share Improve this question asked Oct 24, 2020 at 15:43 doctorsherlockdoctorsherlock 1,3744 gold badges20 silver badges42 bronze badges 1
  • 1 @EstusFlask Yes, here you go repl.it/repls/AdmirableQueasyFlashdrives – doctorsherlock Commented Oct 24, 2020 at 16:22
Add a ment  | 

2 Answers 2

Reset to default 3

jest.resetModules only resets module cache and allows to reimport modules, it doesn't affect module mocks in effect:

Resets the module registry - the cache of all required modules. This is useful to isolate modules where local state might conflict between tests.

In order for module mock to be discarded, jest.unmock or jest.dontMock needs to be used. If a default behaviour for these tests is unmocked constants, this can be:

beforeEach(() => {
  jest.unmock("./constants");
  jest.resetModules();
});

In this scenario it's easier to import original implementation at top level and use it in tests that need it:

const helper = require('./helper');
...

And require a mock only in tests that need mocked implementation of helper or modules that it depends on (constants). beforeEach with jest.resetModules and jest.unmock is still desirable in order for these tests to not cross-contaminate each other, tests that use top-level helper won't be affected by it.

This could work out.

describe('testing helpers', () => {
    beforeEach(() => jest.clearAllMocks());
    ....
})

if use jest + @testing-library/react

import { cleanup } from '@testing-library/react'

beforeEach(cleanup)

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

相关推荐

  • javascript - How do I reset mocked values in Jest? - Stack Overflow

    I am trying my hand jest and writing unit tests.I have written unit tests for couple of functions. The

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信