嘲笑一个函数的返回值

问题描述 投票:0回答:1

我有一个像这样的简单函数,我想用有效的accessToken来模拟authentication.getAccessToken()的返回值,我很难做到这一点。尝试了不同的方法但无法成功,有人可以帮我吗?

  import decodeJWT from "jwt-decode";      
  import authentication from "@kdpw/msal-b2c-react";

  export const testfunction = () => {
      const jwt = decodeJWT(authentication.getAccessToken());
      var current_time = Date.now() / 1000;
      var remaining_time = jwt.exp - current_time;
      return "testing done"
    }

以下是我一直在尝试的单元测试,由于authentication.getAccessToken()没有得到任何值,因此抛出InvalidToken错误。

    import * as commonUtil from "../commonUtil";

    describe('test test function', () => {
       it("Mock", () => {        
          //The following runs but test fails due to null return
          const authentication = require("@kdpw/msal-b2c-react");
          authentication.getAccessToken = jest.fn().mockReturnValue(false);
          expect(commonUtil.testfunction()).toBe(false)         
          }); 
       });

错误消息

Comparing two different types of values. Expected boolean but received null.
javascript reactjs unit-testing jestjs
1个回答
0
投票

您需要在测试中导入authentication

© www.soinside.com 2019 - 2024. All rights reserved.