Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在玩笑中重置模拟值?_Javascript_Unit Testing_Jestjs_Mocking - Fatal编程技术网

Javascript 如何在玩笑中重置模拟值?

Javascript 如何在玩笑中重置模拟值?,javascript,unit-testing,jestjs,mocking,Javascript,Unit Testing,Jestjs,Mocking,我正在试着开玩笑,写单元测试。 我已经为几个函数编写了单元测试。这些函数使用从不同文件导入的常量对象。所以我模拟了这些常数 describe('testing helpers', () => { beforeEach(() => jest.resetModules()); describe('reset board', () => { // first test using original constant values t

我正在试着开玩笑,写单元测试。 我已经为几个函数编写了单元测试。这些函数使用从不同文件导入的常量对象。所以我模拟了这些常数

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);
        });
    });
});
但是,当第二个descripe块中的第三个测试运行时,它将拾取模拟值,而不是原始值。我在每次(()=>jest.resetModules()之前都认为这是
将重置模拟值,但不起作用。
请帮忙。
如有任何其他改进测试的建议,我们将不胜感激。

这可能会奏效

description('测试助手',()=>{
之前(()=>jest.clearAllMocks());
....
})
如果使用
jest+@测试库/react

从'@testing library/react'导入{cleanup}
每次之前(清理)

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

重置模块注册表-所有必需模块的缓存。这对于隔离本地状态可能在测试之间发生冲突的模块非常有用

为了丢弃模块模拟,需要使用
jest.unmack
jest.dontMock
。如果这些测试的默认行为是未修改的
常量
,则可能是:

beforeEach(() => {
  jest.unmock("./constants");
  jest.resetModules();
});
在这种情况下,在顶层导入原始实现并在需要它的测试中使用它更容易:

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

仅在需要模拟实现
助手
或它所依赖的模块(
常量
)的测试中需要模拟<使用
jest.reset modules
jest.unmock
在每次测试之前进行编码>仍然是可取的,为了使这些测试不会相互交叉污染,使用顶级
helper
的测试不会受到影响。

@EstusFlask是的,这不会改变任何事情。描述影响的层次结构,在*测试之前和*测试之后影响测试。由于所有测试共享一个共同的描述块,所以在每个测试影响所有测试之前,它只适用于玩笑间谍,而不是模块模拟
jest.resetModules()
用于OP中的所有测试,应该没有问题。我很确定问题出在我的帖子上。