Skip to content

Conversation

@yryuvraj
Copy link
Member

#5


import { generateCronExpression, generateReadableExpression } from '../src/cronGenerator';

app.get('/v1/generate', (req, res) => {

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, () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. export app from this file
  2. in index.ts, start the actual server by importing app and doing app.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) {

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) {

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants