gpt4 book ai didi

javascript - 引用错误 : Cannot access 'mockMethod1' before initialization

转载 作者:行者123 更新时间:2023-12-05 00:45:52 25 4
gpt4 key购买 nike

我有 3 个源文件 File1.ts、File2.ts、File3.ts。在执行 File3 的单元测试时,出现以下错误。

Test suite failed to run

ReferenceError: Cannot access 'mockMethod1' before initialization

20 | __esModule: true,
21 | default: jest.fn(),
> 22 | method1: mockMethod1,
| ^
23 | method2: mockMethod2
24 | }));
25 |

这是 File3 的 3 个源文件和单元测试的内容。

File1.ts

export default class File1 {
public element;

constructor(element) {
this.element = element;
}

method1(inputs) {
// Logic of Method1.
return output;
}

method2(inputs) {
// Logic of Method2.
return output;
}
}

File2.ts

import File1 from '../Folder1/File1'

export default class File2 {
public file1Object;

constructor(element) {
this.file1Object = new File1(element);
}

method1(inputs) {
// Logic of Method1.
let out = this.file1Object.method1(inputs);
// Logic of Method1.
return output;
}

method2(inputs) {
// Logic of Method2.
let out = this.file1Object.method2(inputs);
// Logic of Method2.
return output;
}
}

File3.ts

import File2 from '../Folder2/File2'
export default class File3 {
public file2Object;

constructor(element) {
this.file2Object = new File2(element);
}

method1(inputs) {
// Logic of Method1.
let out = this.file2Object.method1(inputs);
// Logic of Method1.
return output;
}

method2(inputs) {
// Logic of Method2.
let out = this.file2Object.method1(inputs);
// Logic of Method2.
return output;
}
}

File3.test.ts

import File3 from "./File3";
import File2 from "../Folder2/File2";

const mockMethod1 = jest.fn();
const mockMethod2 = jest.fn();

jest.mock('../Folder2/File2', () => ({
__esModule: true,
default: jest.fn(),
method1: mockMethod1,
method2: mockMethod2
}));

const file3Object = new File3(inputElement);
beforeEach(() => {
jest.clearAllMocks();
});

test('Method-1 Unit Test', () => {
mockMethod1.mockReturnValue(expectedOutput);
let observedOutput = file3Object.method1(inputs);

expect(observedOutput).toBe(expectedOutput);
})

test('Method-2 Unit Test', () => {
mockMethod2.mockReturnValue(expectedOutput);
let observedOutput = file3Object.method2(inputs);

expect(observedOutput).toBe(expectedOutput);
})

我不确定我在哪里犯了错误,所以我无法解决这个错误。任何解决此问题的建议。

最佳答案

有几件事会造成麻烦。首先,如 jest docs 中所述:

A limitation with the factory parameter is that, since calls to jest.mock() are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time!

这意味着您需要移动代码行以使它们看起来像这样:

  // First the mock functions
const mockMethod1 = jest.fn();
const mockMethod2 = jest.fn();

// Only then your imports & jest.mock calls
import File3 from "./File3";
import File2 from "../Folder2/File2";

jest.mock('../Folder2/File2', () => ({
// ...
}));

第二个问题是你在模拟 File2 就好像它在导出 method1method2,你不是在模拟 method1 File2 类的 method2 !看看4 ways of mocking an ES6 class in jest docs .

关于javascript - 引用错误 : Cannot access 'mockMethod1' before initialization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62712367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com