简体   繁体   中英

Mocking method of instance with Jest

How to mock the call of service.request in the code bellow?

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 })
}

In my unit-test I want to do something like this

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

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

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

UPDATE

I updated with my findings my code, but still no luck

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({})
  })
})

You can use jest.mock to mock jayson/promise module. Don't need to use jest.spyOn .

Eg

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('', {});
  });
});

unit test result:

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM