Testing arguments with toBeCalledWith() in Jest











up vote
0
down vote

favorite












I have function that uses puppetteer's page object to evaluate and return some data.



I would like to write a unit test with jest to check if page.evaluate() takes specified parameters



This is the function



async function cinemasfromState(page, state) {
const CINEMA_SELECTOR = `div[data-state=$[STATE]] div.top-select-option a.eccheckbox`;
let res = await page.evaluate(
(elementPath, state) => {
let results = Array.from(document.querySelectorAll(elementPath)).map(
function(cin, index) {
return {
//Stuff
};
return result;
},
{ state }
);
},
CINEMA_SELECTOR.replace("$[STATE]", state),
state
);

return res;
}


Below is what my test looks like



describe("cinemasfromState", () => {
let page_mock = {
click: jest.fn(() => Promise.resolve()),
evaluate: jest.fn((selector, state) => Promise.resolve())
};

test("page.evaluate called correctly ", async () => {
await cinemasfromState(page_mock, "KAN");
expect(page_mock.evaluate).toBeCalledTimes(1);
expect(
page_mock.evaluate)toBeCalledWith(
"div[data-state=KAN] div.top-select-option a.eccheckbox",
"KAN"
);
});
});


And I get the below error as my test output



● cinemasfromState › page.evaluate called correctly

expect(jest.fn()).toBeCalledWith(expected)

Expected mock function to have been called with:
"div[data-state=KAN] div.top-select-option a.eccheckbox"
as argument 1, but it was called with
[Function anonymous].

Difference:

Comparing two different types of values. Expected string but received function.
"KAN"
as argument 2, but it was called with
"div[data-state=KAN] div.top-select-option a.eccheckbox".
undefined
as argument 3, but it was called with
"KAN".

Difference:

Comparing two different types of values. Expected undefined but received string.

52 | expect(page_mock1.evaluate).toBeCalledTimes(1);
53 | expect(page_mock1.evaluate).toBeCalledWith(
> 54 | "div[data-state=KAN] div.top-select-option a.eccheckbox",
| ^
55 | "KAN"
56 | );
57 | });


