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

标签 javascript typescript unit-testing jestjs

我有 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/

相关文章:

java - 调用模拟的私有(private)方法而不是被模拟

node.js - 使用 chai 检查 typescript/nodejs 中的异常不起作用

javascript - Polymer 确定 dom-repeat items 上的最后一个 item

javascript - 简单分页多页,使用多个css选择器

javascript - 下划线不是函数

typescript - 有人可以向我解释一下 'as never' 在这段代码中做了什么吗?

typescript - 如何用其他内容替换Azure Function App中的主页?

TypeScript 类型断言错误 : 'This expression is not callable Type ' .。 .' has no call signatures' 是由于缺少分号引起的

java - Spring Validation 中的单元测试自定义 validator

javascript - HTML5 动态创建 Canvas