Created
December 9, 2013 19:17
-
-
Save bajtos/7879091 to your computer and use it in GitHub Desktop.
RFC: syntax sugar for mocha tests calling multiple async functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original code | |
it('does something complex', function(done) { | |
async.waterfall( | |
[ | |
function setupSchema(cb) { | |
db.setupSchema(/*...*/, cb); | |
}, | |
function createTestUser(cb) { | |
db.createUser({ name: 'a user name' }, cb); | |
} | |
function act(data, cb) { | |
this.data = data; | |
db.users.edit(this.data.UserId, { name: 'new name' }, cb); | |
}, | |
function assert(cb) { | |
db.users.findById(this.data.userId, function(user) { | |
expect(user.name).to.equal('new name'); | |
cb(); | |
} | |
} | |
], | |
done); | |
}); | |
// Proposed API: | |
it('does something complex', [ | |
function setupSchema(cb) { | |
db.setupSchema(/*...*/, cb); | |
}, | |
function createTestUser(cb) { | |
db.createUser({ name: 'a user name' }, cb); | |
} | |
function act(data, cb) { | |
this.data = data; | |
db.users.edit(this.data.UserId, { name: 'new name' }, cb); | |
}, | |
function fetchNewData(cb) { | |
db.users.findById(this.data.userId, cb); | |
}, | |
function assert(user, cb) { | |
expect(user.name).to.equal('new name'); | |
cb(); | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have to agree that I'm on the "less magic" side, as @bajtos should know from my response to his email thread. That said, if you want to go down the "waterfall, building data as you go" model, let me shamelessly plug Stepdown: https://github.com/schoonology/stepdown . That's exactly what it does.