Skip to content

Instantly share code, notes, and snippets.

@davidtweaver
Created May 6, 2024 18:37
Show Gist options
  • Save davidtweaver/60e8848345a821f940a2010f0f615fc5 to your computer and use it in GitHub Desktop.
Save davidtweaver/60e8848345a821f940a2010f0f615fc5 to your computer and use it in GitHub Desktop.
/////// Mocking dynamoDB
const mockDynamoDbPut = jest.fn();
class DocumentClient {
put = mockDynamoDbPut;
}
jest.mock('aws-sdk', () => ({
DynamoDB: {
DocumentClient,
},
}));
//.. import { module we want to test } from '.';.//
mockDynamoDbPut.mockImplementation(() => ({ promise: jest.fn() }));
/////// Mocking a function
import { upsert } from './upsert-logic';
jest.mock('./upsert-logic');
const mockUpsert = upsert as jest.MockedFunction<typeof upsert>;
mockUpsert.mockResolvedValue('foo')
/////// Mocking a class
import { SomeClass } from './SomeClass';
jest.mock('./SomeClass');
const mockedClass = <jest.Mock<SomeClass>>SomeClass;
/////// Mock object
import { mocked } from 'jest-mock';
const mockedObject = jest.mocked(someObject);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment