Can't not mock a function correctly with types

2019-08-03 09:03发布

I want to mock genName function and test getMessage function

moduleA.ts:

const getMessage = (): string => {
  return `Her name is ${genName()}`;
};

function genName(): string {
  return Math.random() > 0.5 ? 'emilie' : 'novaline';
}

export default {
  getMessage,
  genName
}

Here is my test file:

import moduleA from '../moduleA';

moduleA.genName = jest.fn(() => 'mrdulin');

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

  it('t-0', () => {
    expect(jest.isMockFunction(moduleA.genName)).toBeTruthy();
    expect(moduleA.genName()).toBe('mrdulin');
  });

  it('t-0.5', () => {
    expect(jest.isMockFunction(moduleA.genName)).toBeTruthy();
  });

  it('t-1', () => {

    expect(moduleA.getMessage()).toBe('Her name is emilie');
    expect(moduleA.genName).toHaveBeenCalled()


  });

});

but it seems not mock genName function success:

 FAIL  jest-examples/__test__/mock-function-0.spec.ts
  ● mock function › t-1

    expect(received).toBe(expected)

    Expected value to be (using ===):
      "Her name is emilie"
    Received:
      "Her name is novaline"

      at Object.it (jest-examples/__test__/mock-function-0.spec.ts:18:34)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
      at process._tickCallback (internal/process/next_tick.js:109:7)

  mock function
    ✓ t-0 (3ms)
    ✓ t-0.5
    ✕ t-1 (14ms)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 2 passed, 3 total
Snapshots:   0 total
Time:        0.152s, estimated 1s
Ran all test suites related to changed files.

But I am sure the moduleA.genName function has been called.

 PASS  jest-examples/__test__/mock-function-0.spec.ts
  mock function
    ✓ t-0 (2ms)
    ✓ t-0.5 (1ms)
    ✓ t-1 (1ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        0.148s, estimated 1s
Ran all test suites related to changed files.

标签: jestjs
2条回答
Juvenile、少年°
2楼-- · 2019-08-03 09:43

I assume you want to mock it cause of the Math.random call. Why not just mock it:

Math.random = () => 0 
expect(moduleA.getMessage()).toBe('Her name is novaline')
Math.random = () => 1 
expect(moduleA.getMessage()).toBe('Her name is emilie')

As the test run in a sandbox there is also no way that this can interfere any other test

查看更多
疯言疯语
3楼-- · 2019-08-03 09:59

If you adjust a little of your code above, it will work. Instead of calling genName() directly, after the change, you will use exportFunctions.genName()

const getMessage = (): string => {
  return `Her name is ${exportFunctions.genName()}`;
};

function genName(): string {
  return Math.random() > 0.5 ? 'emilie' : 'novaline';
}

const exportFunctions = {
  getMessage,
  genName
}

export default exportFunctions;
查看更多
登录 后发表回答