简体   繁体   English

TypeError: XXXXXXX 不是函数(虽然是在开玩笑)

[英]TypeError: XXXXXXX is not a function (While jest mocking)

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 is not a function.类型错误:XXXXXXX 不是函数。

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 is not a function类型错误:this.mylambda.updateFunctionCode 不是函数

Here is the unit test solution:这是单元测试解决方案:

index.ts : 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 : 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);
  });
});

Unit test result with 100% coverage: 100% 覆盖率的单元测试结果:

 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

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

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

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