Any help on writing test to verify the arguments?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have function that uses puppetteer's page object to evaluate and return some data.



    I would like to write a unit test with jest to check if page.evaluate() takes specified parameters



    This is the function



    async function cinemasfromState(page, state) {
    const CINEMA_SELECTOR = `div[data-state=$[STATE]] div.top-select-option a.eccheckbox`;
    let res = await page.evaluate(
    (elementPath, state) => {
    let results = Array.from(document.querySelectorAll(elementPath)).map(
    function(cin, index) {
    return {
    //Stuff
    };
    return result;
    },
    { state }
    );
    },
    CINEMA_SELECTOR.replace("$[STATE]", state),
    state
    );

    return res;
    }


    Below is what my test looks like



    describe("cinemasfromState", () => {
    let page_mock = {
    click: jest.fn(() => Promise.resolve()),
    evaluate: jest.fn((selector, state) => Promise.resolve())
    };

    test("page.evaluate called correctly ", async () => {
    await cinemasfromState(page_mock, "KAN");
    expect(page_mock.evaluate).toBeCalledTimes(1);
    expect(
    page_mock.evaluate)toBeCalledWith(
    "div[data-state=KAN] div.top-select-option a.eccheckbox",
    "KAN"
    );
    });
    });


    And I get the below error as my test output



    ● cinemasfromState › page.evaluate called correctly

    expect(jest.fn()).toBeCalledWith(expected)

    Expected mock function to have been called with:
    "div[data-state=KAN] div.top-select-option a.eccheckbox"
    as argument 1, but it was called with
    [Function anonymous].

    Difference:

    Comparing two different types of values. Expected string but received function.
    "KAN"
    as argument 2, but it was called with
    "div[data-state=KAN] div.top-select-option a.eccheckbox".
    undefined
    as argument 3, but it was called with
    "KAN".

    Difference:

    Comparing two different types of values. Expected undefined but received string.

    52 | expect(page_mock1.evaluate).toBeCalledTimes(1);
    53 | expect(page_mock1.evaluate).toBeCalledWith(
    > 54 | "div[data-state=KAN] div.top-select-option a.eccheckbox",
    | ^
    55 | "KAN"
    56 | );
    57 | });


    Any help on writing test to verify the arguments?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have function that uses puppetteer's page object to evaluate and return some data.



      I would like to write a unit test with jest to check if page.evaluate() takes specified parameters



      This is the function



      async function cinemasfromState(page, state) {
      const CINEMA_SELECTOR = `div[data-state=$[STATE]] div.top-select-option a.eccheckbox`;
      let res = await page.evaluate(
      (elementPath, state) => {
      let results = Array.from(document.querySelectorAll(elementPath)).map(
      function(cin, index) {
      return {
      //Stuff
      };
      return result;
      },
      { state }
      );
      },
      CINEMA_SELECTOR.replace("$[STATE]", state),
      state
      );

      return res;
      }


      Below is what my test looks like



      describe("cinemasfromState", () => {
      let page_mock = {
      click: jest.fn(() => Promise.resolve()),
      evaluate: jest.fn((selector, state) => Promise.resolve())
      };

      test("page.evaluate called correctly ", async () => {
      await cinemasfromState(page_mock, "KAN");
      expect(page_mock.evaluate).toBeCalledTimes(1);
      expect(
      page_mock.evaluate)toBeCalledWith(
      "div[data-state=KAN] div.top-select-option a.eccheckbox",
      "KAN"
      );
      });
      });


      And I get the below error as my test output



      ● cinemasfromState › page.evaluate called correctly

      expect(jest.fn()).toBeCalledWith(expected)

      Expected mock function to have been called with:
      "div[data-state=KAN] div.top-select-option a.eccheckbox"
      as argument 1, but it was called with
      [Function anonymous].

      Difference:

      Comparing two different types of values. Expected string but received function.
      "KAN"
      as argument 2, but it was called with
      "div[data-state=KAN] div.top-select-option a.eccheckbox".
      undefined
      as argument 3, but it was called with
      "KAN".

      Difference:

      Comparing two different types of values. Expected undefined but received string.

      52 | expect(page_mock1.evaluate).toBeCalledTimes(1);
      53 | expect(page_mock1.evaluate).toBeCalledWith(
      > 54 | "div[data-state=KAN] div.top-select-option a.eccheckbox",
      | ^
      55 | "KAN"
      56 | );
      57 | });


      Any help on writing test to verify the arguments?










      share|improve this question















      I have function that uses puppetteer's page object to evaluate and return some data.



      I would like to write a unit test with jest to check if page.evaluate() takes specified parameters



      This is the function



      async function cinemasfromState(page, state) {
      const CINEMA_SELECTOR = `div[data-state=$[STATE]] div.top-select-option a.eccheckbox`;
      let res = await page.evaluate(
      (elementPath, state) => {
      let results = Array.from(document.querySelectorAll(elementPath)).map(
      function(cin, index) {
      return {
      //Stuff
      };
      return result;
      },
      { state }
      );
      },
      CINEMA_SELECTOR.replace("$[STATE]", state),
      state
      );

      return res;
      }


      Below is what my test looks like



      describe("cinemasfromState", () => {
      let page_mock = {
      click: jest.fn(() => Promise.resolve()),
      evaluate: jest.fn((selector, state) => Promise.resolve())
      };

      test("page.evaluate called correctly ", async () => {
      await cinemasfromState(page_mock, "KAN");
      expect(page_mock.evaluate).toBeCalledTimes(1);
      expect(
      page_mock.evaluate)toBeCalledWith(
      "div[data-state=KAN] div.top-select-option a.eccheckbox",
      "KAN"
      );
      });
      });


      And I get the below error as my test output



      ● cinemasfromState › page.evaluate called correctly

      expect(jest.fn()).toBeCalledWith(expected)

      Expected mock function to have been called with:
      "div[data-state=KAN] div.top-select-option a.eccheckbox"
      as argument 1, but it was called with
      [Function anonymous].

      Difference:

      Comparing two different types of values. Expected string but received function.
      "KAN"
      as argument 2, but it was called with
      "div[data-state=KAN] div.top-select-option a.eccheckbox".
      undefined
      as argument 3, but it was called with
      "KAN".

      Difference:

      Comparing two different types of values. Expected undefined but received string.

      52 | expect(page_mock1.evaluate).toBeCalledTimes(1);
      53 | expect(page_mock1.evaluate).toBeCalledWith(
      > 54 | "div[data-state=KAN] div.top-select-option a.eccheckbox",
      | ^
      55 | "KAN"
      56 | );
      57 | });


      Any help on writing test to verify the arguments?







      javascript unit-testing jestjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 18:05









      skyboyer

      2,99811028




      2,99811028










      asked Nov 11 at 5:08









      Theepan Thevathasasn

      5117




      5117
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          If you read your error log, you'll notice it's trying to match three arguments, but you are only asserting against two. .toBeCalledWith in jest will perform an exact match of the arguments passed to the function along with their order.



          For instance, if you call func(arg1, arg2), then expect(func).toBeCalledWith(arg2) will fail because you did not also assert on arg1. This is what is happening in your case because the first argument to page.evaluate() is actually an anonymous function.



          So, your test will need to be something like this:



          expect(page_mock.evaluate).toBeCalledWith(
          expect.any(Function),
          "div[data-state=KAN] div.top-select-option a.eccheckbox",
          "KAN"
          );





          share|improve this answer





















          • Got it.. Is there a reason expect(page_mock.evaluate).toBeCalledWith( (arg1, arg2) => {}, "div[data-state=KAN] div.top-select-option a.eccheckbox", "KAN" ); wouldn't work
            – Theepan Thevathasasn
            Nov 11 at 9:56













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246007%2ftesting-arguments-with-tobecalledwith-in-jest%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          If you read your error log, you'll notice it's trying to match three arguments, but you are only asserting against two. .toBeCalledWith in jest will perform an exact match of the arguments passed to the function along with their order.



          For instance, if you call func(arg1, arg2), then expect(func).toBeCalledWith(arg2) will fail because you did not also assert on arg1. This is what is happening in your case because the first argument to page.evaluate() is actually an anonymous function.



          So, your test will need to be something like this:



          expect(page_mock.evaluate).toBeCalledWith(
          expect.any(Function),
          "div[data-state=KAN] div.top-select-option a.eccheckbox",
          "KAN"
          );





          share|improve this answer





















          • Got it.. Is there a reason expect(page_mock.evaluate).toBeCalledWith( (arg1, arg2) => {}, "div[data-state=KAN] div.top-select-option a.eccheckbox", "KAN" ); wouldn't work
            – Theepan Thevathasasn
            Nov 11 at 9:56

















          up vote
          0
          down vote



          accepted










          If you read your error log, you'll notice it's trying to match three arguments, but you are only asserting against two. .toBeCalledWith in jest will perform an exact match of the arguments passed to the function along with their order.



          For instance, if you call func(arg1, arg2), then expect(func).toBeCalledWith(arg2) will fail because you did not also assert on arg1. This is what is happening in your case because the first argument to page.evaluate() is actually an anonymous function.



          So, your test will need to be something like this:



          expect(page_mock.evaluate).toBeCalledWith(
          expect.any(Function),
          "div[data-state=KAN] div.top-select-option a.eccheckbox",
          "KAN"
          );





          share|improve this answer





















          • Got it.. Is there a reason expect(page_mock.evaluate).toBeCalledWith( (arg1, arg2) => {}, "div[data-state=KAN] div.top-select-option a.eccheckbox", "KAN" ); wouldn't work
            – Theepan Thevathasasn
            Nov 11 at 9:56















          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          If you read your error log, you'll notice it's trying to match three arguments, but you are only asserting against two. .toBeCalledWith in jest will perform an exact match of the arguments passed to the function along with their order.



          For instance, if you call func(arg1, arg2), then expect(func).toBeCalledWith(arg2) will fail because you did not also assert on arg1. This is what is happening in your case because the first argument to page.evaluate() is actually an anonymous function.



          So, your test will need to be something like this:



          expect(page_mock.evaluate).toBeCalledWith(
          expect.any(Function),
          "div[data-state=KAN] div.top-select-option a.eccheckbox",
          "KAN"
          );





          share|improve this answer












          If you read your error log, you'll notice it's trying to match three arguments, but you are only asserting against two. .toBeCalledWith in jest will perform an exact match of the arguments passed to the function along with their order.



          For instance, if you call func(arg1, arg2), then expect(func).toBeCalledWith(arg2) will fail because you did not also assert on arg1. This is what is happening in your case because the first argument to page.evaluate() is actually an anonymous function.



          So, your test will need to be something like this:



          expect(page_mock.evaluate).toBeCalledWith(
          expect.any(Function),
          "div[data-state=KAN] div.top-select-option a.eccheckbox",
          "KAN"
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 7:45









          Auroratide

          61128




          61128












          • Got it.. Is there a reason expect(page_mock.evaluate).toBeCalledWith( (arg1, arg2) => {}, "div[data-state=KAN] div.top-select-option a.eccheckbox", "KAN" ); wouldn't work
            – Theepan Thevathasasn
            Nov 11 at 9:56




















          • Got it.. Is there a reason expect(page_mock.evaluate).toBeCalledWith( (arg1, arg2) => {}, "div[data-state=KAN] div.top-select-option a.eccheckbox", "KAN" ); wouldn't work
            – Theepan Thevathasasn
            Nov 11 at 9:56


















          Got it.. Is there a reason expect(page_mock.evaluate).toBeCalledWith( (arg1, arg2) => {}, "div[data-state=KAN] div.top-select-option a.eccheckbox", "KAN" ); wouldn't work
          – Theepan Thevathasasn
          Nov 11 at 9:56






          Got it.. Is there a reason expect(page_mock.evaluate).toBeCalledWith( (arg1, arg2) => {}, "div[data-state=KAN] div.top-select-option a.eccheckbox", "KAN" ); wouldn't work
          – Theepan Thevathasasn
          Nov 11 at 9:56




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246007%2ftesting-arguments-with-tobecalledwith-in-jest%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python