Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to deal with jest mock function of a module and typescript type

I use ts-jest and jest to write my testing files with typescript.

I am confused how to typing the mock function of a module.

Here is my code:

./module.ts:

import {IObj} from '../interfaces';

const obj: IObj = {
  getMessage() {
    return `Her name is ${this.genName()}, age is ${this.getAge()}`;
  },

  genName() {
    return 'novaline';
  },

  getAge() {
    return 26;
  }
};

export default obj;

./module.test.ts:

import * as m from './module';

describe('mock function test suites', () => {

  it('t-1', () => {
    // I think the jest.Mock<string> type here is not correct.
    m.genName: jest.Mock<string> = jest.fn(() => 'emilie'); 
    expect(jest.isMockFunction(m.genName)).toBeTruthy();
    expect(m.genName()).toBe('emilie');
    expect(m.getMessage()).toEqual('Her name is emilie, age is 26');
    expect(m.genName).toHaveBeenCalled(); 

  });

});

how to type the mock function genName of module m?

typescript give me an error here:

Error:(8, 7) TS2540:Cannot assign to 'genName' because it is a constant or a read-only property.

like image 843
slideshowp2 Avatar asked Jul 06 '17 06:07

slideshowp2


People also ask

How do I test a TypeScript function?

For TypeScript, unit tests are run against the generated JavaScript code. In most TypeScript scenarios, you can debug a unit test by setting a breakpoint in TypeScript code, right-clicking a test in Test Explorer, and choosing Debug.

How do you mock a function in react component Jest?

To mock a React component, the most straightforward approach is to use the jest. mock function. You mock the file that exports the component and replace it with a custom implementation. Since a component is basically a function, the mock should also return a function.

How do you spyOn a function in Jest?

To spy on an exported function in jest, you need to import all named exports and provide that object to the jest. spyOn function. That would look like this: import * as moduleApi from '@module/api'; // Somewhere in your test case or test suite jest.

How do you mock the return value of a function in Jest?

To mock a function's return value in Jest, you first need to import all named exports from a module, then use mockReturnValue on the imported function. You can use the * as <alias> inside an import statement to import all named exports. Then, you need to chain mockReturnValue off of jest.


1 Answers

This is how I have solved the same problem and how I do all of my mocking and spying now.

import * as m from './module';

describe('your test', () => {
  let mockGenName;

  beforeEach(() => {
    mockGenName = jest.spyOn(m, 
      'genName').mockImplemetation(() => 'franc');
  })

  afterEach(() => {
    mockGenName.mockRestore();
  })


  test('your test description', () => {
    // do something that calls the genName function
    expect(mockGenName).toHaveBeenCalledTimes(1);
  })

})

With this setup, you can programmatically change the implementation of the mock for different tests, and then assert that the function was called and what it was called with, all while clearing your mock in between tests and after all tests.

like image 98
Johan Avatar answered Oct 08 '22 04:10

Johan