首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Jest测试JS脚本的单元:我可以模拟ES6类吗

用Jest测试JS脚本的单元:我可以模拟ES6类吗
EN

Stack Overflow用户
提问于 2018-12-12 09:56:22
回答 1查看 2.1K关注 0票数 1

当我从模块导入类时

代码语言:javascript
复制
const { OAuth2Client } = require('google-auth-library');

我怎么能嘲笑它呢?

代码语言:javascript
复制
jest.mock("google-auth-library");  // not mocking directly OAuth2Client

jest.mock("google-auth-library", OAuth2Client ) // is incorrect

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

代码语言:javascript
复制
jest.mock("google-auth-library", () => {
   return jest.fn().mockImplementation(() => {
      return {
        setCredentials: setCredentialsMock,
        getAccessToken: getAccessTokenMock
      }
   })
});

因此,我不能调用构造函数:

代码语言:javascript
复制
const oAuth2Client = new OAuth2Client({...});

反馈欢迎

更新1-

这里是google-auth-library-nodejs中与我的问题相关的最重要的编码。

google-auth-库-nodejs模块

======================================= /src/auth/index.ts .导出{ ..,OAuth2Client,…}从'./auth/oauth2client';.const =新GoogleAuth();导出{ auth,GoogleAuth};

代码语言:javascript
复制
    =======================================
    /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

代码语言:javascript
复制
    import {Credentials} from './credentials';
    ...
    export abstract class AuthClient extends EventEmitter {
       ...
      setCredentials(credentials: Credentials) {
        this.credentials = credentials;
      }
    }

======================================= /src/auth/credentials.js

代码语言:javascript
复制
    export interface Credentials {
      refresh_token?: string|null;
      expiry_date?: number|null;
      access_token?: string|null;
      token_type?: string|null;
      id_token?: string|null;
    }
    ...
EN

回答 1

Stack Overflow用户

发布于 2018-12-12 16:04:04

解决了..。使用下列规格:

代码语言:javascript
复制
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" });
  });
});
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53740341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档