代码之家  ›  专栏  ›  技术社区  ›  user762579

用jest对js脚本进行单元测试:我可以模拟es6类吗

  •  1
  • user762579  · 技术社区  · 7 年前

    当我从模块导入类时

    const { OAuth2Client } = require('google-auth-library');
    

    我怎么能嘲笑它呢?

    jest.mock("google-auth-library");  // not mocking directly OAuth2Client
    
    jest.mock("google-auth-library", OAuth2Client ) // is incorrect
    

    如果我添加了在线实现,我就没有类名

    jest.mock("google-auth-library", () => {
       return jest.fn().mockImplementation(() => {
          return {
            setCredentials: setCredentialsMock,
            getAccessToken: getAccessTokenMock
          }
       })
    });
    

    所以我不能调用构造函数:

    const oAuth2Client = new OAuth2Client({...});
    

    欢迎反馈

    更新1——

    这里是与我的问题相关的google auth库nodejs中最重要的代码

    google auth库nodejs模块

    ======================================= /src/auth/index.ts文件 … 从“./auth/oauth2client”导出{..,oauth2client,…}; … const auth=new googleauth(); 导出{auth,googleauth};

        =======================================
        /src/auth/oauth2client.js
    
        import {AuthClient} from './authclient';
        ...
        export class OAuth2Client extends AuthClient {
          ....
          constructor(clientId?: string, clientSecret?: string, redirectUri?: string);
          constructor(
             ...
            super();
            ...
            this._clientId = opts.clientId;
            this._clientSecret = opts.clientSecret;
            this.redirectUri = opts.redirectUri;
            ...
          }
          ...
          getAccessToken(): Promise<GetAccessTokenResponse>;
          ...
        }
    

    ======================================= /src/auth/authclient.ts文件

        import {Credentials} from './credentials';
        ...
        export abstract class AuthClient extends EventEmitter {
           ...
          setCredentials(credentials: Credentials) {
            this.credentials = credentials;
          }
        }
    

    ======================================= /src/auth/credentials.js文件

        export interface Credentials {
          refresh_token?: string|null;
          expiry_date?: number|null;
          access_token?: string|null;
          token_type?: string|null;
          id_token?: string|null;
        }
        ...
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   user762579    7 年前

    解决了的。。。使用以下规格:

    jest.mock("google-auth-library");
    const { OAuth2Client } = require('google-auth-library');
    
    const setCredentialsMock = jest.fn();
    const getAccessTokenMock = jest.fn();
    
    OAuth2Client.mockImplementation(() => {
      return {
        setCredentials: setCredentialsMock,
        getAccessToken: getAccessTokenMock
      }
    });
    
    import index from "../index.js"
    
    describe('testing...', () => { 
      it("should setCredentials correctly....", () => {
        // GIVEN
        const oAuth2Client = new OAuth2Client("clientId", "clientSecret", "redirectUri");
        // WHEN
        oAuth2Client.setCredentials({ refresh_token: "aRefreshToken"});
        // THEN
        expect(setCredentialsMock).toHaveBeenCalledWith({ refresh_token: "aRefreshToken" });
      });
    });