Skip to content

Commit

Permalink
Merge pull request #110 from sindresorhus/improve-context-sharing
Browse files Browse the repository at this point in the history
improve context sharing for beforeEach/afterEach
  • Loading branch information
sindresorhus committed Oct 28, 2015
2 parents 730bf53 + 5fd57c1 commit 04b3db5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
24 changes: 15 additions & 9 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,26 @@ Runner.prototype._runTestWithHooks = function (test) {
tests.push(test);
tests.push.apply(tests, afterHooks);

var context = {};
// wrapper to allow context to be a primitive value
var contextWrapper = {
context: {}
};

return eachSeries(tests, function (test) {
return this._runTest(test, context);
Object.defineProperty(test, 'context', {
get: function () {
return contextWrapper.context;
},
set: function (val) {
contextWrapper.context = val;
}
});

return this._runTest(test);
}.bind(this)).catch(noop);
};

Runner.prototype._runTest = function (test, context) {
// shared context (only applies to tests with hooks around them)
// set only if `context` is an object (can be index number)
if (typeof context === 'object') {
test.context = context;
}

Runner.prototype._runTest = function (test) {
// add test result regardless of state
// but on error, don't execute next tests
return test.run()
Expand Down
2 changes: 1 addition & 1 deletion lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Test.prototype.run = function () {
this._timeout = setTimeout(function () {}, TIMEOUT_MAX_VALUE);

try {
var ret = this.fn.call(this.context, this);
var ret = this.fn(this);

if (ret && typeof ret.then === 'function') {
ret
Expand Down
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,20 @@ test(t => {
});
```

The context is by default an object, but it can also be directly assigned:

```js
test.beforeEach(t => {
t.context = 'unicorn';
t.end();
});

test(t => {
t.is(t.context, 'unicorn');
t.end();
});
```

### Custom assertion module

You can use any assertion module instead or in addition to the one that comes with AVA, but you won't be able to use the `.plan()` method, [yet](https://github.com/sindresorhus/ava/issues/25).
Expand Down
37 changes: 29 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,30 +748,51 @@ test('hooks - shared context', function (t) {
var runner = new Runner();

runner.addBeforeHook(function (a) {
a.is(this.arr, undefined);
this.arr = [];
a.is(a.context.arr, undefined);
a.context.arr = [];
a.end();
});

runner.addAfterHook(function (a) {
a.is(this.arr, undefined);
a.is(a.context.arr, undefined);
a.end();
});

runner.addBeforeEachHook(function (a) {
this.arr = ['a'];
a.context.arr = ['a'];
a.end();
});

runner.addTest(function (a) {
this.arr.push('b');
a.same(this.arr, ['a', 'b']);
a.context.arr.push('b');
a.same(a.context.arr, ['a', 'b']);
a.end();
});

runner.addAfterEachHook(function (a) {
this.arr.push('c');
a.same(this.arr, ['a', 'b', 'c']);
a.context.arr.push('c');
a.same(a.context.arr, ['a', 'b', 'c']);
a.end();
});

runner.run().then(function () {
t.is(runner.stats.failCount, 0);
t.end();
});
});

test('hooks - shared context of any type', function (t) {
t.plan(1);

var runner = new Runner();

runner.addBeforeEachHook(function (a) {
a.context = 'foo';
a.end();
});

runner.addTest(function (a) {
a.is(a.context, 'foo');
a.end();
});

Expand Down

0 comments on commit 04b3db5

Please sign in to comment.