TypeError:XXXXXXX不是函数(在开玩笑的同时) [英] TypeError: XXXXXXX is not a function (While jest mocking)

查看:511
本文介绍了TypeError:XXXXXXX不是函数(在开玩笑的同时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力嘲笑以下方法.无法嘲笑.请在下面查看我的测试用例.测试用例因错误而失败

I am struggling to jest mock the below method. Not able to mock. Please see my test case below it. Test case fails with error

TypeError:XXXXXXX不是函数.

TypeError: XXXXXXX is not a function.

当我运行覆盖率报告时,它说所有行都被覆盖.我想念的是什么?

When I run coverage report, it says all lines are covered. What is that I am missing?

import { Lambda } from "aws-sdk";
import fsReadFilePromise = require("fs-readfile-promise");

export class AWSLambdaDeployer {

    readonly mylambda: Lambda;

     public async deploy(zipPath: string, fName: string) {

        const fileData = await fsReadFilePromise(zipPath);

        const params: Lambda.Types.UpdateFunctionCodeRequest = {
            FunctionName: fName,
            ZipFile: fileData,
            Publish: true
        };

        return this.mylambda.updateFunctionCode(params).promise();
    }
}

以下是我开玩笑的测试用例

Below is my jest test case


const mockUpdateFunctionCode = jest.fn().mockResolvedValueOnce('Ready');

jest.mock("../node_modules/aws-sdk/clients/lambda", () =>{
    return{
        updateFunctionCode: mockUpdateFunctionCode,     
    }
});

jest.mock("fs-readfile-promise", () => {
    return jest.fn();
});

import { Lambda } from "aws-sdk";
import fsReadFilePromise = require("fs-readfile-promise");
import { AWSLambdaDeployer } from "../src/index";

describe('LambdaDeployer class', () => {

    afterEach(() => {
     jest.resetAllMocks();
     jest.restoreAllMocks();
    });
           it(' functionality under test', async () =>{

             (fsReadFilePromise as any).mockResolvedValueOnce('get it done');

             const awslambdaDeployer = new AWSLambdaDeployer();

            const actual = await awslambdaDeployer.deploy('filePath', 'workingFunction');

             expect(fsReadFilePromise).toBeCalledWith('filePath');      
           })
});

这给我以下错误.测试用例失败.覆盖率报告显示了覆盖的所有行.

This gives me error as below. The test case fails. The coverage report shows all lines covered.

TypeError:this.mylambda.updateFunctionCode不是函数

TypeError: this.mylambda.updateFunctionCode is not a function

推荐答案

这是单元测试解决方案:

Here is the unit test solution:

index.ts:

import { Lambda } from 'aws-sdk';
import fsReadFilePromise from 'fs-readfile-promise';

export class AWSLambdaDeployer {
  private readonly mylambda: Lambda;
  constructor(lambda: Lambda) {
    this.mylambda = lambda;
  }

  public async deploy(zipPath: string, fName: string) {
    const fileData = await fsReadFilePromise(zipPath);

    const params: Lambda.Types.UpdateFunctionCodeRequest = {
      FunctionName: fName,
      ZipFile: fileData,
      Publish: true,
    };

    return this.mylambda.updateFunctionCode(params).promise();
  }
}

index.spec.ts:

import { AWSLambdaDeployer } from './';
import fsReadFilePromise from 'fs-readfile-promise';
import AWS from 'aws-sdk';

jest.mock('aws-sdk', () => {
  const mLambda = {
    updateFunctionCode: jest.fn().mockReturnThis(),
    promise: jest.fn(),
  };
  return {
    Lambda: jest.fn(() => mLambda),
  };
});

jest.mock('fs-readfile-promise', () => jest.fn());

describe('59463491', () => {
  afterEach(() => {
    jest.resetAllMocks();
    jest.restoreAllMocks();
  });
  it(' functionality under test', async () => {
    (fsReadFilePromise as any).mockResolvedValueOnce('get it done');

    const lambda = new AWS.Lambda();
    const awslambdaDeployer = new AWSLambdaDeployer(lambda);

    await awslambdaDeployer.deploy('filePath', 'workingFunction');

    expect(fsReadFilePromise).toBeCalledWith('filePath');
    expect(lambda.updateFunctionCode).toBeCalledWith({
      FunctionName: 'workingFunction',
      ZipFile: 'get it done',
      Publish: true,
    });
    expect(lambda.updateFunctionCode().promise).toBeCalledTimes(1);
  });
});

单元测试结果覆盖率100%:

Unit test result with 100% coverage:

 PASS  src/stackoverflow/59463491/index.spec.ts (10.592s)
  59463491
    ✓  functionality under test (9ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.ts |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        12.578s, estimated 13s

源代码: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59463491

这篇关于TypeError:XXXXXXX不是函数(在开玩笑的同时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