使用 Mocks 使用 Jest 和 Typescript 进行测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48632801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:12:27  来源:igfitidea点击:

Testing with Jest and Typescript using Mocks

unit-testingtypescriptjest

提问by Steven Scott

I am working with Typescript and Jest to try to test some components for my Angular and Ionic application, but the issue is not limited to Angular or Ionic. As such, I am trying to get the mock functionality of Jest to work.

我正在使用 Typescript 和 Jest 尝试为我的 Angular 和 Ionic 应用程序测试一些组件,但问题不仅限于 Angular 或 Ionic。因此,我试图让 Jest 的模拟功能发挥作用。

I am simply creating a dummy class that I want to try to mock the responses of functions to see if I can override the behaviour.

我只是创建一个虚拟类,我想尝试模拟函数的响应以查看是否可以覆盖该行为。

jest-mock.ts

jest-mock.ts

export class AClass {
    constructor() { }

    GetOne():any {
        return  1;
    }

    GetTwo():any {
        return 2;
    }
}

jest-mock.spec.ts

jest-mock.spec.ts

import { AClass } from './jest-mock';

// const mockGet = jest.fn( () => { return 3; } );  // Tried this to return 3?
const mockGet = jest.fn();
jest.mock('./jest-mock', () => {
    return jest.fn().mockImplementation( () => {
        return { GetOne: mockGet };
    });
});

describe('Testing Jest Mock is working', () => {
    it('should support mocking out the component', () => {
        expect(mockGet).toBeTruthy();
        expect(mockGet).toBe(3);                // Mocked Value
    });
});

I am simply trying to create a test that can change the result of the function, so that my mock will be used by other real test code to provide results for testing.

我只是想创建一个可以更改函数结果的测试,以便其他真实测试代码使用我的模拟来提供测试结果。

When I try to create a class from the mock TestObject = new AClass();

当我尝试从模拟创建一个类时 TestObject = new AClass();

TypeError: _jestMock.AClass is not a constructor

With the test defined above, I get the following error:

通过上面定义的测试,我收到以下错误:

expect(received).toBe(expected)
    Expected value to be (using Object.is):
      3
    Received: 
      [Function mockConstructor]
    Difference:
       Comparing two different types of values. Expected number but received function.

采纳答案by Steven Scott

While checking other references, I did manage to get the mock test working. I changed the jest-mocks.spec.ts to be:

在检查其他参考资料时,我确实设法让模拟测试工作。我将 jest-mocks.spec.ts 更改为:

jest.mock('./jest-mock', () => {
    return {                          // Define Function Mock Return Values
        GetOne: jest.fn( () => 3 )
    }
});
const MockObject = require('./jest-mock');

describe('mock function', () => {
    it('should create mock', () => {
        expect(jest.isMockFunction(MockObject.GetOne)).toBeTruthy();
    });

    it('should return mock values', () => {
        expect(MockObject.GetOne()).toBe(3);
        expect(MockObject.GetOne).toHaveBeenCalled();
        expect(MockObject.GetTwo).toBeUndefined();
    });
});