Skip to content

Instantly share code, notes, and snippets.

@mtimbs
mtimbs / SQSRecordFactory.ts
Created March 11, 2020 00:32
Utility for generating fake SQS Records to help with Unit/Integration testing
import { v4 as uuidv4 } from 'uuid';
import { SQSRecord } from 'aws-lambda';
const now = Math.round((new Date()).getTime() / 1000).toString();
export default (params?: Partial<SQSRecord>): SQSRecord => ({
messageId: uuidv4(),
receiptHandle: uuidv4(),
body: '',
@mtimbs
mtimbs / setEnvironment.js
Created March 10, 2020 14:10
jest setup set/overwrite environment variables before running tests
const env = {
AWS_REGION: 'local',
AWS_ACCESS_KEY: 'fake_key',
AWS_SECRET_KEY: 'fake_secret',
};
process.env = {
...process.env,
...env,
};
@mtimbs
mtimbs / dynamoDBClient.ts
Created March 10, 2020 14:02
example DynamoDB Client base module if not using powertools library
import { ClientConfiguration, DocumentClient } from 'aws-sdk/clients/dynamodb';
const config: ClientConfiguration = {
region: process.env.AWS_REGION,
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY,
};
if (['test'].includes(process.env.NODE_ENV)) {
config.endpoint = 'http://localhost:8000';
@mtimbs
mtimbs / dynamoDBClient.ts
Created March 10, 2020 13:57
example base DynamoDB Client that will work for offline testing
import { DocumentClient } from 'aws-sdk/clients/dynamodb';
import DynamoDB from '@dazn/lambda-powertools-dynamodb-client';
/*
* This checks the environment and either outputs a raw DynamoDocumentClient for offline testing
* or it outputs the dazn-powertools DynamoDocumentClient that adds context and tracing
*/
export default ['test'].includes(process.env.NODE_ENV)
? new DocumentClient({
region: process.env.AWS_REGION,
@mtimbs
mtimbs / jest-dynamodb-config.js
Created March 10, 2020 13:51
basic config for jest and dynamodb offline
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
module.exports = async () => {
/* eslint-disable global-require */
const serverless = new (require('serverless'))();
await serverless.init();
const service = await serverless.variables.populateService();
const resources = service.resources.Resources;
const tables = Object.keys(resources)
@mtimbs
mtimbs / jest.config.js
Last active March 10, 2020 13:51
example jest config
module.exports = {
setupFiles: ['./tests/setup/setEnvironment.js'],
transform: {
'^.+\\.ts?$': 'babel-jest',
},
moduleNameMapper: {
'^@repositories/(.*)$': '<rootDir>/src/repositories/$1',
'^@clients/(.*)$': '<rootDir>/src/clients/$1',
'^@transformers/(.*)$': '<rootDir>/src/transformers/$1',
'^@src/(.*)$': '<rootDir>/src/$1',
@mtimbs
mtimbs / base-IAM.json
Last active February 29, 2020 15:26
Base IAM permissions for deploying a serverless project
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"lambda:CreateFunction",
"lambda:ListVersionsByFunction",
"s3:CreateBucket",
@mtimbs
mtimbs / handler.ts
Last active February 27, 2020 15:01
example API Gateway handler for serverless framework using module aliasing for imports
import { APIGatewayProxyHandler } from ‘aws-lambda';
import { echo } from ‘@queries/exampleQuery';
import 'source-map-support/register’;
export const hello: APIGatewayProxyHandler = async (event) => ({
statusCode: 200,
body: JSON.stringify({
message: echo(‘Module aliasing is really the best’),
input: event,
}, null, 2),
@mtimbs
mtimbs / package.json scripts
Last active February 27, 2020 14:33
default package.json scripts
"scripts": {
"test": "NODE_ENV=test ./node_modules/.bin/jest --ci --verbose",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"buildtest": "tsc --noEmit"
},
@mtimbs
mtimbs / example.test.ts
Created February 27, 2020 14:29
example jest test to ensure jest is configured
describe('who tests the tests?', () => {
it('can run a test', () => {
expect.hasAssertions();
expect(1).toBe(1);
});
});