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

Avoid deprecation warnings in node.js 10 because we have methods called inspect #577

Merged
merged 5 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions lib/Unexpected.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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