Ложный метод sh _orderBy с Jest - PullRequest
0 голосов
/ 10 марта 2020

Мне интересно, как я могу смоделировать метод loda sh _orderBy с помощью Jest и убедиться, что он был вызван с аргументами, приведенными ниже.

Мой Vue .component метод sliceArray

 sliceArray: function(array) {
          let val = _.orderBy(array, "orderDate", "desc");
          return val.slice(0, this.numberOfErrandsLoaded);
        }

Это то, что у меня есть:

import _ from "lodash";
jest.unmock("lodash");

it("Check orderBy method from lodash", () => {
    _.orderBy = jest.fn();
    expect(_.orderBy).toHaveBeenCalledWith([], "orderDate", "desc");
  });

Текущее сообщение об ошибке:

Error: expect(jest.fn()).toHaveBeenCalledWith(...expected)

Expected: [], "orderDate", "desc"

Number of calls: 0

Заранее спасибо!

/ E

1 Ответ

0 голосов
/ 10 марта 2020

Это то, что я делаю, проверяю импортированные библиотеки. Я использую jest.spyOn (object, methodName)

import * as _ from "lodash";
const spyOrderByLodash = jest.spyOn(_, 'orderBy');

it("Check orderBy method from lodash", () => {
    expect(spyOrderByLodash).toHaveBeenCalledWith([], "orderDate", "desc");
  });

Не забудьте очиститьAllMocks перед каждым тестом (необязательно, но обязательно, если у вас есть несколько тестов в одном файле):

  beforeEach(() => {
    jest.clearAllMocks();
  });
...