Skip to content

Commit

Permalink
Merge 342f878 into 626fbd7
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jan 14, 2019
2 parents 626fbd7 + 342f878 commit 6252031
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
27 changes: 27 additions & 0 deletions lib/Unexpected.js
Expand Up @@ -13,6 +13,7 @@ const defaultDepth = require('./defaultDepth');
const createWrappedExpectProto = require('./createWrappedExpectProto');
const AssertionString = require('./AssertionString');
const throwIfNonUnexpectedError = require('./throwIfNonUnexpectedError');
const nodeJsCustomInspect = require('./nodeJsCustomInspect');

function isAssertionArg({ type }) {
return type.is('assertion');
Expand Down Expand Up @@ -73,6 +74,12 @@ const anyType = {
}
};

if (nodeJsCustomInspect !== 'inspect') {
anyType[nodeJsCustomInspect] = function() {
return `type: ${this.name}`;
};
}

function Unexpected(options = {}) {
this.assertions = options.assertions || {};
this.typeByName = options.typeByName || { any: anyType };
Expand Down Expand Up @@ -358,6 +365,10 @@ Unexpected.prototype.inspect = function(obj, depth, outputOrFormat) {
);
};

if (nodeJsCustomInspect !== 'inspect') {
Unexpected.prototype[nodeJsCustomInspect] = Unexpected.prototype.inspect;
}

Unexpected.prototype.expandTypeAlternations = function(assertion) {
const that = this;
function createPermutations(args, i) {
Expand Down Expand Up @@ -819,6 +830,10 @@ Unexpected.prototype.addType = function(type, childUnexpected) {
);
};

if (nodeJsCustomInspect !== 'inspect') {
extendedBaseType[nodeJsCustomInspect] = extendedBaseType.inspect;
}

extendedBaseType.diff = (actual, expected, output) => {
if (!output || !output.isMagicPen) {
throw new Error(
Expand All @@ -844,6 +859,13 @@ Unexpected.prototype.addType = function(type, childUnexpected) {
});
const originalInspect = extendedType.inspect;

// Prevent node.js' util.inspect from complaining about our inspect method:
if (nodeJsCustomInspect !== 'inspect') {
extendedType[nodeJsCustomInspect] = function() {
return `type: ${type.name}`;
};
}

extendedType.inspect = function(obj, depth, output, inspect) {
if (arguments.length < 2 || (!output || !output.isMagicPen)) {
return `type: ${type.name}`;
Expand Down Expand Up @@ -1019,6 +1041,11 @@ function installExpectMethods(unexpected) {
expect.it = unexpected.it.bind(unexpected);
expect.equal = unexpected.equal.bind(unexpected);
expect.inspect = unexpected.inspect.bind(unexpected);
if (nodeJsCustomInspect !== 'inspect') {
expect[nodeJsCustomInspect] = unexpected[nodeJsCustomInspect].bind(
unexpected
);
}
expect.findTypeOf = unexpected.findTypeOf; // Already bound
expect.fail = (...args) => {
try {
Expand Down
5 changes: 5 additions & 0 deletions lib/createWrappedExpectProto.js
Expand Up @@ -5,6 +5,7 @@ const wrapPromiseIfNecessary = require('./wrapPromiseIfNecessary');
const oathbreaker = require('./oathbreaker');
const UnexpectedError = require('./UnexpectedError');
const utils = require('./utils');
const nodeJsCustomInspect = require('./nodeJsCustomInspect');

function isAssertionArg({ type }) {
return type.is('assertion');
Expand Down Expand Up @@ -254,6 +255,10 @@ module.exports = function createWrappedExpectProto(unexpected) {
}
};

if (nodeJsCustomInspect !== 'inspect') {
wrappedExpectProto[nodeJsCustomInspect] = unexpected[nodeJsCustomInspect];
}

if (Object.__defineGetter__) {
Object.defineProperty(wrappedExpectProto, 'subjectType', {
enumerable: true,
Expand Down
4 changes: 3 additions & 1 deletion lib/index.js
Expand Up @@ -6,7 +6,9 @@ module.exports = require('./Unexpected')
.freeze();

// Add an inspect method to all the promises we return that will make the REPL, console.log, and util.inspect render it nicely in node.js:
require('unexpected-bluebird').prototype.inspect = function() {
require('unexpected-bluebird').prototype[
require('./nodeJsCustomInspect')
] = function() {
return module.exports
.createOutput(require('magicpen').defaultFormat)
.appendInspected(this)
Expand Down
8 changes: 8 additions & 0 deletions lib/nodeJsCustomInspect.js
@@ -0,0 +1,8 @@
module.exports = 'inspect';

try {
const util = require('' + 'util');
if (util && typeof util.inspect === 'function' && util.inspect.custom) {
module.exports = util.inspect.custom;
}
} catch (err) {}
23 changes: 18 additions & 5 deletions test/api/promise.spec.js
Expand Up @@ -258,6 +258,15 @@ describe('expect.promise', () => {
});
});

let inspectMethodName = 'inspect';
// In node.js 10+ the custom inspect method name has to be given as a symbol
// or we'll get ugly deprecation warnings logged to the console.
try {
const util = require('util');
if (util.inspect.custom) {
inspectMethodName = util.inspect.custom;
}
} catch (err) {}
describe('#inspect', () => {
var originalDefaultFormat = expect.output.constructor.defaultFormat;
beforeEach(() => {
Expand All @@ -273,7 +282,7 @@ describe('expect.promise', () => {
.promise(function() {
expect(2, 'to equal', 2);
})
.inspect(),
[inspectMethodName](),
'to equal',
'Promise (fulfilled)'
);
Expand All @@ -285,7 +294,7 @@ describe('expect.promise', () => {
.promise(function() {
return 123;
})
.inspect(),
[inspectMethodName](),
'to equal',
'Promise (fulfilled) => 123'
);
Expand All @@ -298,7 +307,11 @@ describe('expect.promise', () => {
'to equal',
'foo'
);
expect(asyncPromise.inspect(), 'to equal', 'Promise (pending)');
expect(
asyncPromise[inspectMethodName](),
'to equal',
'Promise (pending)'
);
return asyncPromise;
});

Expand All @@ -308,7 +321,7 @@ describe('expect.promise', () => {
});

return promise.caught(function() {
expect(promise.inspect(), 'to equal', 'Promise (rejected)');
expect(promise[inspectMethodName](), 'to equal', 'Promise (rejected)');
});
});

Expand All @@ -321,7 +334,7 @@ describe('expect.promise', () => {

return promise.caught(function() {
expect(
promise.inspect(),
promise[inspectMethodName](),
'to equal',
"Promise (rejected) => Error('argh')"
);
Expand Down

0 comments on commit 6252031

Please sign in to comment.