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

后端 未结 1 1701
隐瞒了意图╮
隐瞒了意图╮ 2021-01-24 22:14

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

相关标签:
1条回答
  • 2021-01-24 22:43

    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);
      });
    });
    

    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
    

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

    0 讨论(0)
提交回复
热议问题