The text you are about to read describes an imaginary dialog between two programmers on their way to discover the orgastic pleasures of a library called asyncFn.
asyncFn provides additional methods to eg. jest.fn or sinon.spy to introduce “late resolve” for the promises returned by mock functions. This simplifies async unit testing by allowing tests that read chronologically, like a story.
From this, we get a new perspective of structuring and writing unit tests.
Be warned that we’ll also cover topics such as TDD, pair programming, evil pairing, and “negation testing”. They are not the main topic here, but some words are spared for them nonetheless to justify a way of thinking.
Best of luck, Dear Reader.
Fry: My name is Fry, and I find it difficult to unit test async-stuff in JavaScript.
Leela: Tell me more.
Fry: Yup, say I wanted to implement something to this specification:
Feature: As a player, I can play a game called Monster Beatdown
Scenario: I can damage an encountered monster
Given I encounter a monster
And I choose to attack it
Then the monster loses hit points
Scenario: I can try flee a monster
Given I encounter a monster
And I choose to flee
Then the monster eats me
And I lose
Scenario: I can win the game by beating a monster until it is knocked out
Given I encounter a monster
When I attack it until it has no hit points
Then the monster is knocked out
And I win
Fry: The kicker here is that in this game, prompting the player for action is asynchronous.
Leela: Mm-hmm, I think I get it. Let’s get our hands dirty and see where it lands us.
#javascript #testing #asynchronous #async #jest #nodejs #tdd #unit-testing