Skip to content

Instantly share code, notes, and snippets.

@19WAS85
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 19WAS85/8bc28e5b89d679734a47 to your computer and use it in GitHub Desktop.
Save 19WAS85/8bc28e5b89d679734a47 to your computer and use it in GitHub Desktop.
Proof of concept: abbreviating mocha tests with specify
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) });
});
});
});
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;
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
@19WAS85
Copy link
Author

19WAS85 commented Apr 20, 2015

@felipekm
Copy link

Looks pretty clean :shipit:

@thiagofelix
Copy link

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

@19WAS85
Copy link
Author

19WAS85 commented Apr 22, 2015

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