Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
65 views
in Technique[技术] by (71.8m points)

javascript - 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({})
  })
})
question from:https://stackoverflow.com/questions/65924278/mocking-method-of-instance-with-jest

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

E.g.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...