【问题标题】:Mocking method of instance with Jest用 Jest 模拟实例的方法
【发布时间】:2021-01-27 17:28:24
【问题描述】:

如何在下面的代码中模拟service.request的调用?

import url from 'url'

import jayson from 'jayson/promise'

export async function dispatch(webHook, method, payload) {
  const service = jayson.Client.https({ ...url.parse(webHook) })

  return service.request(method, { ...payload })
}

在我的单元测试中,我想做这样的事情

jest.mock("") // what should go here?

it(() => {
  const method = 'test'

  expect(request).toHaveBeenCalledWith(method...) ?
})

更新

我用我的发现更新了我的代码,但仍然没有运气

import { Client } from 'jayson/promise'

import { dispatch } from '../src/remote'

jest.mock('jayson')

describe('remote', () => {
  let spy: jest.SpyInstance<any>

  beforeEach(() => {
    spy = jest.spyOn(Client.https.prototype, 'request')
  })

  afterEach(() => {
    spy.mockClear()
  })

  it('should invoke request method', () => {
    const url = 'http://example.com:8000'
    const method = ''
    const payload = {}

    dispatch(url, method, payload)

    expect(spy).toHaveBeenCalledWith({})
  })
})

【问题讨论】:

    标签: javascript unit-testing jestjs mocking


    【解决方案1】:

    您可以使用jest.mock 模拟jayson/promise 模块。不需要使用jest.spyOn

    例如

    index.ts:

    import url from 'url';
    import jayson from 'jayson/promise';
    
    export async function dispatch(webHook, method, payload) {
      const service = jayson.Client.https({ ...url.parse(webHook) });
    
      return service.request(method, { ...payload });
    }
    

    index.test.ts

    import { dispatch } from './';
    import jayson, { HttpsClient } from 'jayson/promise';
    import { mocked } from 'ts-jest';
    
    jest.mock('jayson/promise');
    
    const httpsClientMock = mocked(jayson.Client.https);
    
    describe('65924278', () => {
      afterAll(() => {
        jest.resetAllMocks();
      });
      it('should pass', async () => {
        const url = 'http://example.com:8000';
        const method = '';
        const payload = {};
        const serviceMock = ({
          request: jest.fn(),
        } as unknown) as HttpsClient;
        httpsClientMock.mockReturnValueOnce(serviceMock);
        await dispatch(url, method, payload);
        expect(jayson.Client.https).toBeCalledTimes(1);
        expect(serviceMock.request).toBeCalledWith('', {});
      });
    });
    

    单元测试结果:

     PASS  examples/65924278/index.test.ts (10.748 s)
      65924278
        √ should pass (13 ms)
    
    ----------|---------|----------|---------|---------|-------------------
    File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
    ----------|---------|----------|---------|---------|-------------------
    All files |     100 |      100 |     100 |     100 |
     index.ts |     100 |      100 |     100 |     100 |
    ----------|---------|----------|---------|---------|-------------------
    Test Suites: 1 passed, 1 total
    Tests:       1 passed, 1 total
    Snapshots:   0 total
    Time:        13.15 s
    

    【讨论】:

      猜你喜欢
      • 2018-06-21
      • 2018-08-17
      • 2019-03-22
      • 2019-10-09
      • 2021-12-08
      • 2016-11-07
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多