Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - Best way for trying to check what a jest.mock function has been called with

I have the following code in a test where I am mocking the function getName, getName in the real code is passed state and an id, it uses the id to return a single name in a map function, so the return value is an array of names.

I've got an assertion that correctly checks the right array with 'name' and 'second name' strings is returned. However I also want to make sure that getName is called with each item of the array, what would be the best way to check that it's called with names[0] the first time and names[1] the second time?

Thanks

(getNamesFromList as jest.Mock).mockReturnValue([
        { nameType: { id: 'name id1' } },
        { nameType: { id: 'name id2' } },
      ]);

returns an array in the form

   names = [{id: 'name id1'},{id: 'name id2'}]


     ((getName as unknown) as jest.Mock)
    .mockReturnValueOnce('name')
    .mockReturnValueOnce('second name');


expect(getName).toHaveBeenCalledWith(
        state,
        names[0].id
      );
question from:https://stackoverflow.com/questions/65853781/best-way-for-trying-to-check-what-a-jest-mock-function-has-been-called-with

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...