简体   繁体   中英

Testing to see if a function is called with Jest and Typescript, and ts-jest?

So I am trying to test this code

src/helpers/CommentHelper.ts:

export default class CommentHelper {

    gitApiObject: GitApi.IGitApi ;

    constructor(gitApiObject: GitApi.IGitApi)
    {
        this.gitApiObject = gitApiObject;
    }

    async postComment(commentContent: string, repoId: string, pullRequestId: number): Promise<any> {
        const comment: GitInterfaces.Comment = <GitInterfaces.Comment>{content: commentContent};
        const newCommentThread: GitInterfaces.GitPullRequestCommentThread = <GitInterfaces.GitPullRequestCommentThread>{comments: [comment]}
        await this.gitApiObject.createThread(newCommentThread, repoId, pullRequestId);
    }
}

Here the tests:

import  CommentHelper  from "../helpers/CommentHelper";
import { mocked } from 'ts-jest/utils';
import  { GitApi, IGitApi }  from "azure-devops-node-api/GitApi";


jest.mock('../helpers/CommentHelper', () => {
    return {
      default: jest.fn().mockImplementation(() => {})
    };
});

describe("CommentHelper Tests",  () => {
    const mockedGitApi = mocked(GitApi, true);

    beforeEach(() => {
        mockedGitApi.mockClear();
    });

    it("Check to see if the gitApiObject is called properly",  () => {
        const commentHelper = new CommentHelper(<any>mockedGitApi);
        const spy = jest.spyOn(GitApi.prototype ,'createThread')
        commentHelper.postComment("", "", 0);
        expect(spy).toHaveBeenCalled();
    })
})

This is the error:

    TypeError: commentHelper.postComment is not a function

      23 |         const commentHelper = new CommentHelper(<any>mockedGitApi);
      24 |         const spy = jest.spyOn(GitApi.prototype ,'createThread')
    > 25 |         commentHelper.postComment("", "", 0);
         |                       ^
      26 |         expect(spy).toHaveBeenCalled();
      27 |     })
      28 |

Right now we're early in the project so the tests are extremely simple. We just want to make sure gitApiObject/createThread is called. How can I achieve this without explicitly mocking out the postComment function?

Thanks: :)

So if I get your code right, you're currently mocking the default export of your CommentHelper as a function.

When accessing postComment you will get the response of your mock back which is currently not defined.

As I see in the other things you have provided in your example test case you want to test if GitAPI was called. In this case, you can't mock CommentHelper since then there is no possibility for GitApi to be called.

If you want to mock CommentHelper you have to return

jest.mock('../helpers/CommentHelper', () => {
    return {
      default: jest.fn().mockImplementation(() => ({
        postComment:jest.fn()
      }))
    };
});

if you just want to spy on GitAPI your good to go. If you don't want GitAPI to be called add .mockImplementation after your spyOn .

Hope this helps!

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