Skip to content

Commit 39470e7

Browse files
committed
[Refactor] minor cleanups
1 parent 85ddba3 commit 39470e7

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict';
22

33
var defined = require('defined');
4+
var through = require('@ljharb/through');
5+
46
var createDefaultStream = require('./lib/default_stream');
57
var Test = require('./lib/test');
68
var Results = require('./lib/results');
7-
var through = require('@ljharb/through');
89

910
var canEmitExit = typeof process !== 'undefined' && process
1011
&& typeof process.on === 'function' && process.browser !== true;

lib/default_stream.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
var through = require('@ljharb/through');
44
var fs = require('fs');
55

6+
var isObject = require('./isObject');
7+
68
module.exports = function () {
79
var line = '';
810
var stream = through(write, flush);
@@ -11,7 +13,7 @@ module.exports = function () {
1113
function write(buf) {
1214
if (
1315
buf == null // eslint-disable-line eqeqeq
14-
|| (Object(buf) !== buf && typeof buf !== 'string')
16+
|| (!isObject(buf) && typeof buf !== 'string')
1517
) {
1618
flush();
1719
return;

lib/isObject.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = function isObject(x) {
4+
return Object(x) === x;
5+
};

lib/test.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ function Test(name_, opts_, cb_) {
112112

113113
inherits(Test, EventEmitter);
114114

115+
function isPromiseLike(x) {
116+
return x && typeof x.then === 'function';
117+
}
118+
115119
Test.prototype.run = function run() {
116120
this.emit('prerun');
117121
if (!this._cb || this._skip) {
@@ -124,11 +128,7 @@ Test.prototype.run = function run() {
124128

125129
var callbackReturn = this._cb(this);
126130

127-
if (
128-
typeof Promise === 'function'
129-
&& callbackReturn
130-
&& typeof callbackReturn.then === 'function'
131-
) {
131+
if (typeof Promise === 'function' && isPromiseLike(callbackReturn)) {
132132
var self = this;
133133
Promise.resolve(callbackReturn).then(
134134
function onResolve() {
@@ -423,7 +423,7 @@ Test.prototype._end = function _end(err) {
423423
} catch (e) {
424424
self.fail(e);
425425
}
426-
if (res && typeof res.then === 'function') {
426+
if (isPromiseLike(res)) {
427427
res.then(next, function (_err) {
428428
// TODO: wth?
429429
err = err || _err;
@@ -811,6 +811,8 @@ function notDeepLooseEqual(a, b, msg, extra) {
811811
Test.prototype.notDeepLooseEqual
812812
= notDeepLooseEqual;
813813

814+
var isObject = require('./isObject');
815+
814816
Test.prototype['throws'] = function (fn, expected, msg, extra) {
815817
if (typeof expected === 'string') {
816818
msg = expected;
@@ -823,7 +825,7 @@ Test.prototype['throws'] = function (fn, expected, msg, extra) {
823825
fn();
824826
} catch (err) {
825827
caught = { error: err };
826-
if (Object(err) === err && 'message' in err && (!isEnumerable(err, 'message') || !hasOwn(err, 'message'))) {
828+
if (isObject(err) && 'message' in err && (!isEnumerable(err, 'message') || !hasOwn(err, 'message'))) {
827829
try {
828830
var message = err.message;
829831
delete err.message;

0 commit comments

Comments
 (0)