Skip to content

Instantly share code, notes, and snippets.

View samwightt's full-sized avatar

Sam Wight samwightt

View GitHub Profile
@samwightt
samwightt / missing-done-callback-in-subscribe.yml
Last active September 8, 2025 18:42
ast-grep rule for when you're missing a `done` callback in an observable's `subscribe`. Only triggers when there are assertions inside the subscribe as we don't want to be too hasty.
language: TypeScript
id: missing-done-callback-in-subscribe
message: When subscribing to an observable inside a test function, you must call the 'done' function. Otherwise the assertions will never run.
severity: error
note: |
When testing observables in Jest, you must call the `done()` function in the subscribe callback. Jest provides a `done` function to test functions that must be executed to let Jest know when the test is done.
If you don't call the done function, the assertions will never run because Jest will finish the test before the subscribe callback is called.
If possible, it is encouraged to use `firstValueFrom` or `lastValueFrom` instead of subscribing to an observable directly in a test function. These handle subscribing / unsubscribing for you.