简体   繁体   English

是否可以将TypeScript与'aws-sdk-mock'一起使用

[英]Is it possible to use TypeScript with 'aws-sdk-mock'

I'm writing unit tests for a serverless application in TypeScript, and I'd like to mock the AWS SDK. 我正在为TypeScript中的无服务器应用程序编写单元测试,我想模拟AWS SDK。

Unfortunately I have not found many existing type definitions for popular AWS mocking projects. 不幸的是,我没有找到流行的AWS模拟项目的许多现有类型定义。 In particular I'd like to use the aws-sdk-mock library, but without its type definitions I can't. 特别是我想使用aws-sdk-mock库,但没有它的类型定义我不能。

Theoretically I'd like to be able to do something like: 从理论上讲,我希望能够做到这样的事情:

import 'jest';
import * as sinon from 'sinon';
import * as _ from 'lodash';
import { handler } from '../lib/lambda';
import AWSMock from 'aws-sdk-mock';
import { PutItemInput } from 'aws-sdk/clients/dynamodb';

const mockData: DataType = {
   // ...some fields
};

describe('create data lambda tests', () => {

  afterEach(() => {
    sinon.restore();
    AWSMock.restore();
  });

  it('returns a success response on creation', () => {
    AWSMock.mock('DynamoDB.DocumentClient', 'put', (params: PutItemInput, callback: any) => {
      return callback(null, 'Successful creation');
    });

    const mockGatewayEvent: any = {
      headers: {
        Authorization: // some JWT
      },
      body: _.clone(mockData)
    };

    handler(mockGatewayEvent).then((createdData: DataType) => {
      expect(createdData.id).toBeDefined();
      expect(createdData.id.length).toBeGreaterThan(0);
    }, () => {
      fail('The create request should not have failed');
    });
  });
});

Here's how we got it working with jest. 以下是我们如何使用jest。 This tests a lambda function that makes calls to Dynamo using the DynamoDB.DocumentClient. 这将测试一个lambda函数,该函数使用DynamoDB.DocumentClient调用Dynamo。

The warnings about importing the aws-sdk-mock ts definitions go away for me if the file is called *.test.ts or *.spec.ts. 如果文件被称为* .test.ts或* .spec.ts,那么关于导入aws-sdk-mock ts定义的警告就会消失。

// stubbed.test.ts

// this line needs to come first due to my project's config
jest.mock("aws-sdk");

import * as AWS from "aws-sdk-mock";
import { handler } from "../index";
// these next two are just test data
import { mockDynamoData } from "../__data__/dynamo.data";
import { mockIndexData } from "../__data__/index.data";

describe("Stubbed tests", () => {
  it("should return correct result when Dynamo returns one slice", async () => {
    expect.assertions(2);
    const mockQuery = jest.fn((params: any, cb: any) =>
      cb(null, mockDynamoData.queryOneSlice)
    );
    AWS.mock("DynamoDB.DocumentClient", "query", mockQuery);
    // now all calls to DynamoDB.DocumentClient.query() will return mockDynamoData.queryOneSlice

    const response = await handler(mockIndexData.handlerEvent, null, null);

    expect(mockQuery).toHaveBeenCalled();
    expect(response).toEqual(mockIndexData.successResponseOneSlice);

    AWS.restore("DynamoDB.DocumentClient");
  });
});

You don't really need the type definitions - to do a 'typeless vanilla JS' import making the imported module any , just swap import to const as noted above. 你真的不需要类型定义 - 做一个'无类型vanilla JS'导入使得导入的模块any ,只需将import交换为const ,如上所述。 This works for me: 这对我有用:

const AWSMock = require('aws-sdk-mock');
import AWS = require('aws-sdk');
AWSMock.setSDKInstance(AWS);
AWSMock.mock('SQS', /* ... */);

const sqs = new AWS.SQS();
// test stuff

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

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