-
Notifications
You must be signed in to change notification settings - Fork 2
Change endpoint and update Jest configuration and tests #6
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
base: main
Are you sure you want to change the base?
Conversation
|
|
||
| import { generateCronExpression, generateReadableExpression } from '../src/cronGenerator'; | ||
|
|
||
| app.get('/v1/generate', (req, res) => { |
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.
Do not duplicate server code! (remember, DRY (don't repeat yourself)
Refer to the guide mentioned in jest docs.
|
|
||
| app.use('/', routes); | ||
|
|
||
| app.listen(port, () => { |
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.
- export
appfrom this file - in
index.ts, start the actual server by importingappand doingapp.listen(). This allows you to import the app in your test framework for E2E testing as well.
| export function generateReadableExpression(cronExpression: string): string { | ||
| try { | ||
| return cronstrue.toString(cronExpression); | ||
| } catch (error) { |
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.
Don't try catch here, let it throw error. It should be automatically handled by the global express error catcher and accordingly return an error.
|
|
||
| export function generateCronExpression() { | ||
| function getRandomOrWildcard(min: number, max: number) { | ||
| if (Math.random() < 0.7) { |
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.
It seems like Math.random cannot be seeded, so we can't really unit test this part of code. What we can do, however, is use property based testing to fuzzy test the generator and verify if the output is always valid.
We can check validity by passing the output to generadeReadableExpression and expect it to NOT throw.
#5