From 41e2a865213e16acdba101c613b0973fa278d82f Mon Sep 17 00:00:00 2001 From: Alex J Burke Date: Sun, 5 Jan 2020 17:00:30 +0100 Subject: [PATCH 1/4] Add a utility method for creating deprecation warnings. Arrange for this to contain the logic for showing once. Do a little extra work to make sure the warning is coloured on the console. --- lib/utils.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/utils.js b/lib/utils.js index cf4ec5760..efe5aea7a 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,28 @@ const utils = (module.exports = { } else if (typeof process === 'object' && process.env) { return process.env[varName]; } + }, + + createDeprecationWarning(message) { + let deprecationWarningDisplayed = false; + + return () => { + if (deprecationWarningDisplayed) { + return; + } + + deprecationWarningDisplayed = true; + + let format = magicpen.defaultFormat; + if (format === 'html') { + // override given this will be output in the browser console + format = 'text'; + } + console.warn( + magicpen() + .text(message, 'bgYellow', 'black') + .toString(format) + ); + }; } }); From 01e644aed04d5e405a179cd5c9b91913feb6460b Mon Sep 17 00:00:00 2001 From: Alex J Burke Date: Sun, 5 Jan 2020 17:08:16 +0100 Subject: [PATCH 2/4] Output deprecation notice for "to have property" with value argument. --- lib/assertions.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/assertions.js b/lib/assertions.js index 1234351bb..47577fa75 100644 --- a/lib/assertions.js +++ b/lib/assertions.js @@ -300,10 +300,20 @@ module.exports = expect => { } ); + const toHavePropertyValueDeprecation = utils.createDeprecationWarning( + "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.addAssertion( ' to have [own] property ', - (expect, subject, key, expectedPropertyValue) => - expect(subject, 'to have [own] property', key).then( + (expect, subject, key, expectedPropertyValue) => { + if (expectedPropertyValue) { + toHavePropertyValueDeprecation(); + } + + return expect(subject, 'to have [own] property', key).then( actualPropertyValue => { expect.argsOutput = function() { this.appendInspected(key) @@ -315,7 +325,8 @@ module.exports = expect => { expect(actualPropertyValue, 'to equal', expectedPropertyValue); return actualPropertyValue; } - ) + ); + } ); expect.addAssertion( From edd8e4647ee1e81acdf2a2fd43929665670450ae Mon Sep 17 00:00:00 2001 From: Alex J Burke Date: Thu, 9 Jan 2020 09:23:28 +0100 Subject: [PATCH 3/4] Loosen assert on outputFormat so querying is allowed on any expect. --- lib/createTopLevelExpect.js | 4 +++- test/api/outputFormat.spec.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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/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' + ); + }); }); }); From befa754e4117eec17564ec3a7d405cc5930336ec Mon Sep 17 00:00:00 2001 From: Alex J Burke Date: Fri, 10 Jan 2020 23:20:16 +0100 Subject: [PATCH 4/4] Simplify the deprecation warning utility function. Create a deprecationWarning style used for the yellow on black colouring. Keep things simple (per review) and remove the one-time only check which further reduces things down to a simple call. Pass the expect instance for its .output rather than requiring magicpen. --- lib/assertions.js | 13 ++++++------- lib/styles.js | 4 ++++ lib/utils.js | 29 +++++++++-------------------- 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/lib/assertions.js b/lib/assertions.js index 47577fa75..783ef2b31 100644 --- a/lib/assertions.js +++ b/lib/assertions.js @@ -300,17 +300,16 @@ module.exports = expect => { } ); - const toHavePropertyValueDeprecation = utils.createDeprecationWarning( - "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.addAssertion( ' to have [own] property ', (expect, subject, key, expectedPropertyValue) => { if (expectedPropertyValue) { - toHavePropertyValueDeprecation(); + 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( 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 efe5aea7a..034b86e05 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -378,26 +378,15 @@ const utils = (module.exports = { } }, - createDeprecationWarning(message) { - let deprecationWarningDisplayed = false; - - return () => { - if (deprecationWarningDisplayed) { - return; - } - - deprecationWarningDisplayed = true; + deprecationWarning(message, expect) { + let format = expect.outputFormat(); + if (format === 'html') { + // override given this will be output in the browser console + format = 'text'; + } - let format = magicpen.defaultFormat; - if (format === 'html') { - // override given this will be output in the browser console - format = 'text'; - } - console.warn( - magicpen() - .text(message, 'bgYellow', 'black') - .toString(format) - ); - }; + console.warn( + expect.output.clone().deprecationWarning(message).toString(format) + ); } });