Skip to content

Simplify soft assertion example and narrative #2

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ For example, let's say you are testing a user subscription service. There, one o
user.cancelSubscription()

expect(user.subscription.state).toBe('cancelled')
expect(user.subscription.endsAt).toBe('2025-01-01T00:00:00.000Z')
expect(user.subscription.endsAt).toBe('2026-01-01T00:00:00.000Z')
```

There are two criteria to assume the correct cancellation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ export class Plan {
}

const today = new Date()
today.setUTCDate(1)

if (today.getUTCMonth() === 0) {
today.setUTCFullYear(today.getUTCFullYear() + 1)
}
today.setUTCMonth(today.getUTCMonth() + 1, 1)

this.endsAt = today.toISOString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { UnlimitedPlan } from './plans'

beforeAll(() => {
vi.useFakeTimers()
vi.setSystemTime(new Date('2025-12-02T00:00:00Z'))
vi.setSystemTime(new Date('2025-11-02T00:00:00Z'))
})

afterAll(() => {
Expand Down
16 changes: 8 additions & 8 deletions exercises/03.assertions/05.solution.soft-assertions/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Received: "2025-12-01T00:00:00.000Z"

This gives me an overview of the entire system, not just the first failed assertion. Equipped with that knowledge, I can fix the issue as a whole instead of solving each failed assertion individually.

```ts filename=src/plans.ts add=6,10-12
```ts filename=src/plans.ts add=6,9-16
public cancel() {
if (this.state !== 'active') {
return
Expand All @@ -78,13 +78,13 @@ This gives me an overview of the entire system, not just the first failed assert
this.state = 'cancelled'

const today = new Date()
today.setUTCDate(1)
today.setUTCMonth(
today.getUTCMonth() + 1 > 11 ? 0 : today.getUTCMonth() + 1,
)

if (today.getUTCMonth() === 0) {
today.setUTCFullYear(today.getUTCFullYear() + 1)
switch (this.kind) {
case "monthly":
today.setUTCMonth(today.getUTCMonth() + 1, 1)
break;
case "yearly":
today.setUTCFullYear(today.getUTCFullYear() + 1, 0, 1)
Copy link
Member

Choose a reason for hiding this comment

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

I appreciate the realism here but the subscription logic is largely irrelevant for the exercise. For example, it might as well be that in this app the yearly subscription still expires on the first day of the next month once you cancel it, and you get refunded the unused credits. That doesn't really matter.

But what I want to try to do with your feedback is make the actions of the exercise more streamlined. Thinking about that...

break;
}

this.endsAt = today.toISOString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ export class Plan {
}

const today = new Date()
today.setUTCDate(1)

if (today.getUTCMonth() === 0) {
today.setUTCFullYear(today.getUTCFullYear() + 1)
}
today.setUTCMonth(today.getUTCMonth() + 1, 1)

this.endsAt = today.toISOString()
}
Expand Down