Skip to content

Commit e673e40

Browse files
committed
Hide test internals. Fixes avajs#252.
Fixes avajs#252.
1 parent c424ceb commit e673e40

File tree

1 file changed

+65
-33
lines changed

1 file changed

+65
-33
lines changed

lib/test.js

+65-33
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ function Test(title, fn) {
4141
if (this.title === 'callee$0$0') {
4242
this.title = '[anonymous]';
4343
}
44-
45-
Object.keys(Test.prototype).forEach(function (key) {
46-
this[key] = this[key].bind(this);
47-
}, this);
4844
}
4945

5046
module.exports = Test;
@@ -53,33 +49,6 @@ Test.prototype._assert = function () {
5349
this.assertCount++;
5450
};
5551

56-
// patch assert methods to increase assert count and store errors
57-
Object.keys(assert).forEach(function (el) {
58-
Test.prototype[el] = function () {
59-
var self = this;
60-
61-
try {
62-
var fn = assert[el].apply(assert, arguments);
63-
64-
fn = observableToPromise(fn);
65-
66-
if (isPromise(fn)) {
67-
return Promise.resolve(fn)
68-
.catch(function (err) {
69-
self._setAssertError(err);
70-
})
71-
.finally(function () {
72-
self._assert();
73-
});
74-
}
75-
} catch (err) {
76-
this._setAssertError(err);
77-
}
78-
79-
this._assert();
80-
};
81-
});
82-
8352
Test.prototype._setAssertError = function (err) {
8453
if (this.assertError !== undefined) {
8554
return;
@@ -128,7 +97,7 @@ Test.prototype.run = function () {
12897
var ret;
12998

13099
try {
131-
ret = this.fn(this);
100+
ret = this.fn(this._publicApi());
132101
} catch (err) {
133102
this._setAssertError(err);
134103
this.exit();
@@ -171,7 +140,7 @@ Test.prototype.run = function () {
171140
Object.defineProperty(Test.prototype, 'end', {
172141
get: function () {
173142
if (this.metadata.callback) {
174-
return this._end;
143+
return this._end.bind(this);
175144
}
176145
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)` ');
177146
}
@@ -230,3 +199,66 @@ Test.prototype.exit = function () {
230199
});
231200
}
232201
};
202+
203+
Test.prototype._publicApi = function () {
204+
var self = this;
205+
var api = {};
206+
207+
// Getters
208+
[
209+
'assertCount',
210+
'title',
211+
'end',
212+
'_capt',
213+
'_expr'
214+
]
215+
.forEach(function (name) {
216+
Object.defineProperty(api, name, {
217+
enumerable: !/^_/.test(name),
218+
get () {
219+
return self[name];
220+
}
221+
});
222+
});
223+
224+
// Get / Set
225+
Object.defineProperty(api, 'context', {
226+
enumerable: true,
227+
get () {
228+
return self.context;
229+
},
230+
set (context) {
231+
self.context = context;
232+
}
233+
});
234+
235+
// Bound Functions
236+
api.plan = this.plan.bind(this);
237+
238+
// Patched assert methods: increase assert count and store errors.
239+
Object.keys(assert).forEach(function (el) {
240+
api[el] = function () {
241+
try {
242+
var fn = assert[el].apply(assert, arguments);
243+
244+
fn = observableToPromise(fn);
245+
246+
if (isPromise(fn)) {
247+
return Promise.resolve(fn)
248+
.catch(function (err) {
249+
self._setAssertError(err);
250+
})
251+
.finally(function () {
252+
self._assert();
253+
});
254+
}
255+
} catch (err) {
256+
self._setAssertError(err);
257+
}
258+
259+
self._assert();
260+
};
261+
});
262+
263+
return api;
264+
};

0 commit comments

Comments
 (0)