Last active
August 29, 2015 14:19
-
-
Save 19WAS85/8bc28e5b89d679734a47 to your computer and use it in GitHub Desktop.
Proof of concept: abbreviating mocha tests with specify
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
var should = require('chai').should(); | |
var specify = require('./specify').create(it); | |
// ------------------------ | |
// ARRAY TEST TRADITIONAL | |
// ------------------------ | |
describe('Array (traditional)', function(){ | |
var array = [1, 2, 3]; | |
describe('#push', function () { | |
describe('when add a new element', function () { | |
array.push(10); | |
it('should have new element', function () { | |
array.indexOf(10).should.be.above(-1); | |
}); | |
it('should increase length', function () { | |
array.length.should.be.equal(4); | |
}); | |
}); | |
}); | |
}); | |
// ------------------------- | |
// ARRAY TEST WITH SPECIFY | |
// ------------------------- | |
describe('Array (specify)', function(){ | |
var array = [1, 2, 3]; | |
describe('#push', function () { | |
describe('when add a new element', function () { | |
var newElement = 10; | |
array.push(newElement); | |
specify(function () { array.indexOf(newElement).should.be.above(-1) }); | |
specify(function () { array.length.should.be.equal(4) }); | |
}); | |
}); | |
}); |
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
var callsite = require('callsite'); | |
var fs = require('fs'); | |
var Specifier = function (it) { | |
this.it = it; | |
} | |
Specifier.prototype.perform = function (assertion) { | |
var stack = callsite(); | |
var call = stack[1]; | |
var file = call.getFileName(); | |
var lineno = call.getLineNumber(); | |
var src = fs.readFileSync(file, 'utf8'); | |
var line = src.split('\n')[lineno - 1]; | |
var desc = line | |
.replace(/(specify|function|[{}().;])/g, ' ') | |
.replace(/^\s*|\s*$/g, '') | |
.replace(/\s{2}/g, ' '); | |
return this.it(desc, assertion); | |
} | |
Specifier.create = function (mochaContext) { | |
return new Specifier(mochaContext).perform; | |
} | |
module.exports = Specifier; |
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
Array (traditional) | |
#push | |
when add a new element | |
✓ should have new element | |
✓ should increase length | |
Array (specify) | |
#push | |
when add a new element | |
✓ array indexOf newElement should be above -1 | |
✓ array length should be equal 4 |
Looks pretty clean
Good Stuff 👍
Definitely more sugar than the regular it syntax.
Question:
Would it still work if the specify execute asynchronous expectations? Maybe you would need to change sepecify:19:
From
return this.it(desc, assertion);
To
return this.it.apply([desc].concat[arguments])
to ensure you are by passing all the arguments from specify to the underline it block
Thanks guys!
Important advice, @thiagofelix. I'll considering specify in my personal projects to see if it's really useful. I'm going to implement this async caution, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by https://github.com/tj/better-assert