Skip to content
Open
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
16 changes: 13 additions & 3 deletions lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,17 @@ module.exports = expect => {

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) {
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)
Expand All @@ -315,7 +324,8 @@ module.exports = expect => {
expect(actualPropertyValue, 'to equal', expectedPropertyValue);
return actualPropertyValue;
}
)
);
}
);

expect.addAssertion(
Expand Down
4 changes: 3 additions & 1 deletion lib/createTopLevelExpect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions lib/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("'")
Expand Down
13 changes: 13 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,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)
);
}
});
17 changes: 17 additions & 0 deletions test/api/outputFormat.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,22 @@ describe('outputFormat', () => {
}
);
});

it('throws if being reset on a child expect', () => {
const clonedExpect = expect
.clone()
.addAssertion('<string> 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'
);
});
});
});