diff --git a/lib/assertions.js b/lib/assertions.js index 1234351bb..783ef2b31 100644 --- a/lib/assertions.js +++ b/lib/assertions.js @@ -302,8 +302,17 @@ module.exports = expect => { expect.addAssertion( ' to have [own] property ', - (expect, subject, key, expectedPropertyValue) => - expect(subject, 'to have [own] property', key).then( + (expect, subject, key, expectedPropertyValue) => { + if (expectedPropertyValue) { + utils.deprecationWarning( + "unexpected: The value argument of 'to have property' assertion is deprecated.\n" + + "Please use 'to have properties' with object argument instead:\n" + + 'http://unexpected.js.org/assertions/object/to-have-properties/', + expect + ); + } + + return expect(subject, 'to have [own] property', key).then( actualPropertyValue => { expect.argsOutput = function() { this.appendInspected(key) @@ -315,7 +324,8 @@ module.exports = expect => { expect(actualPropertyValue, 'to equal', expectedPropertyValue); return actualPropertyValue; } - ) + ); + } ); expect.addAssertion( diff --git a/lib/createTopLevelExpect.js b/lib/createTopLevelExpect.js index 6d7797c95..393c3ae9b 100644 --- a/lib/createTopLevelExpect.js +++ b/lib/createTopLevelExpect.js @@ -1633,7 +1633,9 @@ expectPrototype.freeze = function() { }; expectPrototype.outputFormat = function(format) { - this._assertTopLevelExpect(); + if (format) { + this._assertTopLevelExpect(); + } if (typeof format === 'undefined') { return this._outputFormat; } else { diff --git a/lib/styles.js b/lib/styles.js index e98a4a02a..624ef62c6 100644 --- a/lib/styles.js +++ b/lib/styles.js @@ -93,6 +93,10 @@ module.exports = expect => { } }); + expect.addStyle('deprecationWarning', function(value) { + this.text(value, 'bgYellow', 'black'); + }); + expect.addStyle('singleQuotedString', function(content) { content = String(content); this.jsString("'") diff --git a/lib/utils.js b/lib/utils.js index cf4ec5760..034b86e05 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -4,6 +4,7 @@ const canSetPrototype = Object.setPrototypeOf || { __proto__: [] } instanceof Array; const greedyIntervalPacker = require('greedy-interval-packer'); +const magicpen = require('magicpen'); const setPrototypeOf = Object.setPrototypeOf || @@ -375,5 +376,17 @@ const utils = (module.exports = { } else if (typeof process === 'object' && process.env) { return process.env[varName]; } + }, + + deprecationWarning(message, expect) { + let format = expect.outputFormat(); + if (format === 'html') { + // override given this will be output in the browser console + format = 'text'; + } + + console.warn( + expect.output.clone().deprecationWarning(message).toString(format) + ); } }); diff --git a/test/api/outputFormat.spec.js b/test/api/outputFormat.spec.js index a297dec4e..01c393432 100644 --- a/test/api/outputFormat.spec.js +++ b/test/api/outputFormat.spec.js @@ -28,5 +28,22 @@ describe('outputFormat', () => { } ); }); + + it('throws if being reset on a child expect', () => { + const clonedExpect = expect + .clone() + .addAssertion(' to foo', (expect, subject) => { + expect.child().outputFormat('html'); + expect(subject, 'to contain', 'foo'); + }); + + expect( + () => { + clonedExpect('foobar', 'to foo'); + }, + 'to throw', + 'This method only works on the top level expect function' + ); + }); }); });