简体   繁体   English

authenticationUser 的单元测试 - aws-cognito-identity-js - Jest

[英]Unit test of authenticateUser - aws-cognito-identity-js - Jest

I have made an API for users to login through AWS cognito.我为用户制作了一个 API,可以通过 AWS cognito 登录。 I am writing unit test case for the same using Jest.我正在使用 Jest 编写相同的单元测试用例。 I am not sure what's wrong there.我不确定那里出了什么问题。 I am very new to cognito as well as unit test cases, ie Jest so i could use the help here.我对认知和单元测试用例非常陌生,即 Jest,所以我可以在这里使用帮助。

import { Request, NextFunction, Response } from 'express';
import { AuthController } from '../../src/api/controller/authController';
import { Request as JestRequest } from 'jest-express/lib/request';
import { Response as JestResponse } from 'jest-express/lib/response';

describe('LoginUser', () => {
  it('should successfully login the user', async () => {
    const dataObj = {
      email: 'user@email.com',
      password: 'somepassword',
    };
    const mockRequest = {
      body: dataObj,
    } as Request;
    const mockResponse = new JestResponse();
    new AuthController().login(
      (mockRequest as unknown) as Request,
      (mockResponse as unknown) as Response
    ).then(response => {
      console.log("response for test", response);
      expect(response.body.message).toEqual('Successfully Logged In');
    })
  });

  it('should not login the user', async () => {
    const dataObj = {
      email: 'user@email.com',
      password: 'password',
    };
    const mockRequest = {
      body: dataObj,
    } as Request;
    const mockResponse = new JestResponse();
    new AuthController().login(
      (mockRequest as unknown) as Request,
      (mockResponse as unknown) as Response
    ).then(response => expect(response.body.message).toEqual('Incorrect username or password.'));
  });
});

It is passing both the test cases but it is not returning the promise, ie, not going into the then part of the code.它通过了两个测试用例,但没有返回承诺,即没有进入代码的 then 部分。

The cognito service to authenticateUser is as follows:对authenticationUser 的cognito 服务如下:

public async login(logInUserObj: LogInUserObj): Promise<any> {
    console.log("Came here through test 2--");
    const poolData = {
      UserPoolId: AWS_COGNITO_USER_POOL_ID,
      ClientId: AWS_COGNITO_USER_POOL_CLIENT_ID
    };
    const userPool = new CognitoUserPool(poolData);
    const authenticationDetails = new AuthenticationDetails({
      Username: logInUserObj.email,
      Password: logInUserObj.password
    });
    const userData = {
      Username: logInUserObj.email,
      Pool: userPool
    };
    const cognitoUser = new CognitoUser(userData);
    console.log("Came here through test 5--");
    return new Promise((resolve, reject) => {
      console.log("Came here through test 6--");
     return cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: (result) => {
          const userDetails = {
            access_token: result.getAccessToken().getJwtToken(),
            user_details: result.getIdToken().decodePayload(),
            refresh_token: result.getRefreshToken().getToken(),
            user_id: result.getIdToken().decodePayload().sub
          };
          console.log("Came here through test 3--");
          resolve(userDetails);
        },
        onFailure: (err) => {
          console.log("Came here through test 4--");
          resolve(err.message);
        }
      });
    });
  }

It is not going into the cognitoUser.authenticateUser function.它不会进入cognitoUser.authenticateUser函数。 What am I doing wrong?我究竟做错了什么?

回答这个问题可能有点晚了,但这可能是由于cognitoUser.authenticateUser前面的额外return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM