javascript - 如何在 Jest 中重置模拟值?

标签 javascript unit-testing jestjs mocking

我正在尝试我的手 Jest 和编写单元测试。 我已经为几个函数编写了单元测试。这些函数使用从不同文件导入的常量对象。所以我 mock 了这些常量。

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);
        });
    });
});

但是当第二个 describe block 中的第三个测试运行时,它正在获取模拟值而不是原始值。我认为这个 beforeEach(() => jest.resetModules()); 会重置模拟值,但它不起作用。 请帮忙解决这个问题。 任何其他改进测试的技巧将不胜感激。

最佳答案

jest.resetModules 仅重置模块缓存并允许重新导入模块,它不会影响生效的模块模拟​​:

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

为了丢弃模块 mock,需要使用 jest.unmockjest.dontMock。如果这些测试的默认行为是未模拟的 constants,这可以是:

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

在这种情况下,更容易在顶层导入原始实现并在需要它的测试中使用它:

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

require 仅在需要模拟实现helper 或其依赖的模块(constants)的测试中才需要模拟。 beforeEachjest.resetModulesjest.unmock 仍然是可取的,以便这些测试不会相互交叉污染,使用 top 的测试-level helper 不会受其影响。

关于javascript - 如何在 Jest 中重置模拟值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64515162/

相关文章:

javascript - .js 范围程序抛出错误

c# - Javascript 在 asp.net 中验证开始日期和结束日期

java - WebAppConfiguration 不能很好地与 ContextConfiguration 配合使用?

javascript - 从 javascript 对象访问属性

node.js - 如何使用 Jest 模拟日期对象?

javascript - 防止子元素的事件默认

javascript - ReactJS onChange,如果嵌套 hash/json

unit-testing - 如何为我的 meteor 方法编写单元测试?

php - 如何在 Yii2 中使用 Codeception 运行单元测试?

javascript - 在 Jest 中为 es6 类的静态方法创建模拟实现