Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide test internals. Fixes #252. #286

Merged
merged 1 commit into from
Nov 30, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 65 additions & 33 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ function Test(title, fn) {
if (this.title === 'callee$0$0') {
this.title = '[anonymous]';
}

Object.keys(Test.prototype).forEach(function (key) {
this[key] = this[key].bind(this);
}, this);
}

module.exports = Test;
Expand All @@ -53,33 +49,6 @@ Test.prototype._assert = function () {
this.assertCount++;
};

// patch assert methods to increase assert count and store errors
Object.keys(assert).forEach(function (el) {
Test.prototype[el] = function () {
var self = this;

try {
var fn = assert[el].apply(assert, arguments);

fn = observableToPromise(fn);

if (isPromise(fn)) {
return Promise.resolve(fn)
.catch(function (err) {
self._setAssertError(err);
})
.finally(function () {
self._assert();
});
}
} catch (err) {
this._setAssertError(err);
}

this._assert();
};
});

Test.prototype._setAssertError = function (err) {
if (this.assertError !== undefined) {
return;
Expand Down Expand Up @@ -128,7 +97,7 @@ Test.prototype.run = function () {
var ret;

try {
ret = this.fn(this);
ret = this.fn(this._publicApi());
} catch (err) {
this._setAssertError(err);
this.exit();
Expand Down Expand Up @@ -171,7 +140,7 @@ Test.prototype.run = function () {
Object.defineProperty(Test.prototype, 'end', {
get: function () {
if (this.metadata.callback) {
return this._end;
return this._end.bind(this);
}
throw new Error('t.end is not supported in this context. To use t.end as a callback, you must use "callback mode" via `test.cb(testName, fn)` ');
}
Expand Down Expand Up @@ -230,3 +199,66 @@ Test.prototype.exit = function () {
});
}
};

Test.prototype._publicApi = function () {
var self = this;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My OCD, can you put the self statement before api?

var api = {};

// Getters
[
'assertCount',
'title',
'end',
'_capt',
'_expr'
]
.forEach(function (name) {
Object.defineProperty(api, name, {
enumerable: !/^_/.test(name),
get: function () {
return self[name];
}
});
});

// Get / Set
Object.defineProperty(api, 'context', {
enumerable: true,
get: function () {
return self.context;
},
set: function (context) {
self.context = context;
}
});

// Bound Functions
api.plan = this.plan.bind(this);

// Patched assert methods: increase assert count and store errors.
Object.keys(assert).forEach(function (el) {
api[el] = function () {
try {
var fn = assert[el].apply(assert, arguments);

fn = observableToPromise(fn);

if (isPromise(fn)) {
return Promise.resolve(fn)
.catch(function (err) {
self._setAssertError(err);
})
.finally(function () {
self._assert();
});
}
} catch (err) {
self._setAssertError(err);
}

self._assert();
};
});

return api;
};