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

Deprecate value argument in "to have property" #688

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
17 changes: 14 additions & 3 deletions lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<object> to have [own] property <string> <any>',
(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)
Expand All @@ -315,7 +325,8 @@ module.exports = expect => {
expect(actualPropertyValue, 'to equal', expectedPropertyValue);
return actualPropertyValue;
}
)
);
}
);

expect.addAssertion(
Expand Down
24 changes: 24 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be cleaver about how much we show that warning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we can make a proper style for the warning in styles.js and make a util for printing them to the console.

Copy link
Member Author

@alexjeffburke alexjeffburke Jan 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep it simple.


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)
);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get the output format this way:

const format = expect.outputFormat()

So this should also work:

const output = expect.output.clone(expect.outputFormat())

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sunesimonsen that actually doesn't work currently - seems that the method is only allowed on the top-level expect.. should we change that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be okay with me. Just keep in mind that this method also allow you to give it an argument to change the default format.

}
});