-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create unit tests for validation #71
Conversation
First one added is portfolioStepCommand, placing it into its own file so we can unit test the creation of the commands and test them against a mock dynamo table
unit tests for various validation methods, aswell as object mapping for the portfolioStep object
- isBodyPresent boolean valdiation to check for valid JSON but empty request body - isPathParameterPresent, same thing but for the path parameter
now using isPathParameterPresent and isBodyPresent
- add jest test script - add jest dependency - add ts-jest dependency
Used for jest tests, matches any file with '.test.ts' file extension
* @param body - The body of the request | ||
* @returns true if the string is empty or null | ||
*/ | ||
export function isBodyPresent(body: string | null): body is string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is may be the wrong use of a type guard. It does accomplish the goals we have and it does in fact assert that the input is a string. But it fails to capture the additional restrictions imposed within the body.
Before: string | undefined Changed to: string The actual UpdateCommand will not handle an undefined table name, so it must be removed from the portfolioStepCommand
This will be used for unit testing locally with aws-sdk-mock
Not as useful as other mock tests, because we are not too concerned if the object actually updates. But this proves a simple implementation of local mocking.
This will allow us to unit test the getPortfolioStep dynamodb call, and improves function clarity
|
||
const TABLE_NAME = process.env.ATAT_TABLE_NAME; | ||
const TABLE_NAME = process.env.ATAT_TABLE_NAME ?? ""; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this isn't set, we need to totally abort. We can't work with a default value. This would be an unexpected infrastructure issue. We probably want a helper that does something like:
if (!process.env.ATAT_TABLE_NAME) {
throw new Error("Oh no! The table name isn't set.")
}
of course with a better message and maybe a subclass of Error
? But this really is an exceptional case that should never happen and if it does, we need to return a 500 response with a really good log message ASAP.
- deleted unused imports - refined test data
- mock GetCommand for getPortfolioStep - this ensures that getPortfolioStepCommand is a proper GetCommand (returns Item)
SonarCloud Quality Gate failed. 0 Bugs 0.0% Coverage Catch issues before they fail your Quality Gate with our IDE extension SonarLint |
Overview
Using
jest
to test the business logic of the API endpoints.Moved all of the validation to
utils/validation.ts
, each microservice will use these validation fns.npm test
output:TODO