Skip to content

Commit

Permalink
Preserving context binding, fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Sep 30, 2015
1 parent 0e85b2a commit 535f96c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ describeIt(..., function (getFn) {
});
```

## Context is preserved

We follow the convention and preserve the original context inside the `describeIt` callback, thus
you can assign the extracted value to a property

```js
desribeFunction(fooFilename, 'getFoo()', function (getFn) {
beforeEach(function () {
this.getFoo = getFn();
});

it('returns "foo"', function () {
la(this.getFoo() === 'foo');
});
});
```

[1]: http://philipwalton.com/articles/how-to-unit-test-private-functions-in-javascript/
[2]: https://github.com/jasmine/jasmine/pull/908
[3]: http://glebbahmutov.com/blog/picking-javascript-testing-framework/
Expand Down
2 changes: 1 addition & 1 deletion describe-it.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function describeIt(filename, functionSignature, useBeforeEach, cb) {
}

log('executing describeIt callback');
cb(returnsFn);
cb.call(this, returnsFn);

afterFn(function () {
log('deleting __exports object', global.__exports);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "describe-it",
"version": "1.4.0",
"version": "1.5.0",
"description": "Extracts a private function / variable for BDD unit testing",
"main": "describe-it.js",
"scripts": {
Expand Down
32 changes: 29 additions & 3 deletions test/get-foo-spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var desribeFunction = require('..');
var fooFilename = __dirname + '/foo.js';

desribeFunction(__dirname + '/foo.js', 'getFoo()', function (getFn) {
desribeFunction(fooFilename, 'getFoo()', function (getFn) {
it('has function argument', function () {
la(typeof getFn === 'function');
});
Expand All @@ -16,14 +17,14 @@ desribeFunction(__dirname + '/foo.js', 'getFoo()', function (getFn) {
});
});

desribeFunction(__dirname + '/foo.js', 'getFoo()', function (getFn) {
desribeFunction(fooFilename, 'getFoo()', function (getFn) {
it('returns "foo"', function () {
var getFoo = getFn();
la(getFoo() === 'foo');
});
});

desribeFunction(__dirname + '/foo.js', 'getFoo()', function (getFn) {
desribeFunction(fooFilename, 'getFoo()', function (getFn) {
var getFoo;

beforeEach(function () {
Expand All @@ -38,3 +39,28 @@ desribeFunction(__dirname + '/foo.js', 'getFoo()', function (getFn) {
la(getFn() === getFoo);
});
});

// storing value in 'this'
desribeFunction(fooFilename, 'getFoo()', function (getFn) {
beforeEach(function () {
this.getFoo = getFn();
});

it('returns "foo"', function () {
la(this.getFoo() === 'foo');
});
});

// using assign shortcut
desribeFunction(fooFilename, 'getFoo()', function (getFn) {
function assign(property, get) {
this[property] = get();
return this[property];
}

beforeEach(assign.bind(this.ctx, 'getFoo', getFn));

it('returns "foo"', function () {
la(this.getFoo() === 'foo');
});
});

0 comments on commit 535f96c

Please sign in to comment.