使用开玩笑使用 aws-sdk-mock 模拟 aws-sdk promises
Mocking aws-sdk promises with aws-sdk-mock using jest
你能看看下面的代码并告诉我它有什么问题吗?代码在 5 秒后超时,但根据官方描述,我希望它 运行 就好了。
有人看出什么是根本错误的吗?
import * as AWS from "aws-sdk-mock";
import * as _AWS from "aws-sdk";
beforeAll(async (done) => {
//get requires env vars
});
describe("the module", () => {
it("should read from the database", async () => {
AWS.mock('DynamoDB.DocumentClient', 'get', (error, callback) => { callback(null, "got it")});
expect(await (new _AWS.DynamoDB.DocumentClient()).get({TableName:"", Key: {pk: "foo", sk: "bar"}}).promise()).toBe("got it");
});
});
afterAll(() => {
AWS.restore();
});
我终于找到了工作变体:
import * as AWSMock from "aws-sdk-mock";
import * as AWS from "aws-sdk";
import { GetItemInput } from "aws-sdk/clients/dynamodb";
beforeAll(async (done) => {
//get requires env vars
done();
});
describe("the module", () => {
it("should mock getItem from DynamoDB", async () => {
AWSMock.setSDKInstance(AWS);
AWSMock.mock('DynamoDB', 'getItem', (params: GetItemInput, callback: Function) => {
console.log('DynamoDB', 'getItem', 'mock called');
callback(null, {pk: "foo", sk: "bar"});
})
let input:GetItemInput = { TableName: '', Key: {} };
const dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
expect(await dynamodb.getItem(input).promise()).toStrictEqual( { pk: 'foo', sk: 'bar' });
AWSMock.restore('DynamoDB');
});
it("should mock reading from DocumentClient", async () => {
AWSMock.setSDKInstance(AWS);
AWSMock.mock('DynamoDB.DocumentClient', 'get', (params: GetItemInput, callback: Function) => {
console.log('DynamoDB.DocumentClient', 'get', 'mock called');
callback(null, {pk: "foo", sk: "bar"});
})
let input:GetItemInput = { TableName: '', Key: {} };
const client = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
expect(await client.get(input).promise()).toStrictEqual( { pk: 'foo', sk: 'bar' });
AWSMock.restore('DynamoDB.DocumentClient');
});
});
你能看看下面的代码并告诉我它有什么问题吗?代码在 5 秒后超时,但根据官方描述,我希望它 运行 就好了。
有人看出什么是根本错误的吗?
import * as AWS from "aws-sdk-mock";
import * as _AWS from "aws-sdk";
beforeAll(async (done) => {
//get requires env vars
});
describe("the module", () => {
it("should read from the database", async () => {
AWS.mock('DynamoDB.DocumentClient', 'get', (error, callback) => { callback(null, "got it")});
expect(await (new _AWS.DynamoDB.DocumentClient()).get({TableName:"", Key: {pk: "foo", sk: "bar"}}).promise()).toBe("got it");
});
});
afterAll(() => {
AWS.restore();
});
我终于找到了工作变体:
import * as AWSMock from "aws-sdk-mock";
import * as AWS from "aws-sdk";
import { GetItemInput } from "aws-sdk/clients/dynamodb";
beforeAll(async (done) => {
//get requires env vars
done();
});
describe("the module", () => {
it("should mock getItem from DynamoDB", async () => {
AWSMock.setSDKInstance(AWS);
AWSMock.mock('DynamoDB', 'getItem', (params: GetItemInput, callback: Function) => {
console.log('DynamoDB', 'getItem', 'mock called');
callback(null, {pk: "foo", sk: "bar"});
})
let input:GetItemInput = { TableName: '', Key: {} };
const dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
expect(await dynamodb.getItem(input).promise()).toStrictEqual( { pk: 'foo', sk: 'bar' });
AWSMock.restore('DynamoDB');
});
it("should mock reading from DocumentClient", async () => {
AWSMock.setSDKInstance(AWS);
AWSMock.mock('DynamoDB.DocumentClient', 'get', (params: GetItemInput, callback: Function) => {
console.log('DynamoDB.DocumentClient', 'get', 'mock called');
callback(null, {pk: "foo", sk: "bar"});
})
let input:GetItemInput = { TableName: '', Key: {} };
const client = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
expect(await client.get(input).promise()).toStrictEqual( { pk: 'foo', sk: 'bar' });
AWSMock.restore('DynamoDB.DocumentClient');
});
});