I avoid using serverless-offline-sqs.
You will need to mess up your SQSClientConfig
and also install ElasticMQ.
In most cases you can simply check if you are offline, call directly your function and rely on SQS only when deployed. After all, when developing you don't have many rules, but when testing, your environment should be as close as possible to your real enviroment.
import {
GetQueueUrlCommand,
SendMessageCommand,
SQSClient,
} from "@aws-sdk/client-sqs";
const { IS_OFFLINE, SQS_QUEUE_NAME } = process.env;
const sqs = new SQSClient();
const handler = async () => {
const payload = {
hello: "world",
};
if (IS_OFFLINE === "true") {
myBusinessLogicFunction(payload);
} else {
const { QueueUrl } = await sqs.send(
new GetQueueUrlCommand({
QueueName: SQS_QUEUE_NAME,
})
);
await sqs.send(
new SendMessageCommand({
QueueUrl,
MessageBody: JSON.stringify(payload),
})
);
}
};
export const sqsHandler: SQSHandler = async (event) => {
for (const record of event.Records) {
await myBusinessLogicFunction(JSON.parse(record.body));
}
};
Can't say the same for serverless-dynamodb. But the less dependencies your project has, the better.
The APIs will be deployed in the ${stage}-${service}-${resourceName}
pattern so I suggest using the same pattern for deploying your Resources
.
The following example assumes you have serverless.ts
.
const Resources = {
UploadsBucket: {
Type: "AWS::S3::Bucket",
Properties: {
BucketName: `${stage}-${service}-uploads`,
},
},
};
I always use the serverless-dotenv-plugin.
It allows me to easily create multiple local dotnev files for multiple enviroments or situations. By default I always load my configuration from .env.dev.local
by explicitly setting the stage
when running locally.
sls offline --reloadHandler --stage dev
In my tests/dotenv-config.ts
I explicitly reference .env.test.local
.
import { config } from "dotenv"
config({ path: ".env.test.local" })
And add this file to my setupFiles
in jest.config.ts
import type { Config } from "jest"
const config: Config = {
setupFiles: ["<rootDir>/tests/dotenv-config.ts"],
detectOpenHandles: true,
testEnvironment: "node",
transform: {
"^.+.tsx?$": ["ts-jest", {}]
}
}
export default config