Browse Source

Hide test internals. Fixes #252.

Fixes #252.
babel-plugin-for-integration-tests
James Talmage 9 years ago
parent
commit
c78c46fa85
  1. 98
      lib/test.js

98
lib/test.js

@ -41,10 +41,6 @@ function Test(title, fn) {
if (this.title === 'callee$0$0') { if (this.title === 'callee$0$0') {
this.title = '[anonymous]'; this.title = '[anonymous]';
} }
Object.keys(Test.prototype).forEach(function (key) {
this[key] = this[key].bind(this);
}, this);
} }
module.exports = Test; module.exports = Test;
@ -53,33 +49,6 @@ Test.prototype._assert = function () {
this.assertCount++; 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) { Test.prototype._setAssertError = function (err) {
if (this.assertError !== undefined) { if (this.assertError !== undefined) {
return; return;
@ -128,7 +97,7 @@ Test.prototype.run = function () {
var ret; var ret;
try { try {
ret = this.fn(this); ret = this.fn(this._publicApi());
} catch (err) { } catch (err) {
this._setAssertError(err); this._setAssertError(err);
this.exit(); this.exit();
@ -171,7 +140,7 @@ Test.prototype.run = function () {
Object.defineProperty(Test.prototype, 'end', { Object.defineProperty(Test.prototype, 'end', {
get: function () { get: function () {
if (this.metadata.callback) { 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)` '); 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)` ');
} }
@ -230,3 +199,66 @@ Test.prototype.exit = function () {
}); });
} }
}; };
Test.prototype._publicApi = function () {
var self = this;
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;
};

Loading…
Cancel
Save