javascript - 使用 Jest : Can I mock an ES6 class 对 JS 脚本进行单元测试

标签 javascript mocking jestjs es6-class

当我从一个模块导入一个类时

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-library-nodejs 的与我的问题相关的最重要的编码

google-auth-library-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;
    }
    ...

最佳答案

已解决...使用以下规范:

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" });
  });
});

关于javascript - 使用 Jest : Can I mock an ES6 class 对 JS 脚本进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53740341/

相关文章:

javascript - 通过AJAX调用Perl脚本打印文本文件的内容

编程面试中的 Javascript 和优先队列

python通过模拟测试装饰装饰器

JavaScript/Jest : How to show logs from test case only when test fails?

javascript - 将变量引入onreadystatechange

scala - 如何使用ScalaMock模拟按名称调用参数(如getOrElse)?

java - 如何使用 ArgumentMatchers 告诉 Mockito 任何队列?

node.js - Jest 测试因打开 Sequelize 连接而挂起

javascript - 对带有 Jest 的组件使用自定义模拟

javascript - 以 Angular 连接两个对象